Advertisement
ISSOtm

31-bit "10^6 modulo" function

Sep 10th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Performs [hl] mod 10^6, where [hl] is a 4-byte big-endian integer
  2. ; [hl] is edited in place
  3.  
  4. ; Principle of modulus : subtract until there's not enough to subtract, the remainder is the result
  5. ; Principle of this : subtract until we overshot it by 1 subtraction, then compensate by adding once
  6.  
  7. TEN_POW_SIX equ 1000000
  8.  
  9. Modulo10pow6:
  10.     res 7, [hl] ; Reset this bit, it's not part of the number (and we're allowed to trash it \:D)
  11.     inc hl
  12. .subtractOnce
  13.     inc hl
  14.     inc hl ; Repoint to last byte
  15.     ; Subtract 10^6 from our number...
  16.     ld a, [hl]
  17.     sub TEN_POW_SIX & $FF
  18.     ld [hld], a
  19.     ld a, [hl]
  20.     sbc (TEN_POW_SIX & $FF00) >> 8
  21.     ld [hld], a
  22.     ld a, [hl]
  23.     sbc (TEN_POW_SIX & $FF0000) >> 16
  24.     ld [hld], a
  25.     ld a, [hl]
  26.     sbc (TEN_POW_SIX & $FF000000) >> 24
  27.     ld [hli], a
  28.     jr nc, .subtractOnce ; If there's something remaining then keep subtracting
  29.    
  30.     ; Oh god, we went too far, we subtracted one time too much
  31.     inc hl
  32.     inc hl
  33.     ; Add 10^6 to our number to obtain the modulo, then.
  34.     ld a, [hl]
  35.     add TEN_POW_SIX & $FF
  36.     ld [hld], a
  37.     ld a, [hl]
  38.     adc (TEN_POW_SIX & $FF00) >> 8
  39.     ld [hld], a
  40.     ld a, [hl]
  41.     adc (TEN_POW_SIX & $FF0000) >> 16
  42.     ld [hld], a
  43.     ld a, [hl]
  44.     adc (TEN_POW_SIX & $FF000000) >> 24
  45.     ld [hli], a
  46.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement