-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.asm
43 lines (31 loc) · 975 Bytes
/
hash.asm
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
; hash function
org &4000
key1 defb 'SWAP',0
HASH_ADDR equ &8888
ld a,0
ld de, key1
ld hl, &0000
; compute the ascii sum of consecutive memory addresses pointed at the defb instruction.
ascii_sum:
ld a, (de) ; store char for loop control (if A=0 exit)
ld (&5555), a ; move char to temp addr
ld bc, (&5555) ; move temp addr char to bc so we can add.
add hl, bc ; sum will be in HL
inc de
cp 0
jp nz,ascii_sum
; HL should be 013B (SWAP)
; compute the hash by doing ascii_sum MOD 255 (255 will be the length of the hashtable)
ld bc, &ff
;sbc hl, bc
compute_modulo:
sbc hl, bc
jp nc, compute_modulo
jp c, store_hash ; when this jumps, the value in HL will have generated a carry flag, the
; remainder would be at the previous calculation.
; HL should be 3C from the example.
store_hash:
adc hl, bc ; so we add &ff again to go back.
dec hl ; and add 1 (why the off-by-one here?)
ld (HASH_ADDR), hl
ret