add a test program for multiplication

This commit is contained in:
2024-09-29 00:05:44 +02:00
parent 98bb294827
commit d6ca76fbc6

58
src/MATHTST.ASM Normal file
View File

@@ -0,0 +1,58 @@
; Test of different math functions
; Uses intel 8080 mnemonics
ORG 100H ; CP/M program start here
JMP START ; jump to the program entry point
; Variable storage space
MsgStr: DB 'Correct.',13,10,0
Stack: DW 0 ; place to store the old stack
SBOT: DS 32 ; reserve 32 bytes of stack
STOP: EQU $-1 ; points to the top of the stack
BDOS EQU 5 ; address for BDOS calls in CP/M
; Start of the code segment
; PRINTF: Prints a string on the screen using a BDOS function.
; The address of the string is expected to be in register HL.
PRINTF:
PRLOOP: MOV A, M ; read next char
ORA A ; check, if string has ended
JZ PREND ; if char == 0, then leave
MOV E, A ; load the char into E
MVI C, 2 ; call BDOS function id 2
PUSH H ; save the HL register
CALL BDOS ; call the BDOS function
POP H ; restore the HL register
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
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
CALL MUL ; multiply the two values
LXI A, 63H ; load the expected result into the accumulator
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
PROGEND: RET
END