\ We now calculate the slope for the second \ side of the triangle, from (x1, y1) to \ (x3, y3), as follows: \ \ R7 = (y1 - y3) / (x3 - x1) SUBS R9, R1, R5 \ Set R9 = R1 - R5 \ = y1 - y3 SUBS R14, R4, R0 \ Set R14 = R4 - R0 \ = x3 - x1 RSBMI R14, R4, R0 \ If R14 is negative, set R4 = x1 - x3, so \ we have the following: \ \ R14 = |x3 - x1| CMP R14, #64 \ If either R14 >= 64 or R9 >= 64, then at CMPLO R9, #64 \ least one of these is true: BHS trin7 \ \ R14 = |x3 - x1| >= 64 \ \ R9 = (y1 - y3) >= 64 \ \ so jump to trin7 to calculate the slope \ using the shift-and-subtract algorithm LDR R10, divisionTableAddr \ Set R10 to the address of the division \ tables ADD R10, R10, R14, LSL #8 \ Set R10 to the address of the division \ table containing n / R14, for n = 0 to 63 \ \ This works because each division table \ contains 64 words, or 256 bytes, so the \ address of table n / d is: \ \ divisionTable + d * 256 LDR R7, [R10, R9, LSL #2] \ Set R7 to the R9-th word in the division \ table containing n / R14, so this \ calculates the following: \ \ R7 = R9 / R14 \ \ = (y1 - y3) / |x3 - x1| B trin9 \ Jump to trin9 to skip the following and \ keep going .trin7 \ If we get here then at least one of these \ is true: \ \ R14 = |x3 - x1| >= 64 \ \ R9 = (y1 - y3) >= 64 \ \ We now calculate R2 = R9 / R14 using the \ shift-and-subtract division algorithm MOV R9, R9, LSL #16 \ First we scale R9 up as far as we can, to \ make the result as accurate as possible MOV R7, #0 \ Set R7 = 0 to contain the result MOV R10, #&80000000 \ Set bit 31 of R10 so we can shift it to \ the right in each iteration, using it as a \ counter .trin8 MOVS R14, R14, LSL #1 \ Shift R14 left, moving the top bit into \ the C flag CMPCC R14, R9 \ If we shifted a 0 out of the top of R14, \ test for a possible subtraction SUBCS R14, R14, R9 \ If we shifted a 1 out of the top of R14 or ORRCS R7, R7, R10 \ R14 >= R9, then do the subtraction: \ \ R14 = R14 - R9 \ \ and set the relevant bit in the result \ (i.e. apply the set bit in R10 to the \ result in R7) MOVS R10, R10, LSR #1 \ Shift R10 to the right, moving bit 0 into \ the C flag BCC trin8 \ Loop back until we shift the 1 out of the \ right end of R10 (after 32 shifts) \ So we now have the following result: \ \ R7 = R9 / R14 \ \ = (y1 - y3) / |x3 - x1| .trin9 CMP R4, R0 \ If R4 - R0 < 0 then: RSBMI R7, R7, #0 \ \ x3 - x1 < 0 \ \ so negate R7 to give R7 the correct sign \ for the following calculation: \ \ R7 = (y1 - y3) / (x3 - x1) \ \ So R7 contains the slope of the second \ side of the triangle, from (x1, y1) to \ (x3, y3)Name: DrawTriangle (Part 3 of 11) [Show more] Type: Subroutine Category: Drawing triangles Summary: Calculate the slope of (x1, y1) to (x3, y3) Deep dive: Drawing trianglesContext: See this subroutine in context in the source code References: No direct references to this subroutine in this source file
[X]
Variable divisionTableAddr (category: Maths (Arithmetic))
The address of the division lookup table
[X]
Label trin7 is local to this routine
[X]
Label trin8 is local to this routine
[X]
Label trin9 is local to this routine