Skip to main content
Fix code formatting by using backticks instead of (inline) code tags
Source Link

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

Input
DE  First number (M)
HL  Second number (N)
Output
HL  M*N
HL_HLxDE    30A9    PUSH BC         BC is saved.
            30AA    LD B,$10        It is to be a 16-bit multiplication.
            30AC    LD A,H          A holds the high byte.
            30AD    LD C,L          C holds the low byte.
            30AE    LD HL,$0000     Initialise the result to zero.
HL_LOOP     30B1    ADD HL,HL       Double the result.
            30B2    JR C,HL_END     Jump if overflow.
            30B4    RL C            Rotate bit 7 of C into the carry.
            30B6    RLA             Rotate the carry bit into bit 0 and bit 7 into the carry flag.
            30B7    JR NC,HL_AGAIN  Jump if the carry flag is reset.
            30B9    ADD HL,DE       Otherwise add DE in once.
            30BA    JR C,HL_END     Jump if overflow.
HL_AGAIN    30BC    DJNZ HL_LOOP    Repeat until 16 passes have been made.
HL_END      30BE    POP BC          Restore BC.
            30BF    RET             Finished.

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

Input
DE  First number (M)
HL  Second number (N)
Output
HL  M*N
HL_HLxDE    30A9    PUSH BC         BC is saved.
            30AA    LD B,$10        It is to be a 16-bit multiplication.
            30AC    LD A,H          A holds the high byte.
            30AD    LD C,L          C holds the low byte.
            30AE    LD HL,$0000     Initialise the result to zero.
HL_LOOP     30B1    ADD HL,HL       Double the result.
            30B2    JR C,HL_END     Jump if overflow.
            30B4    RL C            Rotate bit 7 of C into the carry.
            30B6    RLA             Rotate the carry bit into bit 0 and bit 7 into the carry flag.
            30B7    JR NC,HL_AGAIN  Jump if the carry flag is reset.
            30B9    ADD HL,DE       Otherwise add DE in once.
            30BA    JR C,HL_END     Jump if overflow.
HL_AGAIN    30BC    DJNZ HL_LOOP    Repeat until 16 passes have been made.
HL_END      30BE    POP BC          Restore BC.
            30BF    RET             Finished.
Tidied code a bit
Source Link
Mark Williams
  • 4.4k
  • 1
  • 31
  • 55
Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

Copied and formatted the relevant assembly code into the answer for ease of the reader.
Source Link
Mark Williams
  • 4.4k
  • 1
  • 31
  • 55

For Spectrum BASIC, the routine for Small Integers (16 bit) can be seen on page 179 of the Complete ZX Spectrum ROM Disassembly, where it loops over the sixteen bits of one operand, shifting them into the carry bit, adding successively doubling values to the result value each time the test passes, and testing for overflow if the result doesn't fit in a small integer.

https://skoolkid.github.io/rom/asm/30A9.html

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

For larger floating point numbers (which the result is promoted to if the multiplication result overflows a small integer, or if either operand is already floating point), the full five byte floating point multiplication routine follows on page 180.

For Spectrum BASIC, the routine for Small Integers (16 bit) can be seen on page 179 of the Complete ZX Spectrum ROM Disassembly, where it loops over the sixteen bits of one operand, shifting them into the carry bit, adding successively doubling values to the result value each time the test passes, and testing for overflow if the result doesn't fit in a small integer.

https://skoolkid.github.io/rom/asm/30A9.html

For larger floating point numbers (which the result is promoted to if the multiplication result overflows a small integer, or if either operand is already floating point), the full five byte floating point multiplication routine follows on page 180.

For Spectrum BASIC, the routine for Small Integers (16 bit) can be seen on page 179 of the Complete ZX Spectrum ROM Disassembly, where it loops over the sixteen bits of one operand, shifting them into the carry bit, adding successively doubling values to the result value each time the test passes, and testing for overflow if the result doesn't fit in a small integer.

Input DE First number (M) HL Second number (N) Output HL M*N HL_HLxDE 30A9 PUSH BC BC is saved. 30AA LD B,$10 It is to be a 16-bit multiplication. 30AC LD A,H A holds the high byte. 30AD LD C,L C holds the low byte. 30AE LD HL,$0000 Initialise the result to zero. HL_LOOP 30B1 ADD HL,HL Double the result. 30B2 JR C,HL_END Jump if overflow. 30B4 RL C Rotate bit 7 of C into the carry. 30B6 RLA Rotate the carry bit into bit 0 and bit 7 into the carry flag. 30B7 JR NC,HL_AGAIN Jump if the carry flag is reset. 30B9 ADD HL,DE Otherwise add DE in once. 30BA JR C,HL_END Jump if overflow. HL_AGAIN 30BC DJNZ HL_LOOP Repeat until 16 passes have been made. HL_END 30BE POP BC Restore BC. 30BF RET Finished.

For larger floating point numbers (which the result is promoted to if the multiplication result overflows a small integer, or if either operand is already floating point), the full five byte floating point multiplication routine follows on page 180.

added 48 characters in body
Source Link
user16540
  • 234
  • 2
  • 3
Loading
added 48 characters in body
Source Link
user16540
  • 234
  • 2
  • 3
Loading
Source Link
user16540
  • 234
  • 2
  • 3
Loading