Revs on the BBC Micro

Maths (Arithmetic): MultiplyBy1Point5

```       Name: MultiplyBy1Point5                                       [Show more]
Type: Subroutine
Category: Maths (Arithmetic)
Summary: Multiply a 16-bit signed number by 1.5
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplySpinYaw calls MultiplyBy1Point5
* ScaleTyreForces calls MultiplyBy1Point5

Calculate the following:

(A T) = (U T) * 1.5

Arguments:

(U T)                A 16-bit signed number

.MultiplyBy1Point5

LDA U                  \ Set A = U, the high byte of (U T)

CLC                    \ Clear the C flag, to use when A is positive

BPL mulh1              \ If A is positive, jump to mulh1 to skip the following
\ instruction

SEC                    \ Set the C flag, to use when A is negative

.mulh1

\ By this point, the C flag contains the sign bit of A
\ (set if negative, clear if positive)

ROR A                  \ Shift A to the right, inserting the C flag into bit 7
\ to retain the correct sign

PHA                    \ Store the result on the stack, so the stack contains
\ the top byte of (U T), shifted right by one place

LDA T                  \ Set A = T, the low byte of (U T)

ROR A                  \ Shift A to the right, so the stack and A contain the
\ high and low bytes of (U T) >> 1, so if S is the value
\ on top of the stack, we have:
\
\   (S A) = (U T) >> 1

CLC                    \ Set (A T) = (S A) + (U T)
ADC T                  \           = (U T) >> 1 + (U T)
STA T                  \           = (U T) * 1.5
\
\ starting with the low bytes

PLA                    \ And then the high bytes