Thingamajig/examples/ascii.asm

182 lines
4.0 KiB
NASM
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;ASCII code printer
;Print a prompt
load r0, prompt
store ffff, r0
load r0, space
store ffff, r0
;Read a character to r0 and load it to r2
load r0, ffff
xor r2, r2
xor r2, r0
;Print a newline and align
cleq r0, r0, newln
load r1, space
store ffff, r1
store ffff, r1
;Convert and print the high nibble
;Convert
ror r0
ror r0
ror r0
ror r0
cleq r0, r0, n2hex
;Print
store ffff, r0
;Re-load the character to r0
xor r0, r0
xor r0, r2
;Convert and print the low nibble
;Convert
cleq r0, r0, n2hex
;Print
store ffff, r0
;Print a newline
cleq r0, r0, newln
;Halt
halt
;***
;Print a newline
newln: load r1, cr
store ffff, r1
load r1, lf
store ffff, r1
ret
;***
;Get the hexadecimal digit of a nibble
;Extract the low nibble
n2hex: load r1, mask
and r0, r1
;Locate the nibble in the table
load r1, nbl0
breq r0, r1, dgt0
load r1, nbl1
breq r0, r1, dgt1
load r1, nbl2
breq r0, r1, dgt2
load r1, nbl3
breq r0, r1, dgt3
load r1, nbl4
breq r0, r1, dgt4
load r1, nbl5
breq r0, r1, dgt5
load r1, nbl6
breq r0, r1, dgt6
load r1, nbl7
breq r0, r1, dgt7
load r1, nbl8
breq r0, r1, dgt8
load r1, nbl9
breq r0, r1, dgt9
load r1, nbla
breq r0, r1, dgta
load r1, nblb
breq r0, r1, dgtb
load r1, nblc
breq r0, r1, dgtc
load r1, nbld
breq r0, r1, dgtd
load r1, nble
breq r0, r1, dgte
load r1, nblf
breq r0, r1, dgtf
;Load the hexadecimal digit of the nibble
dgt0: load r0, hex0
breq r0, r0, n2hend
dgt1: load r0, hex1
breq r0, r0, n2hend
dgt2: load r0, hex2
breq r0, r0, n2hend
dgt3: load r0, hex3
breq r0, r0, n2hend
dgt4: load r0, hex4
breq r0, r0, n2hend
dgt5: load r0, hex5
breq r0, r0, n2hend
dgt6: load r0, hex6
breq r0, r0, n2hend
dgt7: load r0, hex7
breq r0, r0, n2hend
dgt8: load r0, hex8
breq r0, r0, n2hend
dgt9: load r0, hex9
breq r0, r0, n2hend
dgta: load r0, hexa
breq r0, r0, n2hend
dgtb: load r0, hexb
breq r0, r0, n2hend
dgtc: load r0, hexc
breq r0, r0, n2hend
dgtd: load r0, hexd
breq r0, r0, n2hend
dgte: load r0, hexe
breq r0, r0, n2hend
dgtf: load r0, hexf
breq r0, r0, n2hend
;Return
n2hend: ret
;***
;Data
;Characters
cr: data d
lf: data a
space: data 20
prompt: data 3e
;Mask
mask: data f
;Nibble table
nbl0: data 0
nbl1: data 1
nbl2: data 2
nbl3: data 3
nbl4: data 4
nbl5: data 5
nbl6: data 6
nbl7: data 7
nbl8: data 8
nbl9: data 9
nbla: data a
nblb: data b
nblc: data c
nbld: data d
nble: data e
nblf: data f
;Hexadecimal table
hex0: data 30
hex1: data 31
hex2: data 32
hex3: data 33
hex4: data 34
hex5: data 35
hex6: data 36
hex7: data 37
hex8: data 38
hex9: data 39
hexa: data 41
hexb: data 42
hexc: data 43
hexd: data 44
hexe: data 45
hexf: data 46