How to resolve the algorithm Multiplication tables step by step in the 8080 Assembly programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Multiplication tables step by step in the 8080 Assembly programming language
Table of Contents
Problem Statement
Produce a formatted 12×12 multiplication table of the kind memorized by rote when in primary (or elementary) school.
Only print the top half triangle of products.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Multiplication tables step by step in the 8080 Assembly programming language
Source code in the 8080 programming language
org 100h
lxi h,output
;;; Make the header
call skip ; Four spaces,
mvi m,'|' ; separator,
inx h
lxi d,0C01h ; 12 fields starting at 1
fnum: mov a,e ; Field number
call num
inr e
dcr d ; If not 12 yet, next field number
jnz fnum
call nl ; Newline
mvi a,'-' ; Four dashes,
mvi b,4
call bchr
mvi m,'+' ; Plus,
inx h
mvi b,12*4 ; and 12*4 more dashes
call bchr
call nl ; Newline
;;; Write the 12 lines
mvi d,1 ; Start at line 1,
line: mov a,d ; Add the line number
call num
mvi m,'|' ; separator
inx h
mvi e,1 ; Start at column 1
mvi c,0 ; Cumulative sum at C
field: mov a,c ; Add line number giving next column
add d
mov c,a
mov a,e ; If column >= line, we need to print
cmp d
mov a,c ; the current total
cc skip ; skip field if column >= line
cnc num ; print field if column < line
inr e ; next column
mov a,e
cpi 13 ; column 13?
jnz field ; If not, next field on line
call nl ; But if so, add newline
inr d ; next line
mov a,d
cpi 13 ; line 13?
jnz line ; If not, next line
mvi m,'$' ; Write a CP/M string terminator,
mvi c,9 ; And use CP/M to print the string
lxi d,output
jmp 5
;;; Add the character in A to the string at HL, B times
bchr: mov m,a
inx h
dcr b
jnz bchr
ret
;;; Add newline to string at HL
nl: mvi m,13 ; CR
inx h
mvi m,10 ; LF
inx h
ret
;;; Add four spaces to string at HL (skip field)
skip: mvi b,' '
mov m,b
inx h
mov m,b
inx h
mov m,b
inx h
mov m,b
inx h
ret
;;; Add 3-digit number in A to string at HL
num: mvi m,' ' ; Separator space
inx h
ana a ; Clear carry
mvi b,100 ; 100s digit
call dspc
mvi b,10 ; 10s digit
call dspc
mvi b,1 ; 1s digit
dspc: jc dgt ; If carry, we need a digit
cmp b ; >= digit?
jnc dgt ; If not, we need a digit
mvi m,' ' ; Otherwise, fill with space
inx h
cmc ; Return with carry off
ret
dgt: mvi m,'0'-1 ; Calculate digit
dloop: inr m ; Increment digit
sub b ; while B can be subtracted
jnc dloop
add b
inx h
ret
output: equ $
You may also check:How to resolve the algorithm Tree traversal step by step in the Coq programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Binary digits step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the Python programming language
You may also check:How to resolve the algorithm DNS query step by step in the D programming language