Skip to content

Commit

Permalink
QoL aaaaaa123456789#13: Faster multiplication (AddNTimes)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaaaa123456789 authored and vulcandth committed Oct 16, 2022
1 parent ca651b7 commit eae158a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
30 changes: 15 additions & 15 deletions engine/math/math.asm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ _Multiply::

ldh a, [hMathBuffer + 2]
ld c, a
ldh a, [hMultiplicand + 0]
ldh a, [hMultiplicand]
adc c
ldh [hMathBuffer + 2], a

Expand All @@ -54,9 +54,9 @@ _Multiply::
rla
ldh [hMultiplicand + 1], a

ldh a, [hMultiplicand + 0]
ldh a, [hMultiplicand]
rla
ldh [hMultiplicand + 0], a
ldh [hMultiplicand], a

ldh a, [hMultiplicand - 1]
rla
Expand All @@ -75,13 +75,13 @@ _Multiply::
ldh [hProduct + 1], a

ldh a, [hMathBuffer + 1]
ldh [hProduct + 0], a
ldh [hProduct], a

ret

_Divide::
xor a
ldh [hMathBuffer + 0], a
ldh [hMathBuffer], a
ldh [hMathBuffer + 1], a
ldh [hMathBuffer + 2], a
ldh [hMathBuffer + 3], a
Expand All @@ -91,19 +91,19 @@ _Divide::
ld e, a

.loop
ldh a, [hMathBuffer + 0]
ldh a, [hMathBuffer]
ld c, a
ldh a, [hDividend + 1]
sub c
ld d, a

ldh a, [hDivisor]
ld c, a
ldh a, [hDividend + 0]
ldh a, [hDividend]
sbc c
jr c, .next

ldh [hDividend + 0], a
ldh [hDividend], a

ld a, d
ldh [hDividend + 1], a
Expand Down Expand Up @@ -139,13 +139,13 @@ _Divide::
jr nz, .next2

ld e, 8
ldh a, [hMathBuffer + 0]
ldh a, [hMathBuffer]
ldh [hDivisor], a
xor a
ldh [hMathBuffer + 0], a
ldh [hMathBuffer], a

ldh a, [hDividend + 1]
ldh [hDividend + 0], a
ldh [hDividend], a

ldh a, [hDividend + 2]
ldh [hDividend + 1], a
Expand All @@ -164,9 +164,9 @@ _Divide::
srl a
ldh [hDivisor], a

ldh a, [hMathBuffer + 0]
rr a
ldh [hMathBuffer + 0], a
ldh a, [hMathBuffer]
rra
ldh [hMathBuffer], a

jr .loop

Expand All @@ -184,6 +184,6 @@ _Divide::
ldh [hQuotient + 1], a

ldh a, [hMathBuffer + 1]
ldh [hQuotient + 0], a
ldh [hQuotient], a

ret
12 changes: 11 additions & 1 deletion home/array.asm
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ SkipNames::

AddNTimes::
; Add bc * a to hl.
; Preserves bc
and a
ret z

push bc
.loop
rra ; and a from below and above resets carry
jr nc, .noadd
add hl, bc
dec a
.noadd
sla c
rl b
and a
jr nz, .loop
.done
pop bc
ret

0 comments on commit eae158a

Please sign in to comment.