Advertisement
Zeda

GCDHL_BC

Jan 4th, 2012
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GCDHL_BC:
  2. ;Inputs:
  3. ;     HL is a number
  4. ;     BC is a number
  5. ;Outputs:
  6. ;     A is 0
  7. ;     BC is the GCD
  8. ;     DE is 0
  9. ;Destroys:
  10. ;     HL
  11. ;Size:  25 bytes
  12. ;Speed: 30 to 49708 cycles
  13. ;       -As slow as about 126 times per second at 6MHz
  14. ;       -As fast as about 209715 times per second at 6MHz
  15. ;Speed break down:
  16. ;     If HL=BC, 30 cycles
  17. ;     24+1552x
  18. ;     If BC>HL, add 20 cycles
  19. ;     *x is from 1 to at most 32 (because we use 2 16-bit numbers)
  20. ;
  21.      or a \ sbc hl,bc     ;B7ED42    19
  22.      ret z                ;C8        5|11
  23.      add hl,bc            ;09        11
  24.      jr nc,$+8            ;3006      11|31
  25.        ld a,h             ;7C        --
  26.        ld h,b             ;60        --
  27.        ld b,a             ;47        --
  28.        ld a,l             ;7D        --
  29.        ld l,c             ;69        --
  30.        ld c,a             ;4F        --
  31. Loop:
  32.      call HL_Div_BC       ;CD****    1511
  33.      ld a,d \ or e        ;7AB2      8
  34.      ret z                ;C8        5|11
  35.      ld h,b \ ld l,c      ;6069      8
  36.      ld b,d \ ld c,e      ;424B      8
  37.      jr $-10              ;18F8      12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement