Advertisement
Zeda

HL_Div_BC_Signed

Jul 2nd, 2013
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;===============================================================
  2. HL_Div_BC_Signed:
  3. ;===============================================================
  4. ;Performs HL/BC
  5. ;Speed:   1344-55n-2b
  6. ;         b is the number of set bits in the result
  7. ;         n is the number of leading zeroes in the absolute value of HL, minus 1
  8. ;         add 24 if HL is negative
  9. ;         add 24 if BC is negative
  10. ;         add 28 if the result is negative
  11. ;Size:    68 bytes
  12. ;Inputs:
  13. ;     HL is the numerator
  14. ;     BC is the denominator
  15. ;Outputs:
  16. ;     DE is the quotient
  17. ;     HL is the remainder
  18. ;     BC is not changed
  19. ;Destroys:
  20. ;     A
  21. ;===============================================================
  22.      ld a,h
  23.      xor b
  24.      push af
  25. ;absHL
  26.      xor b
  27.      jp p,$+9
  28.      xor a \ sub l \ ld l,a
  29.      sbc a,a \ sub h \ ld h,a
  30. ;absBC:
  31.      xor b
  32.      jp p,$+9
  33.      xor a \ sub c \ ld c,a
  34.      sbc a,a \ sub b \ ld b,a
  35.  
  36.      ld de,0
  37.      adc hl,hl
  38.      jr z,EndSDiv
  39.      ld a,16
  40.  
  41.      add hl,hl
  42.      dec a
  43.      jp nc,$-2
  44.  
  45.      ex de,hl
  46.      jp jumpin
  47. Loop1:
  48.      add hl,bc     ;--
  49. Loop2:
  50.      dec a         ;4
  51.      jr z,EndSDiv  ;12|23
  52.      sla e         ;--
  53.      rl d          ;--
  54. jumpin:            ;
  55.      adc hl,hl     ;15
  56.      sbc hl,bc     ;15
  57.      jr c,Loop1    ;23-2b     ;b is the number of bits in the absolute value of the result.
  58.      inc e         ;--
  59.      jp Loop2      ;--
  60. EndSDiv:
  61.      pop af \ ret p
  62.      xor a \ sub e \ ld e,a
  63.      sbc a,a \ sub d \ ld d,a
  64.      ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement