Aviator on the BBC Micro

# Maths: Multiply16x16Bit0

```       Name: Multiply16x16Bit0                                       [Show more]
Type: Subroutine
Category: Maths
Summary: Multiply two 16-bit numbers that have their signs in bit 0
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* SetMatrices calls Multiply16x16Bit0
* SetMatrixEntry calls Multiply16x16Bit0

This routine multiplies two 16-bit numbers, both of which have their signs in
bit 0 of the low byte. It calculates:

(H G) = (J I) * (S R) >> 16

The result in (H G) has the sign in bit 0 of the low byte.

Arguments:

(J I)                A signed 16-bit number, with the sign in bit 0 of I

(S R)                A signed 16-bit number, with the sign in bit 0 of R

Returns:

(H G)                The result of the multiplication, with the sign in bit 0
of G

K                    The sign of the result (in bit 0)

.Multiply16x16Bit0

LDA R                  \ Extract the sign of (J I) * (S R) from bit 0 of I and
EOR I                  \ bit 0 of R, and store the result in K
AND #%00000001
STA K

JSR Multiply16x16      \ Calculate (H A) = (S R) * (J I) >> 16
\
\ and set the C flag if we need to increment H

AND #%11111110         \ Set bit 0 of A to the sign we stored in K above, so
ORA K                  \ (H A) has the correct sign of the multiplication

STA G                  \ Set (H G) = (H A)
\           = (S R) * (J I) >> 16

BCC mbit1              \ Increment the top byte in H if required
INC H

.mbit1

RTS                    \ Return from the subroutine
```