Zeda

gcdHL_DE

May 31st, 2016
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. gcdHL_DE:
  2. ;gcd(HL,DE)->HL
  3. ;binary GCD algorithm
  4.     ld a,h \ or l \ ret z
  5.     ex de,hl
  6.     ld a,h \ or l \ ret z
  7.     sbc hl,de
  8.     add hl,de
  9.     ret z
  10.     ld b,1
  11.     ld a,e \ or l \ rra \ jr c,+_
  12.     inc b
  13.     rr h \ rr l
  14.     rr d \ rr e
  15.     ld a,e \ or l \ rra \ jr nc,$-12
  16. _:
  17.     srl h \ rr l \ jr nc,$-4 \ adc hl,hl
  18.     ex de,hl
  19. _:
  20.     srl h \ rr l \ jr nc,$-4 \ adc hl,hl
  21.     xor a \ sbc hl,de
  22.     jr z,step4
  23.     jr nc,-_ \ sub l \ ld l,a \ sbc a,a \ sub h \ ld h,a
  24.     jp -_-1
  25. step4:
  26.     ex de,hl
  27.     dec b
  28.     ret z
  29.     add hl,hl
  30.     djnz $-1
  31.     ret
Add Comment
Please, Sign In to add comment