Skip to navigation

Revs on the BBC Micro

Maths (Arithmetic): Multiply8x8

Name: Multiply8x8 [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate (A T) = T * U
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: ADC U \ \ 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 ADC U ROR A ROR T BCC P%+5 \ Repeat the shift-and-add loop for bit 2 CLC ADC U ROR A ROR T BCC P%+5 \ Repeat the shift-and-add loop for bit 3 CLC ADC U ROR A ROR T BCC P%+5 \ Repeat the shift-and-add loop for bit 4 CLC ADC U ROR A ROR T BCC P%+5 \ Repeat the shift-and-add loop for bit 5 CLC ADC U ROR A ROR T BCC P%+5 \ Repeat the shift-and-add loop for bit 6 CLC ADC U ROR A ROR T BCC P%+5 \ Repeat the shift-and-add loop for bit 7 CLC ADC U ROR A ROR T RTS \ Return from the subroutine