BBC Micro Elite

# Maths (Arithmetic): MLTU2 (6502SP version)

```       Name: MLTU2                                             [View in context]
Type: Subroutine
Category: Maths (Arithmetic)
Summary: Calculate (A P+1 P) = (A ~P) * Q

Do the following multiplication of an unsigned 16-bit number and an unsigned
8-bit number:

(A P+1 P) = (A ~P) * Q

where ~P means P EOR %11111111 (i.e. P with all its bits flipped). In other
words, if you wanted to calculate &1234 * &56, you would:

* Set A to &12
* Set P to &34 EOR %11111111 = &CB
* Set Q to &56

before calling MLTU2.

This routine is like a mash-up of MU11 and FMLTU. It uses part of FMLTU's
inverted argument trick to work out whether or not to do an addition, and like
MU11 it sets up a counter in X to extract bits from (P+1 P). But this time we
extract 16 bits from (P+1 P), so the result is a 24-bit number. The core of
the algorithm is still the shift-and-add approach explained in MULT1, just
with more bits.

Returns:

Q                    Q is preserved

Other entry points:

MLTU2-2              Set Q to X, so this calculates (A P+1 P) = (A ~P) * X

STX Q                  \ Store X in Q

.MLTU2

EOR #%11111111         \ Flip the bits in A and rotate right, storing the
LSR A                  \ result in P+1, so we now calculate (P+1 P) * Q
STA P+1

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

LDX #16                \ Set up a counter in X to count the 16 bits in (P+1 P)

ROR P                  \ Set P = P >> 1 with bit 7 = bit 0 of A
\ and C flag = bit 0 of P

.MUL7

BCS MU21               \ If C (i.e. the next bit from P) is set, do not do the
\ to just do the shifts

\
\   A = A + Q + C
\     = A + Q

ROR A                  \ Rotate (A P+1 P) to the right, so we capture the next
ROR P+1                \ digit of the result in P+1, and extract the next digit
ROR P                  \ of (P+1 P) in the C flag

DEX                    \ Decrement the loop counter

BNE MUL7               \ Loop back for the next bit until P has been rotated
\ all the way

RTS                    \ Return from the subroutine

.MU21

LSR A                  \ Shift (A P+1 P) to the right, so we capture the next
ROR P+1                \ digit of the result in P+1, and extract the next digit
ROR P                  \ of (P+1 P) in the C flag

DEX                    \ Decrement the loop counter

BNE MUL7               \ Loop back for the next bit until P has been rotated
\ all the way

RTS                    \ Return from the subroutine
```
The text passage describes a subroutine in assembly language for performing a specific 16-bit by 8-bit multiplication operation. It includes detailed steps and explanations of the algorithm used.
no_question
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
BBC Micro Elite

# Maths (Arithmetic): MLTU2 (6502SP version)

```       Name: MLTU2                                             [View in context]
Type: Subroutine
Category: Maths (Arithmetic)
Summary: Calculate (A P+1 P) = (A ~P) * Q

Do the following multiplication of an unsigned 16-bit number and an unsigned
8-bit number:

(A P+1 P) = (A ~P) * Q

where ~P means P EOR %11111111 (i.e. P with all its bits flipped). In other
words, if you wanted to calculate &1234 * &56, you would:

* Set A to &12
* Set P to &34 EOR %11111111 = &CB
* Set Q to &56

before calling MLTU2.

This routine is like a mash-up of MU11 and FMLTU. It uses part of FMLTU's
inverted argument trick to work out whether or not to do an addition, and like
MU11 it sets up a counter in X to extract bits from (P+1 P). But this time we
extract 16 bits from (P+1 P), so the result is a 24-bit number. The core of
the algorithm is still the shift-and-add approach explained in MULT1, just
with more bits.

Returns:

Q                    Q is preserved

Other entry points:

MLTU2-2              Set Q to X, so this calculates (A P+1 P) = (A ~P) * X

STX Q                  \ Store X in Q

.MLTU2

EOR #%11111111         \ Flip the bits in A and rotate right, storing the
LSR A                  \ result in P+1, so we now calculate (P+1 P) * Q
STA P+1

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

LDX #16                \ Set up a counter in X to count the 16 bits in (P+1 P)

ROR P                  \ Set P = P >> 1 with bit 7 = bit 0 of A
\ and C flag = bit 0 of P

.MUL7

BCS MU21               \ If C (i.e. the next bit from P) is set, do not do the
\ to just do the shifts

\
\   A = A + Q + C
\     = A + Q

ROR A                  \ Rotate (A P+1 P) to the right, so we capture the next
ROR P+1                \ digit of the result in P+1, and extract the next digit
ROR P                  \ of (P+1 P) in the C flag

DEX                    \ Decrement the loop counter

BNE MUL7               \ Loop back for the next bit until P has been rotated
\ all the way

RTS                    \ Return from the subroutine

.MU21

LSR A                  \ Shift (A P+1 P) to the right, so we capture the next
ROR P+1                \ digit of the result in P+1, and extract the next digit
ROR P                  \ of (P+1 P) in the C flag

DEX                    \ Decrement the loop counter

BNE MUL7               \ Loop back for the next bit until P has been rotated
\ all the way

RTS                    \ Return from the subroutine
```
user
The text passage describes a subroutine in assembly language for performing a specific 16-bit by 8-bit multiplication operation. It includes detailed steps and explanations of the algorithm used.

TYPE: NO_QUESTION