Revs on the BBC Micro

# Maths (Arithmetic): Multiply8x16Signed

```       Name: Multiply8x16Signed                                      [Show more]
Type: Subroutine
Category: Maths (Arithmetic)
Summary: Multiply an 8-bit and a 16-bit number and apply a sign to the
result
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplySpinYaw calls Multiply8x16Signed
* ScaleTyreForces calls Multiply8x16Signed

This routine calculates:

(A T) = (A T) * abs(A) * Y / 256

where A is the last variable to be loaded before the subroutine call. So if
the call follows an LDA instruction, for example, the following is calculated
if A is positive:

(A T) = (A T) * Y / 256

and the following is calculated if A is negative:

(A T) = -(A T) * Y / 256

Arguments:

(A T)                A 16-bit signed number

Y                    An unsigned number

N flag               Controls the sign to be applied:

* N flag clear to calculate (A T) * Y / 256

* N flag set to calculate -(A T) * Y / 256

.Multiply8x16Signed

PHP                    \ Store the N flag on the stack

JSR Absolute16Bit      \ Set the sign of (A T) to that of the N flag argument

STA V                  \ Set (V T) = (A T)

STY U                  \ Set U = Y

JSR Multiply8x16       \ Set (U T) = U * (V T) / 256
\           = Y * (A T) / 256

LDA U                  \ Set (A T) = (U T)
\           = Y * (A T) / 256

PLP                    \ Retrieve the N flag from the stack

JSR Absolute16Bit      \ Set the sign of (A T) to that of the N flag argument

RTS                    \ Return from the subroutine
```