Skip to navigation


Maths (Arithmetic): SPS2

[NES version, Bank 0]

Name: SPS2 [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate X = A / 16
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * SP2 calls SPS2

Calculate the following, where A is a sign-magnitude 8-bit integer and the result is a signed 8-bit integer: X = A / 16
Returns: C flag The C flag is cleared
.SPS2 TAY ; Copy the argument A to Y, so we can check its sign ; below AND #%01111111 ; Clear the sign bit of the argument A LSR A ; Set A = A / 16 LSR A LSR A LSR A ADC #0 ; Round the result up to the nearest integer by adding ; the bit we just shifted off the right (which went into ; the C flag) CPY #%10000000 ; If Y is positive (i.e. the original argument was BCC LL163 ; positive), jump to LL163 EOR #$FF ; Negate A using two's complement ADC #0 .LL163 TAX ; Copy the result in A to X RTS ; Return from the subroutine