Advertisement
Zeda

BC_Div_DE (Speed Tweak #2, +17 bytes -150.5cc avg).

Jun 23rd, 2015
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;+17 bytes -150.5cc
  2. ;===============================================================
  3. BC_Div_DE:
  4. ;===============================================================
  5. ;Performs BC/DE
  6. ;Speed:   890+6b+a cycles when DE<256 (1/256 of the time)
  7. ;         541+6b cycles when DE>=256 (255/256 of the time)
  8. ;         Avg = 566.37890625cc
  9. ;Size:    52 bytes
  10. ;Inputs:
  11. ;     BC is the numerator
  12. ;     DE is the denominator
  13. ;Outputs:
  14. ;     BC is the quotient
  15. ;     HL is the remainder
  16. ;     DE is not changed
  17. ;     A is 0
  18. ;     z flag is set
  19. ;     c flag is reset
  20. ;===============================================================
  21.     ld hl,0
  22.     ld a,b
  23.     inc d
  24.     dec d
  25.     jr z,upper
  26.     ld l,a
  27.     ld a,c
  28.     ld c,h
  29. lower:
  30.     ld b,8
  31. loop:
  32.     rla
  33.     adc hl,hl
  34.     sbc hl,de
  35.     jr nc,$+3
  36.     add hl,de
  37.     djnz loop
  38.     ld b,c
  39.     rla
  40.     cpl
  41.     ld c,a
  42.     ret
  43. upper:
  44.     ld b,8
  45.     ld a,h
  46. loop_upper:
  47.     rl c
  48.     rla
  49.     sub e
  50.     jr nc,$+3
  51.     add a,e
  52.     djnz loop_upper
  53.     ld l,a
  54.     ld a,c
  55.     rla
  56.     cpl
  57.     ld c,a
  58.     jp lower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement