Skip to navigation

Maths (Arithmetic): DivideBy16

Name: DivideBy16 [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Divide a 16-bit sign-magnitude number by 16
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * GetVectorForAngles calls DivideBy16

This routine divides a 16-bit sign-magnitude number by 16 and returns the result as a signed 16-bit number.
Arguments: Y Offset of the 16-bit sign-magnitude number to divide: * 0 = sinAngle * 1 = cosAngle
Returns: (A X) The 16-bit signed number containing the result
.DivideBy16 LDA sinAngleLo,Y \ Set (A T) to the 16-bit sign-magnitude number pointed STA T \ to by Y LDA sinAngleHi,Y LSR A \ Set (A T) = (A T) / 16 ROR T \ PHP \ We store bit 0 of the original 16-bit sign-magnitude LSR A \ number on the stack in the C flag (as it gets rotated ROR T \ out from bit 0 on the first ROR T) LSR A ROR T LSR A ROR T PLP \ We stored the sign bit from the original 16-bit \ sign-magnitude number on the stack, so fetch it into \ the C flag BCC divi1 \ If the sign bit was 0 then the original number was \ positive, so skip the following JSR Negate16Bit \ The original 16-bit sign-magnitude number was negative \ so call Negate16Bit to negate the result as follows: \ \ (A T) = -(A T) .divi1 LDX T \ Set (A X) = (A T) RTS \ Return from the subroutine