Advertisement
Zeda

nCr_HL_DE

Jan 4th, 2012
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;===============================================================
  2. nCrHL_DE:
  3. ;===============================================================
  4. ;Inputs:
  5. ;     hl is "n"
  6. ;     de is "r"
  7. ;Outputs:
  8. ;     interrupts off
  9. ;     a is 0
  10. ;     bc is an intermediate result
  11. ;     de is "n"
  12. ;     hl is the result
  13. ;     a' is not changed
  14. ;     bc' is "r"+1
  15. ;     de' is the same as bc
  16. ;     hl' is "r" or the compliment, whichever is smaller
  17. ;===============================================================
  18.      or a                     ;reset carry flag
  19.      sbc hl,de
  20.      ret c                    ;r should not be bigger than n
  21.      sbc hl,de \ add hl,de
  22.      jr nc,$+3
  23.        ex de,hl
  24.                              ;hl is R
  25.      push de
  26.      ld bc,1                 ;A
  27.      exx
  28.      pop de                  ;N
  29.      ld bc,1                 ;C
  30.      ld h,b \ ld l,c         ;D
  31. nCrLoop:
  32.      push de
  33.      push hl
  34.      call DE_Times_BC
  35.      push hl \ exx \ pop de
  36.      push hl
  37.      call DE_Div_BC
  38.      pop de
  39.      push hl \ ex de,hl \ exx \ pop hl
  40.      ld b,h \ ld c,l
  41.      pop de \ add hl,de
  42.      pop de \ inc de
  43.      exx
  44.      inc bc
  45.      or a \ sbc hl,bc \ add hl,bc
  46.      exx
  47.      jr nc,nCrLoop
  48.      ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement