-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.s
154 lines (147 loc) · 3.39 KB
/
hash.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
global hashf_krlose
global hashf_djb2
global hashf_sdbm
global hashf_jenkins_one_at_a_time
section .data
djb2_seed equ 5381
section .text
;
; uint32_t hashf_rklose(const char *str) {
; uint32_t hash = 0;
; while (*str) {
; hash += *str;
; str++;
; }
; return hash;
; }
;
; store pointer to input data in rdi
; return hash in eax
hashf_krlose:
xor eax, eax ; hash accum.
xor ebx, ebx ; current byte
.hashbyte:
mov bl, byte[rdi]
cmp bl, 0 ; end of the data
jz .ret
add eax, ebx ; hash += *str
inc rdi
jmp .hashbyte
.ret:
ret
;
; uint32_t hashf_djb2(const char *str) {
; uint32_t hash = 5381;
; while (*str) {
; hash = ((hash << 5) + hash) + *str;
; str++;
; }
; return hash;
; }
;
; store pointer to input data in rdi
; return hash in eax
hashf_djb2:
mov eax, djb2_seed ; initialize hash
xor ebx, ebx ; current byte
xor edx, edx ; for bitwise shifts
.hashbyte: ; for each byte of data (until zero is reached)
mov bl, byte[rdi] ; take 1 byte from input data
cmp bl, 0 ; end of the data
jz .ret
; hash << 5
mov edx, eax ; store hash value
shl edx, 5
; + hash
add eax, edx
; + *str
add eax, ebx
inc rdi
jmp .hashbyte
.ret:
ret
;
; uint32_t hashf_sdbm(const char *str) {
; uint32_t hash = 0;
; while (*str) {
; hash = *str + (hash << 6) + (hash << 16) - hash;
; str++;
; }
; return hash;
; }
;
; store pointer to input data in rdi
; return hash in eax
hashf_sdbm:
xor eax, eax ; hash accum.
xor ebx, ebx ; current byte
xor edx, edx ; for bitwise shifts
xor ecx, ecx ; for bitwise shifts
.hashbyte: ; for each byte of input data (until zero is reached)
mov bl, byte[rdi]
cmp bl, 0
jz .ret
; hash << 6
mov edx, eax
shl edx, 6
; hash << 16
mov ecx, eax
shl ecx, 16
add edx, ebx ; + *str
add edx, ecx ; (hash << 6) + (hash << 16)
sub edx, eax ; - hash
mov eax, edx
inc rdi
jmp .hashbyte
.ret:
ret
;
; uint32_t hashf_jenkins_one_at_a_time(const char *str) {
; uint32_t hash = 0;
; while (*str) {
; hash += *str;
; hash += (hash << 10);
; hash ^= (hash >> 6);
; str++;
; }
; hash += (hash << 3);
; hash ^= (hash >> 11);
; hash += (hash << 15);
; return hash;
; }
;
; store pointer to input data in rdi
; return hash in eax
hashf_jenkins_one_at_a_time:
xor eax, eax ; hash accum.
xor ebx, ebx ; current byte
xor edx, edx ; for bitwise shifts
.hashbyte:
mov bl, byte[rdi] ; take 1 byte from input data
cmp bl, 0
jz .ret
add eax, ebx ; hash += *str
; hash += (hash << 10)
mov edx, eax
shl edx, 10
add eax, edx
; hash ^= (hash >> 6)
mov edx, eax
shr edx, 6
xor eax, edx ; storing result in first argument (eax)
inc rdi ; str++
jmp .hashbyte
.ret:
; hash += (hash << 3)
mov edx, eax
shl edx, 3
add eax, edx
; hash ^= (hash >> 11)
mov edx, eax
shr edx, 11
xor eax, edx
; hash += (hash << 15)
mov edx, eax
shl edx, 15
add eax, edx
ret