Skip to content

Instantly share code, notes, and snippets.

@v1stra
Last active January 21, 2023 13:26
Show Gist options
  • Select an option

  • Save v1stra/a173cec7f6f6e1ab417d5197c2169e09 to your computer and use it in GitHub Desktop.

Select an option

Save v1stra/a173cec7f6f6e1ab417d5197c2169e09 to your computer and use it in GitHub Desktop.
DJB2 Hash in assembly
.code
asm_hash PROC
push rbp
mov rbp, rsp
mov qword ptr [rbp - 28h], rdi
mov qword ptr [rbp - 30h], rsi
mov qword ptr [rbp - 10h], 1505h
mov rax, qword ptr [rbp - 28h]
mov qword ptr [rbp - 8h], rcx
loc_20:
mov rax, qword ptr [rbp - 8h]
movzx eax, byte ptr [rax]
mov byte ptr [rbp - 11h], al
cmp qword ptr [rbp - 30h], 0
jne short loc_3E
mov rax, qword ptr [rbp - 8]
movzx eax, byte ptr [rax]
test al, al
jnz short loc_5E
jmp short loc_8D
loc_3E:
mov rax, qword ptr [rbp - 8]
sub rax, qword ptr [rbp - 28h]
cmp qword ptr [rbp - 30h], rax
jbe short loc_8C
mov rax, qword ptr [rbp - 8]
movzx eax, byte ptr [rax]
test al, al
jne short loc_5E
add qword ptr [rbp - 8], 1
jmp short loc_8A
loc_5E:
cmp byte ptr [rbp - 11h], 60h
jbe short loc_68
sub byte ptr [rbp - 11h], 20h
loc_68:
mov rax, qword ptr [rbp - 10h]
shl rax, 5
mov rdx, rax
mov rax, qword ptr [rbp - 10h]
add rdx, rax
movzx eax, byte ptr [rbp - 11h]
add rax, rdx
mov qword ptr [rbp - 10h], rax
add qword ptr [rbp - 8], 1
loc_8A:
jmp short loc_20
loc_8C:
nop
loc_8D:
mov rax, qword ptr [rbp - 10h]
pop rbp
ret
asm_hash ENDP
end
#include <stdio.h>
#include <Windows.h>
EXTERN_C unsigned long long asm_hash(void* in, unsigned long len);
#define DEFINE(definition, ntapi) printf("#define %s\t0x%llx\n", definition, asm_hash(ntapi, strlen(ntapi)))
int main()
{
DEFINE("NT_ALLOCATE_VIRTUAL_MEMORY", "NtAllocateVirtualMemory");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment