Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Performs [hl] mod 10^6, where [hl] is a 4-byte big-endian integer
- ; [hl] is edited in place
- ; Principle of modulus : subtract until there's not enough to subtract, the remainder is the result
- ; Principle of this : subtract until we overshot it by 1 subtraction, then compensate by adding once
- TEN_POW_SIX equ 1000000
- Modulo10pow6:
- res 7, [hl] ; Reset this bit, it's not part of the number (and we're allowed to trash it \:D)
- inc hl
- .subtractOnce
- inc hl
- inc hl ; Repoint to last byte
- ; Subtract 10^6 from our number...
- ld a, [hl]
- sub TEN_POW_SIX & $FF
- ld [hld], a
- ld a, [hl]
- sbc (TEN_POW_SIX & $FF00) >> 8
- ld [hld], a
- ld a, [hl]
- sbc (TEN_POW_SIX & $FF0000) >> 16
- ld [hld], a
- ld a, [hl]
- sbc (TEN_POW_SIX & $FF000000) >> 24
- ld [hli], a
- jr nc, .subtractOnce ; If there's something remaining then keep subtracting
- ; Oh god, we went too far, we subtracted one time too much
- inc hl
- inc hl
- ; Add 10^6 to our number to obtain the modulo, then.
- ld a, [hl]
- add TEN_POW_SIX & $FF
- ld [hld], a
- ld a, [hl]
- adc (TEN_POW_SIX & $FF00) >> 8
- ld [hld], a
- ld a, [hl]
- adc (TEN_POW_SIX & $FF0000) >> 16
- ld [hld], a
- ld a, [hl]
- adc (TEN_POW_SIX & $FF000000) >> 24
- ld [hli], a
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement