Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Routines:
- ;for the BCD_* routines, HL is input 1 ('x'), DE is input 2 ('y'), and BC is where to output the result ('z')
- ; BCD_add x+y -> z, preserves all registers
- ; BCD_sub x-y -> z, preserves all registers
- ; BCD_rsub -x+y -> z, preserves all registers
- ; roundfpOP0 rounds fpOP0 from an extended float to a regular float.
- ; normalize normalizes the float
- ; loadinputs Takes the float at HL and stores it as an extended float in fpOP0, and does the same for DE->fpOP1
- ; mov9 Moves 9 bytes from (HL) to (DE), decrements BC by 9
- ; mov7 Moves 7 bytes from (HL) to (DE), decrements BC by 7
- BCD_rsub:
- push hl
- push de
- push bc
- push af
- push bc
- call +_
- pop de
- ld hl,fpOP0
- call mov9
- pop af
- pop bc
- pop de
- pop hl
- ret
- _:
- inc hl
- inc de
- ld a,(de)
- sub (hl)
- jr nc,$+5
- ex de,hl
- neg
- dec hl
- dec de
- call loadInputs
- push af
- ld a,80
- ld hl,fpOP1
- jr c,$+5
- ld hl,fpOP0
- xor (hl)
- ld (hl),a
- pop af
- jp BCD_add_stepin
- BCD_sub:
- push hl
- push de
- push bc
- push af
- push bc
- call +_
- pop de
- ld hl,fpOP0
- call mov9
- pop af
- pop bc
- pop de
- pop hl
- ret
- _:
- inc hl
- inc de
- ld a,(de)
- sub (hl)
- jr nc,$+5
- ex de,hl
- neg
- dec hl
- dec de
- call loadInputs
- push af
- ld a,80
- ld hl,fpOP0
- jr c,$+5
- ld hl,fpOP1
- xor (hl)
- ld (hl),a
- pop af
- jp BCD_add_stepin
- BCD_add:
- push hl
- push de
- push bc
- push af
- push bc
- call +_
- pop de
- ld hl,fpOP0
- call mov9
- pop af
- pop bc
- pop de
- pop hl
- ret
- _:
- inc hl
- inc de
- ld a,(de)
- sub (hl)
- jr nc,$+5
- ex de,hl
- neg
- dec hl
- dec de
- call loadInputs
- BCD_add_stepin:
- or a
- jr z,BCD_add_shift_done
- cp 15
- ret nc
- srl a
- jr nc,BCD_add_halfshiftdone
- push af
- ld hl,fpOP1+2
- xor a
- ld b,8
- rld
- inc hl
- djnz $-3
- pop af
- BCD_add_halfshiftdone:
- jr z,BCD_add_shift_done
- ld de,fpOP1+9
- ld h,d
- ld l,a
- ld a,e
- sub l
- ld l,a
- jr nc,$+3
- dec h
- ld a,8
- add a,l
- sub e
- ld c,a
- ld b,0
- ldir
- BCD_add_shift_done:
- ld hl,fpOP1
- ld a,(fpOP0)
- xor (hl)
- jp m,bcd_add_sub
- ld hl,fpOP0+9
- ld de,fpOP1+9
- ld b,8
- _:
- ld a,(de)
- adc a,(hl)
- daa
- ld (hl),a
- dec de
- dec hl
- djnz -_
- jr nc,roundfpOP0
- inc (hl)
- jr z,err_Overflow
- ld a,1
- ld hl,fpOP0+2
- ld b,8
- rld
- inc hl
- djnz $-3
- roundfpOP0:
- ld a,(fpOP0+9)
- cp $50
- ret c
- ld hl,fpOP0+8
- inc (hl)
- dec hl
- jr nz,$-2
- ld bc,fpOP0
- sbc hl,bc
- ret nz
- inc hl
- inc hl
- ld (hl),$10
- ret
- bcd_add_sub:
- ld hl,fpOP0+9
- ld de,fpOP1+9
- ld b,8
- _:
- ld a,(de)
- sbc a,(hl)
- daa
- ld (hl),a
- dec de
- dec hl
- djnz -_
- jr nc,normalize
- dec hl
- ld a,80h
- xor (hl)
- ld (hl),a
- ld hl,fpOP0+9
- xor a
- ld c,a
- ld b,8
- _:
- ld a,c
- sbc a,(hl)
- daa
- ld (hl),a
- dec hl
- djnz -_
- normalize:
- ld hl,fpOP0+2
- ld a,(hl)
- cp 16
- jp nc,roundfpOP0
- ld b,8
- ld a,(hl)
- or a
- jr nz,$+8_
- inc hl
- djnz $-5
- jp setfpOP0_Zero
- ld de,fpOP0+2
- ld a,8
- sub b
- ld c,b
- ld b,0
- ldir
- jr z,$+8
- ex de,hl
- ld b,a
- ld (hl),c
- inc hl
- djnz $-2
- ld hl,fpOP0+2
- ld a,(hl)
- and $F0
- jp nz,roundfpOP0
- ld hl,fpOP0+9
- ld b,8
- rrd
- dec hl
- djnz $-3
- jp roundfpOP0
- loadInputs:
- push de
- ld de,fpOP0
- call mov7
- pop hl
- jp mov7
- mov9:
- ldi
- ldi
- mov7:
- ldi
- ldi
- ldi
- ldi
- ldi
- ldi
- ldi
- ldi
- ldi
- ex de,hl
- ld (hl),0
- ex de,hl
- inc de
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement