Create multiplication routine

This commit is contained in:
2024-10-19 02:07:40 +02:00
parent d6ca76fbc6
commit 244293b8ff

View File

@@ -23,36 +23,41 @@ PRLOOP: MOV A, M ; read next char
INX H ; point to the next char INX H ; point to the next char
JMP PRLOOP ; do the next char JMP PRLOOP ; do the next char
PREND: RET ; return from subroutine PREND: RET ; return from subroutine
; MUL: Multiplicates two 8 bit numbers by using right shifts ; MUL: Multiply two values using the shift and add method.
; The multiplicator must be in C and the multiplicand in D ; Expects two 8 bit values on the stack
MUL: MVI B, 0 ; initialize the most significant byte MUL: POP D ; Get the operand from the stack.
; of the result MVI D, 00H ; Make sure D is 0
MVI E, 9 ; set the bit counter (is bits + 1) POP B ; Get the multiplier
MULT0: MOV A, C ; rotate the least significant bit of MOV A, C ; load the multiplier into A
RAR ; multiplier to carry and shift MVI C, 08H ; initialize counter with 08H
MOV C, A ; low order byte of result LXI H, 0000H ; Clear HL pair
DCR E ; decrease the bit counter LOOP: RRC ; Rotate the acc content to right
JZ MULEND ; exit, if all bits are done JNC SKIP ; If carry flag is 0, jump to SKIP
MOV A, B ; load the most significant bit DAD D ; Add DE with HL
JNC MULT1 ; continue with rotation, when carry not set SKIP: XCHG ; Exchange DE and HL
ADD D ; Add multiplicant to high-order byte of result, DAD H ; Add HL with itself
; if the bit was 1 XCHG ; exchange DE with HL
MULT1: RAR ; shift hight-order byte of result DCR C ; decrease C register
MOV B, A ; load the high-order byte back to B JNZ LOOP ; repeat, if C is not 0
JMP MULT0 ; jump to beginning of loop PUSH H ; Push the result to the stack
MULEND: RET ; return from subroutine RET ; return from subroutine
START: LXI H, 0 ; blank out HL START: LXI H, 0 ; blank out HL
DAD SP ; HL = SP DAD SP ; HL = SP
SHLD Stack ; save the original stack SHLD Stack ; save the original stack
LXI H, STOP ; set HL to the new stack top LXI H, STOP ; set HL to the new stack top
SPHL ; set SP to point to our new stack SPHL ; set SP to point to our new stack
LXI D, 0BH ; loads a value into the multiplicant LXI D, 0BH ; loads a value into the multiplicant
LXI C, 09H ; loads multiplier to C PUSH D ; push to stack
LXI D, 09H ; loads multiplier to DE
PUSH D ; push to stack
CALL MUL ; multiply the two values CALL MUL ; multiply the two values
LXI A, 63H ; load the expected result into the accumulator MVI A, 63H ; load the expected result into the accumulator
POP B ; Get the result to BC
CMP C ; compare the result. CMP C ; compare the result.
JNZ PROGEND ; if the result is not met, fail. JNZ PROGEND ; if the result is not met, fail.
LXI H, MsgStr ; load the message LXI H, MsgStr ; load the message
CALL PRINTF ; print the results CALL PRINTF ; print the results
LHLD Stack ; load the original stack addr
SPHL ; restore the original stack
PROGEND: RET PROGEND: RET
END END