Skip to navigation

Elite on the BBC Micro and NES

Version analysis of LL38

This code appears in the following versions (click to see it in the source code):

Code variations between these versions are shown below.

Name: LL38 Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate (S A) = (S R) + (A Q)
Calculate the following between sign-magnitude numbers: (S A) = (S R) + (A Q) where the sign bytes only contain the sign bits, not magnitudes.
Returns: C flag Set if the addition overflowed, clear otherwise
.LL38 EOR S \ If the sign of A * S is negative, skip to LL35, as BMI LL39 \ A and S have different signs so we need to subtract LDA Q \ Otherwise set A = R + Q, which is the result we need, CLC \ as S already contains the correct sign ADC R RTS \ Return from the subroutine .LL39 LDA R \ Set A = R - Q SEC SBC Q BCC P%+4 \ If the subtraction underflowed, skip the next two \ instructions so we can negate the result CLC \ Otherwise the result is correct, and S contains the \ correct sign of the result as R is the dominant side \ of the subtraction, so clear the C flag RTS \ And return from the subroutine \ If we get here we need to negate both the result and \ the sign in S, as both are the wrong sign

Code variation 1 of 1A variation in the labels only

This variation is blank in the Cassette, Disc (flight), Disc (docked), 6502 Second Processor and Electron versions.

.LL40
 PHA                    \ Store the result of the subtraction on the stack

 LDA S                  \ Flip the sign of S
 EOR #%10000000
 STA S

 PLA                    \ Restore the subtraction result into A

 EOR #%11111111         \ Negate the result in A using two's complement, i.e.
 ADC #1                 \ set A = ~A + 1

 RTS                    \ Return from the subroutine