Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/home/count_set_bits.asm
1270 views
1
; function to count how many bits are set in a string of bytes
2
; INPUT:
3
; hl = address of string of bytes
4
; b = length of string of bytes
5
; OUTPUT:
6
; [wNumSetBits] = number of set bits
7
CountSetBits::
8
ld c, 0
9
.loop
10
ld a, [hli]
11
ld e, a
12
ld d, 8
13
.innerLoop ; count how many bits are set in the current byte
14
srl e
15
ld a, 0
16
adc c
17
ld c, a
18
dec d
19
jr nz, .innerLoop
20
dec b
21
jr nz, .loop
22
ld a, c
23
ld [wNumSetBits], a
24
ret
25
26