|
191 | 191 | dex |
192 | 192 | bne !- |
193 | 193 | } |
| 194 | + |
| 195 | +/* |
| 196 | + Divides the two-byte number dividend by the two-byte number divisor, leaving the quotient in |
| 197 | + dividend and the remainder in remainder. Addressing mode of 16-bit numbers uses little endian. |
| 198 | + dividend - 2 bytes (quotient also 2 bytes) |
| 199 | + divisor - 2 bytes |
| 200 | + remainder - 2 bytes (must be as wide as divisor) |
| 201 | + |
| 202 | +*/ |
| 203 | +.macro div16By16(dividend, divisor, remainder) { |
| 204 | + lda #0 |
| 205 | + sta remainder // Initialize remainder to 0. |
| 206 | + sta remainder+1 |
| 207 | + ldx #16 // There are 16 bits in the dividend |
| 208 | + |
| 209 | + loop1: |
| 210 | + /* Shift the hi bit of dividend into remainder */ |
| 211 | + asl dividend |
| 212 | + rol dividend+1 |
| 213 | + rol remainder |
| 214 | + rol remainder+1 |
| 215 | + |
| 216 | + /* Trial subtraction */ |
| 217 | + lda remainder |
| 218 | + sec |
| 219 | + sbc divisor |
| 220 | + tay |
| 221 | + lda remainder+1 |
| 222 | + sbc divisor+1 |
| 223 | + |
| 224 | + /* Check subtraction */ |
| 225 | + bcc loop2 // Did subtraction succeed? |
| 226 | + sta remainder+1 // If yes, save it, else loop2 |
| 227 | + sty remainder |
| 228 | + inc dividend // and record a 1 in the quotient |
| 229 | + |
| 230 | +loop2: |
| 231 | + dex |
| 232 | + bne loop1 |
| 233 | + |
| 234 | +} |
| 235 | + |
| 236 | +/* |
| 237 | + Divides the two-byte number dividend by the one-byte number divisor, leaving the quotient in |
| 238 | + dividend and the remainder in remainder. Addressing mode of 16-bit numbers uses little endian. |
| 239 | + dividend - 2 bytes (quotient also 2 bytes) |
| 240 | + divisor - 1 byte |
| 241 | + remainder - 1 byte (must be as wide as divisor) |
| 242 | +*/ |
| 243 | +.macro div16By8(dividend, divisor, remainder) { |
| 244 | + lda #0 |
| 245 | + sta remainder // Initialize remainder to 0. |
| 246 | + ldx #16 // There are 16 bits in the dividend |
| 247 | + loop1: |
| 248 | + /* Shift the hi bit of dividend into remainder */ |
| 249 | + asl dividend |
| 250 | + rol dividend+1 |
| 251 | + rol remainder |
| 252 | + |
| 253 | + /* Trial subtraction */ |
| 254 | + lda remainder |
| 255 | + sec |
| 256 | + sbc divisor |
| 257 | + |
| 258 | + /* Check subtraction */ |
| 259 | + bcc loop2 // Did subtraction succeed? |
| 260 | + sta remainder // If yes, save it, else loop2 |
| 261 | + inc dividend // and record a 1 in the quotient |
| 262 | + |
| 263 | +loop2: |
| 264 | + dex |
| 265 | + bne loop1 |
| 266 | + |
| 267 | +} |
0 commit comments