RosettaCodeData/Task/Display-a-linear-combination/8080-Assembly/display-a-linear-combination.8080
2026-04-30 12:34:36 -04:00

122 lines
2.5 KiB
Text

org 100h
lxi h,tests
test: mov b,m
inx h
dcr b
inr b
rz
lxi d,buf
call lincmb
push b
push h
mvi c,9
lxi d,buf
call 5
mvi c,9
lxi d,nl
call 5
pop h
pop b
jmp test
tests: db 3, 1, 2, 3
db 4, 0, 1, 2, 3
db 4, 1, 0, 3, 4
db 3, 1, 2, 0
db 3, 0, 0, 0
db 1, 0
db 3, 1, 1, 1
db 3, -1, -1, -1
db 4, -1, -2, 0, -3
db 1, -1
db 0
nl: db 13,10,'$'
; Write string representation of linear combination
; at [HL], of length B, to memory at [DE].
lincmb: push d
lxi d,1
lcstep: mov a,m
inx h
ora a
jz lcnxt
xthl
call lcsgn
ana a
jp lcchk
dcr a
cma
lcchk: cpi 1
jz lctrm
push b
call wrnum
pop b
mvi m,'*'
inx h
lctrm: mvi m,'e'
inx h
mvi m,'('
inx h
mov a,e
push b
call wrnum
pop b
mvi m,')'
inx h
inr d
xthl
lcnxt: inr e
dcr b
jnz lcstep
dcr d
inr d
pop d
xchg
jnz lcdone
mvi m,'0'
inx h
lcdone: mvi m,'$'
xchg
ret
lcsgn: dcr d
inr d
mvi c,'-'
jz lcsgnf
ana a
jm lcsgnw
mvi c,'+'
lcsgnw: mvi m,' '
inx h
mov m,c
inx h
mvi m,' '
inx h
ret
lcsgnf: ana a
rp
mov m,c
inx h
ret
; Write string representation of signed integer in A at [HL]
; Returns with HL pointing after the number
wrnum: ana a
jp wrdgt
mvi m,'-'
inx h
dcr a
cma
wrdgt: mvi b,-1
dgtlp: inr b
sui 10
jnc dgtlp
adi 58
mov c,a
mov a,b
ana a
push b
cnz wrdgt
pop b
mov m,c
inx h
ret
buf equ $