Elite on the BBC Micro

# Maths (Arithmetic): FMLTU (Master version)

```       Name: FMLTU                                             [View in context]
Type: Subroutine                                       [Compare versions]
Category: Maths (Arithmetic)
Summary: Calculate A = A * Q / 256
Deep dive: Multiplication and division using logarithms

Do the following multiplication of two unsigned 8-bit numbers, returning only
the high byte of the result:

(A ?) = A * Q

or, to put it another way:

A = A * Q / 256

The Master and 6502 Second Processor versions use logarithms to speed up the
multiplication process. See the deep dive on "Multiplication using logarithms"
for more details.

.FMLTU

STX P                  \ Store X in P so we can preserve it through the call to
\ FMULTU

STA widget             \ Store A in widget, so now widget = argument A

TAX                    \ Transfer A into X, so now X = argument A

BEQ MU3                \ If A = 0, jump to MU3 to return a result of 0, as
\ 0 * Q / 256 is always 0

\ We now want to calculate La + Lq, first adding the low
\ bytes (from the logL table), and then the high bytes
\ (from the log table)

LDA logL,X             \ Set A = low byte of La
\       = low byte of La (as we set X to A above)

LDX Q                  \ Set X = Q

BEQ MU3again           \ If X = 0, jump to MU3again to return a result of 0, as
\ A * 0 / 256 is always 0

CLC                    \ Set A = A + low byte of Lq
ADC logL,X             \       = low byte of La + low byte of Lq

LDA log,X              \ Set A = high byte of Lq

LDX widget             \ Set A = A + C + high byte of La
ADC log,X              \       = high byte of Lq + high byte of La + C
\
\ so we now have:
\
\   A = high byte of (La + Lq)

BCC MU3again           \ If the addition fitted into one byte and didn't carry,
\ then La + Lq < 256, so we jump to MU3again to return a
\ result of 0

TAX                    \ Otherwise La + Lq >= 256, so we return the A-th entry
LDA antilog,X          \ from the antilog table

LDX P                  \ Restore X from P so it is preserved

RTS                    \ Return from the subroutine

.MU3again

LDA #0                 \ Set A = 0

.MU3

\ If we get here then A (our result) is already 0

LDX P                  \ Restore X from P so it is preserved

RTS                    \ Return from the subroutine
```