Added a division subroutine

This commit is contained in:
2024-09-15 16:02:32 +02:00
parent ac0c32d189
commit 44411cda89

View File

@@ -6,6 +6,7 @@
MsgStr: DB 'Hello, world.',13,10,0 MsgStr: DB 'Hello, world.',13,10,0
ClsStr: DB 27,'[2J',0 ClsStr: DB 27,'[2J',0
HmeStr: DB 27,'[H',0 HmeStr: DB 27,'[H',0
CurPos: DS 11
Stack: DW 0 ; place to save the old stack Stack: DW 0 ; place to save the old stack
SBOT: DS 32 ; reserve a local stack SBOT: DS 32 ; reserve a local stack
; Constants ; Constants
@@ -34,6 +35,37 @@ CLS: LXI H, ClsStr ; Load the escape sequence
HOME: LXI H, HmeStr ; Load the escape sequence HOME: LXI H, HmeStr ; Load the escape sequence
CALL PRINTF ; print the sequence CALL PRINTF ; print the sequence
RET RET
; DIV: Divides a 16 bit number by an 8bit number using rotation
; The dividend is stored in BC and the divisor in D
DIV: MVI E,9 ; bit counter
MOV A,B ; move the MSB to the accumulator
DIV0: MOV B,A ; store the current MSB in B
MOV A,C ; rotate carry into C register
; rotate next most significant bit
; to carry.
MOV C,A
DCR E ; decrease E
JZ DIV2 ; if E==0, cleanup
MOV A,B ; rotate most significat but to
RAL ; high order quotient
JNC DIV1 ; if no carry, check, if divisior is valid
SUB D ; substract divisor
JMP DIV0 ; and loop
DIV1: SUB D ; substract divisor. if less than high-order
JNC DIV0 ; quotient, loop
ADD D ; otherwise, add it back
JMP DIV0 ; and then loop
DIV2: RAL ; rotate left
MOV E,A ; store A to E
MVI A,0FFH ; complement the quotient
XRA C ;
MOV C,A ; store the complemtn in C
MOV A,E ;
RAR ; rotate the A register.
RET ; return to the caller
; SETCUR: Places the cursor anywhere on the screen.
; Coordinates are stored in a memory location that
; is referred to by HL
; Start of the main code block ; Start of the main code block
START: LXI H, 0 ; blank out HL START: LXI H, 0 ; blank out HL
DAD SP ; HL = SP DAD SP ; HL = SP