Gave names to the functions and labels and adresses used in DivideBCD

and added a few comments
This commit is contained in:
Joe Farebrother 2017-03-19 21:09:24 +00:00
parent ce35f2f7a1
commit 39d31cecb2
2 changed files with 75 additions and 72 deletions

View file

@ -6,155 +6,157 @@ DivideBCDPredef4::
DivideBCD:: DivideBCD::
xor a xor a
ld [$ffa5], a ld [hDivideBCDBuffer], a
ld [$ffa6], a ld [hDivideBCDBuffer+1], a
ld [$ffa7], a ld [hDivideBCDBuffer+2], a
ld d, $1 ld d, $1
.asm_f72a .mulBy10Loop
ld a, [$ffa2] ; multiply the divisor by 10 until the leading digit is nonzero
; to set up the standard long division algorithm
ld a, [hDivideBCDDivisor]
and $f0 and $f0
jr nz, .asm_f75b jr nz, .next
inc d inc d
ld a, [$ffa2] ld a, [hDivideBCDDivisor]
swap a swap a
and $f0 and $f0
ld b, a ld b, a
ld a, [$ffa3] ld a, [hDivideBCDDivisor+1]
swap a swap a
ld [$ffa3], a ld [hDivideBCDDivisor+1], a
and $f and $f
or b or b
ld [$ffa2], a ld [hDivideBCDDivisor], a
ld a, [$ffa3] ld a, [hDivideBCDDivisor+1]
and $f0 and $f0
ld b, a ld b, a
ld a, [$ffa4] ld a, [hDivideBCDDivisor+2]
swap a swap a
ld [$ffa4], a ld [hDivideBCDDivisor+2], a
and $f and $f
or b or b
ld [$ffa3], a ld [hDivideBCDDivisor+1], a
ld a, [$ffa4] ld a, [hDivideBCDDivisor+2]
and $f0 and $f0
ld [$ffa4], a ld [hDivideBCDDivisor+2], a
jr .asm_f72a jr .mulBy10Loop
.asm_f75b .next
push de push de
push de push de
call DivideBCD_f800 call DivideBCD_getNextDigit
pop de pop de
ld a, b ld a, b
swap a swap a
and $f0 and $f0
ld [$ffa5], a ld [hDivideBCDBuffer], a
dec d dec d
jr z, .asm_f7bc jr z, .next2
push de push de
call DivideBCD_f7d7 call DivideBCD_divDivisorBy10
call DivideBCD_f800 call DivideBCD_getNextDigit
pop de pop de
ld a, [$ffa5] ld a, [hDivideBCDBuffer]
or b or b
ld [$ffa5], a ld [hDivideBCDBuffer], a
dec d dec d
jr z, .asm_f7bc jr z, .next2
push de push de
call DivideBCD_f7d7 call DivideBCD_divDivisorBy10
call DivideBCD_f800 call DivideBCD_getNextDigit
pop de pop de
ld a, b ld a, b
swap a swap a
and $f0 and $f0
ld [$ffa6], a ld [hDivideBCDBuffer+1], a
dec d dec d
jr z, .asm_f7bc jr z, .next2
push de push de
call DivideBCD_f7d7 call DivideBCD_divDivisorBy10
call DivideBCD_f800 call DivideBCD_getNextDigit
pop de pop de
ld a, [$ffa6] ld a, [hDivideBCDBuffer+1]
or b or b
ld [$ffa6], a ld [hDivideBCDBuffer+1], a
dec d dec d
jr z, .asm_f7bc jr z, .next2
push de push de
call DivideBCD_f7d7 call DivideBCD_divDivisorBy10
call DivideBCD_f800 call DivideBCD_getNextDigit
pop de pop de
ld a, b ld a, b
swap a swap a
and $f0 and $f0
ld [$ffa7], a ld [hDivideBCDBuffer+2], a
dec d dec d
jr z, .asm_f7bc jr z, .next2
push de push de
call DivideBCD_f7d7 call DivideBCD_divDivisorBy10
call DivideBCD_f800 call DivideBCD_getNextDigit
pop de pop de
ld a, [$ffa7] ld a, [hDivideBCDBuffer+2]
or b or b
ld [$ffa7], a ld [hDivideBCDBuffer+2], a
.asm_f7bc .next2
ld a, [$ffa5] ld a, [hDivideBCDBuffer]
ld [$ffa2], a ld [hDivideBCDQuotient], a ; the same memory location as hDivideBCDDivisor
ld a, [$ffa6] ld a, [hDivideBCDBuffer+1]
ld [$ffa3], a ld [hDivideBCDQuotient+1], a
ld a, [$ffa7] ld a, [hDivideBCDBuffer+2]
ld [$ffa4], a ld [hDivideBCDQuotient+2], a
pop de pop de
ld a, $6 ld a, $6
sub d sub d
and a and a
ret z ret z
.asm_f7ce .divResultBy10loop
push af push af
call DivideBCD_f7d7 call DivideBCD_divDivisorBy10
pop af pop af
dec a dec a
jr nz, .asm_f7ce jr nz, .divResultBy10loop
ret ret
DivideBCD_f7d7: DivideBCD_divDivisorBy10:
ld a, [$ffa4] ld a, [hDivideBCDDivisor+2]
swap a swap a
and $f and $f
ld b, a ld b, a
ld a, [$ffa3] ld a, [hDivideBCDDivisor+1]
swap a swap a
ld [$ffa3], a ld [hDivideBCDDivisor+1], a
and $f0 and $f0
or b or b
ld [$ffa4], a ld [hDivideBCDDivisor+2], a
ld a, [$ffa3] ld a, [hDivideBCDDivisor+1]
and $f and $f
ld b, a ld b, a
ld a, [$ffa2] ld a, [hDivideBCDDivisor]
swap a swap a
ld [$ffa2], a ld [hDivideBCDDivisor], a
and $f0 and $f0
or b or b
ld [$ffa3], a ld [hDivideBCDDivisor+1], a
ld a, [$ffa2] ld a, [hDivideBCDDivisor]
and $f and $f
ld [$ffa2], a ld [hDivideBCDDivisor], a
ret ret
DivideBCD_f800: DivideBCD_getNextDigit:
ld bc, $3 ld bc, $3
.asm_f803 .loop
ld de, $ff9f ld de, hMoney ; the dividend
ld hl, $ffa2 ld hl, hDivideBCDDivisor
push bc push bc
call StringCmp call StringCmp
pop bc pop bc
ret c ret c
inc b inc b
ld de, $ffa1 ld de, hMoney+2 ; since SubBCD works starting from the least significant digit
ld hl, $ffa4 ld hl, hDivideBCDDivisor+2
push bc push bc
call SubBCD call SubBCD
pop bc pop bc
jr .asm_f803 jr .loop
AddBCDPredef:: AddBCDPredef::

View file

@ -155,6 +155,7 @@ hCoins EQU $FFA0 ; 2-byte BCD number
hDivideBCDDivisor EQU $FFA2 ; 3-byte BCD number hDivideBCDDivisor EQU $FFA2 ; 3-byte BCD number
hDivideBCDQuotient EQU $FFA2 ; 3-byte BCD number hDivideBCDQuotient EQU $FFA2 ; 3-byte BCD number
hDivideBCDBuffer EQU $FFA5 ; 3-byte BCD number
hSerialReceivedNewData EQU $FFA9 hSerialReceivedNewData EQU $FFA9