Advertisement
Zeda

BCD_add, BCD_sub, BCD_rsub

Jul 5th, 2017
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Routines:
  2. ;for the BCD_* routines, HL is input 1 ('x'), DE is input 2 ('y'), and BC is where to output the result ('z')
  3. ;  BCD_add      x+y -> z, preserves all registers
  4. ;  BCD_sub      x-y -> z, preserves all registers
  5. ;  BCD_rsub    -x+y -> z, preserves all registers
  6. ;  roundfpOP0   rounds fpOP0 from an extended float to a regular float.
  7. ;  normalize    normalizes the float
  8. ;  loadinputs   Takes the float at HL and stores it as an extended float in fpOP0, and does the same for DE->fpOP1
  9. ;  mov9         Moves 9 bytes from (HL) to (DE), decrements BC by 9
  10. ;  mov7         Moves 7 bytes from (HL) to (DE), decrements BC by 7
  11. BCD_rsub:
  12.     push hl
  13.     push de
  14.     push bc
  15.     push af
  16.     push bc
  17.     call +_
  18.     pop de
  19.     ld hl,fpOP0
  20.     call mov9
  21.     pop af
  22.     pop bc
  23.     pop de
  24.     pop hl
  25.     ret
  26. _:
  27.     inc hl
  28.     inc de
  29.     ld a,(de)
  30.     sub (hl)
  31.     jr nc,$+5
  32.     ex de,hl
  33.     neg
  34.     dec hl
  35.     dec de
  36.     call loadInputs
  37.     push af
  38.     ld a,80
  39.     ld hl,fpOP1
  40.     jr c,$+5
  41.     ld hl,fpOP0
  42.     xor (hl)
  43.     ld (hl),a
  44.     pop af
  45.     jp BCD_add_stepin
  46. BCD_sub:
  47.     push hl
  48.     push de
  49.     push bc
  50.     push af
  51.     push bc
  52.     call +_
  53.     pop de
  54.     ld hl,fpOP0
  55.     call mov9
  56.     pop af
  57.     pop bc
  58.     pop de
  59.     pop hl
  60.     ret
  61. _:
  62.     inc hl
  63.     inc de
  64.     ld a,(de)
  65.     sub (hl)
  66.     jr nc,$+5
  67.     ex de,hl
  68.     neg
  69.     dec hl
  70.     dec de
  71.     call loadInputs
  72.     push af
  73.     ld a,80
  74.     ld hl,fpOP0
  75.     jr c,$+5
  76.     ld hl,fpOP1
  77.     xor (hl)
  78.     ld (hl),a
  79.     pop af
  80.     jp BCD_add_stepin
  81. BCD_add:
  82.     push hl
  83.     push de
  84.     push bc
  85.     push af
  86.     push bc
  87.     call +_
  88.     pop de
  89.     ld hl,fpOP0
  90.     call mov9
  91.     pop af
  92.     pop bc
  93.     pop de
  94.     pop hl
  95.     ret
  96. _:
  97.     inc hl
  98.     inc de
  99.     ld a,(de)
  100.     sub (hl)
  101.     jr nc,$+5
  102.     ex de,hl
  103.     neg
  104.     dec hl
  105.     dec de
  106.     call loadInputs
  107. BCD_add_stepin:
  108.     or a
  109.     jr z,BCD_add_shift_done
  110.     cp 15
  111.     ret nc
  112.     srl a
  113.     jr nc,BCD_add_halfshiftdone
  114.     push af
  115.     ld hl,fpOP1+2
  116.     xor a
  117.     ld b,8
  118.     rld
  119.     inc hl
  120.     djnz $-3
  121.     pop af
  122. BCD_add_halfshiftdone:
  123.     jr z,BCD_add_shift_done
  124.     ld de,fpOP1+9
  125.     ld h,d
  126.     ld l,a
  127.     ld a,e
  128.     sub l
  129.     ld l,a
  130.     jr nc,$+3
  131.     dec h
  132.     ld a,8
  133.     add a,l
  134.     sub e
  135.     ld c,a
  136.     ld b,0
  137.     ldir
  138. BCD_add_shift_done:
  139.     ld hl,fpOP1
  140.     ld a,(fpOP0)
  141.     xor (hl)
  142.     jp m,bcd_add_sub
  143.     ld hl,fpOP0+9
  144.     ld de,fpOP1+9
  145.     ld b,8
  146. _:
  147.     ld a,(de)
  148.     adc a,(hl)
  149.     daa
  150.     ld (hl),a
  151.     dec de
  152.     dec hl
  153.     djnz -_
  154.     jr nc,roundfpOP0
  155.     inc (hl)
  156.     jr z,err_Overflow
  157.     ld a,1
  158.     ld hl,fpOP0+2
  159.     ld b,8
  160.     rld
  161.     inc hl
  162.     djnz $-3
  163. roundfpOP0:
  164.     ld a,(fpOP0+9)
  165.     cp $50
  166.     ret c
  167.     ld hl,fpOP0+8
  168.     inc (hl)
  169.     dec hl
  170.     jr nz,$-2
  171.     ld bc,fpOP0
  172.     sbc hl,bc
  173.     ret nz
  174.     inc hl
  175.     inc hl
  176.     ld (hl),$10
  177.     ret
  178. bcd_add_sub:
  179.     ld hl,fpOP0+9
  180.     ld de,fpOP1+9
  181.     ld b,8
  182. _:
  183.     ld a,(de)
  184.     sbc a,(hl)
  185.     daa
  186.     ld (hl),a
  187.     dec de
  188.     dec hl
  189.     djnz -_
  190.     jr nc,normalize
  191.     dec hl
  192.     ld a,80h
  193.     xor (hl)
  194.     ld (hl),a
  195.     ld hl,fpOP0+9
  196.     xor a
  197.     ld c,a
  198.     ld b,8
  199. _:
  200.     ld a,c
  201.     sbc a,(hl)
  202.     daa
  203.     ld (hl),a
  204.     dec hl
  205.     djnz -_
  206. normalize:
  207.     ld hl,fpOP0+2
  208.     ld a,(hl)
  209.     cp 16
  210.     jp nc,roundfpOP0
  211.     ld b,8
  212.     ld a,(hl)
  213.     or a
  214.     jr nz,$+8_
  215.     inc hl
  216.     djnz $-5
  217.     jp setfpOP0_Zero
  218.     ld de,fpOP0+2
  219.     ld a,8
  220.     sub b
  221.     ld c,b
  222.     ld b,0
  223.     ldir
  224.     jr z,$+8
  225.     ex de,hl
  226.     ld b,a
  227.     ld (hl),c
  228.     inc hl
  229.     djnz $-2
  230.     ld hl,fpOP0+2
  231.     ld a,(hl)
  232.     and $F0
  233.     jp nz,roundfpOP0
  234.     ld hl,fpOP0+9
  235.     ld b,8
  236.     rrd
  237.     dec hl
  238.     djnz $-3
  239.     jp roundfpOP0
  240. loadInputs:
  241.     push de
  242.     ld de,fpOP0
  243.     call mov7
  244.     pop hl
  245.     jp mov7
  246. mov9:
  247.     ldi
  248.     ldi
  249. mov7:
  250.     ldi
  251.     ldi
  252.     ldi
  253.     ldi
  254.     ldi
  255.     ldi
  256.     ldi
  257.     ldi
  258.     ldi
  259.     ex de,hl
  260.     ld (hl),0
  261.     ex de,hl
  262.     inc de
  263.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement