This code appears in the following versions (click to see it in the source code):
Code variations between these versions are shown below.
Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate (P R) = 256 * A / Q Deep dive: Shift-and-subtract division
Calculate the following division and remainder: P = A / Q R = remainder as a fraction of Q, where 1.0 = 255 Another way of saying the above is this: (P R) = 256 * A / Q This uses the same shift-and-subtract algorithm as TIS2, but this time we keep the remainder.
Returns: C flag The C flag is cleared
LDX #8 \ Set a counter in X to count the 8 bits in A ASL A \ Shift A left and store in P (we will build the result STA P \ in P) LDA #0 \ Set A = 0 for us to build a remainder
ROL A \ Shift A to the left
SBC Q \ A >= Q, so set A = A - Q
There are two differences in the DVID4 routine between the original versions and the advanced versions that look like they would affect the result of the division; I haven't yet worked out what this is all about.
See below for more variations related to this code.
This variation is blank in the 6502 Second Processor and Master versions.
SEC \ Set the C flag, so that P gets a 1 shifted into bit 0
ROL P \ Shift P to the left, pulling the C flag into bit 0 DEX \ Decrement the loop counter
Code variation 8 of 8
See variation 5 above for details.
Tap on a block to expand it, and tap it again to revert.