1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
; divide hMoney by hDivideBCDDivisor
; return output in hDivideBCDQuotient (same as hDivideBCDDivisor)
; used only to halve player money upon losing a fight
DivideBCDPredef::
DivideBCDPredef2::
DivideBCDPredef3:: ; only used function
DivideBCDPredef4::
call GetPredefRegisters
DivideBCD::
xor a
ldh [hDivideBCDBuffer], a
ldh [hDivideBCDBuffer+1], a
ldh [hDivideBCDBuffer+2], a
ld d, $1
.mulBy10Loop
; multiply the divisor by 10 until the leading digit is nonzero
; to set up the standard long division algorithm
ldh a, [hDivideBCDDivisor]
and $f0
jr nz, .next
inc d
ldh a, [hDivideBCDDivisor]
swap a
and $f0
ld b, a
ldh a, [hDivideBCDDivisor+1]
swap a
ldh [hDivideBCDDivisor+1], a
and $f
or b
ldh [hDivideBCDDivisor], a
ldh a, [hDivideBCDDivisor+1]
and $f0
ld b, a
ldh a, [hDivideBCDDivisor+2]
swap a
ldh [hDivideBCDDivisor+2], a
and $f
or b
ldh [hDivideBCDDivisor+1], a
ldh a, [hDivideBCDDivisor+2]
and $f0
ldh [hDivideBCDDivisor+2], a
jr .mulBy10Loop
.next
push de
push de
call DivideBCD_getNextDigit
pop de
ld a, b
swap a
and $f0
ldh [hDivideBCDBuffer], a
dec d
jr z, .next2
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
pop de
ldh a, [hDivideBCDBuffer]
or b
ldh [hDivideBCDBuffer], a
dec d
jr z, .next2
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
pop de
ld a, b
swap a
and $f0
ldh [hDivideBCDBuffer+1], a
dec d
jr z, .next2
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
pop de
ldh a, [hDivideBCDBuffer+1]
or b
ldh [hDivideBCDBuffer+1], a
dec d
jr z, .next2
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
pop de
ld a, b
swap a
and $f0
ldh [hDivideBCDBuffer+2], a
dec d
jr z, .next2
push de
call DivideBCD_divDivisorBy10
call DivideBCD_getNextDigit
pop de
ldh a, [hDivideBCDBuffer+2]
or b
ldh [hDivideBCDBuffer+2], a
.next2
ldh a, [hDivideBCDBuffer]
ldh [hDivideBCDQuotient], a ; the same memory location as hDivideBCDDivisor
ldh a, [hDivideBCDBuffer+1]
ldh [hDivideBCDQuotient+1], a
ldh a, [hDivideBCDBuffer+2]
ldh [hDivideBCDQuotient+2], a
pop de
ld a, $6
sub d
and a
ret z
.divResultBy10loop
push af
call DivideBCD_divDivisorBy10
pop af
dec a
jr nz, .divResultBy10loop
ret
DivideBCD_divDivisorBy10:
ldh a, [hDivideBCDDivisor+2]
swap a
and $f
ld b, a
ldh a, [hDivideBCDDivisor+1]
swap a
ldh [hDivideBCDDivisor+1], a
and $f0
or b
ldh [hDivideBCDDivisor+2], a
ldh a, [hDivideBCDDivisor+1]
and $f
ld b, a
ldh a, [hDivideBCDDivisor]
swap a
ldh [hDivideBCDDivisor], a
and $f0
or b
ldh [hDivideBCDDivisor+1], a
ldh a, [hDivideBCDDivisor]
and $f
ldh [hDivideBCDDivisor], a
ret
DivideBCD_getNextDigit:
ld bc, $3
.loop
ld de, hMoney ; the dividend
ld hl, hDivideBCDDivisor
push bc
call StringCmp
pop bc
ret c
inc b
ld de, hMoney + 2 ; since SubBCD works starting from the least significant digit
ld hl, hDivideBCDDivisor + 2
push bc
call SubBCD
pop bc
jr .loop
AddBCDPredef::
call GetPredefRegisters
AddBCD::
and a
ld b, c
.add
ld a, [de]
adc [hl]
daa
ld [de], a
dec de
dec hl
dec c
jr nz, .add
jr nc, .done
ld a, $99
inc de
.fill
ld [de], a
inc de
dec b
jr nz, .fill
.done
ret
SubBCDPredef::
call GetPredefRegisters
SubBCD::
and a
ld b, c
.sub
ld a, [de]
sbc [hl]
daa
ld [de], a
dec de
dec hl
dec c
jr nz, .sub
jr nc, .done
ld a, $00
inc de
.fill
ld [de], a
inc de
dec b
jr nz, .fill
scf
.done
ret
|