From 244293b8ff05eb96a96756f462110868c169e268 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 19 Oct 2024 02:07:40 +0200 Subject: [PATCH] Create multiplication routine --- src/MATHTST.ASM | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/MATHTST.ASM b/src/MATHTST.ASM index b6bedc8..89a6b15 100644 --- a/src/MATHTST.ASM +++ b/src/MATHTST.ASM @@ -23,36 +23,41 @@ PRLOOP: MOV A, M ; read next char INX H ; point to the next char JMP PRLOOP ; do the next char PREND: RET ; return from subroutine -; MUL: Multiplicates two 8 bit numbers by using right shifts -; The multiplicator must be in C and the multiplicand in D -MUL: MVI B, 0 ; initialize the most significant byte - ; of the result - MVI E, 9 ; set the bit counter (is bits + 1) -MULT0: MOV A, C ; rotate the least significant bit of - RAR ; multiplier to carry and shift - MOV C, A ; low order byte of result - DCR E ; decrease the bit counter - JZ MULEND ; exit, if all bits are done - MOV A, B ; load the most significant bit - JNC MULT1 ; continue with rotation, when carry not set - ADD D ; Add multiplicant to high-order byte of result, - ; if the bit was 1 -MULT1: RAR ; shift hight-order byte of result - MOV B, A ; load the high-order byte back to B - JMP MULT0 ; jump to beginning of loop -MULEND: RET ; return from subroutine +; MUL: Multiply two values using the shift and add method. +; Expects two 8 bit values on the stack +MUL: POP D ; Get the operand from the stack. + MVI D, 00H ; Make sure D is 0 + POP B ; Get the multiplier + MOV A, C ; load the multiplier into A + MVI C, 08H ; initialize counter with 08H + LXI H, 0000H ; Clear HL pair +LOOP: RRC ; Rotate the acc content to right + JNC SKIP ; If carry flag is 0, jump to SKIP + DAD D ; Add DE with HL +SKIP: XCHG ; Exchange DE and HL + DAD H ; Add HL with itself + XCHG ; exchange DE with HL + DCR C ; decrease C register + JNZ LOOP ; repeat, if C is not 0 + PUSH H ; Push the result to the stack + RET ; return from subroutine START: LXI H, 0 ; blank out HL DAD SP ; HL = SP SHLD Stack ; save the original stack LXI H, STOP ; set HL to the new stack top SPHL ; set SP to point to our new stack 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 - 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. JNZ PROGEND ; if the result is not met, fail. LXI H, MsgStr ; load the message CALL PRINTF ; print the results + LHLD Stack ; load the original stack addr + SPHL ; restore the original stack PROGEND: RET END