Revs on the BBC Micro

# Maths (Arithmetic): Multiply8x8

```       Name: Multiply8x8                                             [Show more]
Type: Subroutine
Category: Maths (Arithmetic)
Summary: Calculate (A T) = T * U
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplyElevation (Part 5 of 5) calls Multiply8x8
* ApplyEngine calls Multiply8x8
* ApplyGrassOrTrack calls Multiply8x8
* ApplyWingBalance calls Multiply8x8
* AwardRacePoints calls Multiply8x8
* BuildCarObjects (Part 1 of 3) calls Multiply8x8
* BuildCarObjects (Part 2 of 3) calls Multiply8x8
* GetCarInSegment calls Multiply8x8
* GetRotationMatrix (Part 2 of 5) calls Multiply8x8
* GetSectionSteering calls Multiply8x8
* GetTyreForces calls Multiply8x8
* Multiply16x16 calls Multiply8x8
* Multiply8x16 calls Multiply8x8
* MultiplyHeight calls Multiply8x8
* ProcessContact calls Multiply8x8
* ProcessDrivingKeys (Part 1 of 6) calls Multiply8x8
* ScaleWingSettings calls Multiply8x8
* Multiply8x16 calls via Multiply8x8+2

Do the following multiplication of two unsigned 8-bit numbers:

(A T) = A * U

Returns:

X                    X is unchanged

Other entry points:

Multiply8x8+2        Calculate (A T) = T * U

.Multiply8x8

STA T                  \ Set T = A

\ We now calculate (A T) = T * U
\                        = A * U

LDA #0                 \ Set A = 0 so we can start building the answer in A

LSR T                  \ Set T = T >> 1
\ and C flag = bit 0 of T

\ We are now going to work our way through the bits of
\ T, and do a shift-add for any bits that are set,
\ keeping the running total in A, and instead of using a
\ loop, we unroll the calculation, starting with bit 0

BCC P%+5               \ If C (i.e. the next bit from T) is set, do the
CLC                    \ addition for this bit of T:
\   A = A + U

ROR A                  \ Shift A right to catch the next digit of our result,
\ which the next ROR sticks into the left end of T while
\ also extracting the next bit of T

ROR T                  \ Add the overspill from shifting A to the right onto
\ the start of T, and shift T right to fetch the next
\ bit for the calculation into the C flag

BCC P%+5               \ Repeat the shift-and-add loop for bit 1
CLC
ROR A
ROR T

BCC P%+5               \ Repeat the shift-and-add loop for bit 2
CLC
ROR A
ROR T

BCC P%+5               \ Repeat the shift-and-add loop for bit 3
CLC
ROR A
ROR T

BCC P%+5               \ Repeat the shift-and-add loop for bit 4
CLC
ROR A
ROR T

BCC P%+5               \ Repeat the shift-and-add loop for bit 5
CLC
ROR A
ROR T

BCC P%+5               \ Repeat the shift-and-add loop for bit 6
CLC