Implement line reading in calculator.asm

This commit is contained in:
Juhani Krekelä 2022-08-17 04:07:11 +03:00
parent 7a4c6295f3
commit a993cab542
1 changed files with 183 additions and 15 deletions

View File

@ -1,21 +1,143 @@
org 0
start:
load r0, lit01
load r1, litff
cleq r0, r0, pushWord
cleq r0, r0, stPrinthex
xor r0, r0
load r1, lit41
cleq r0, r0, pushWord
cleq r0, r0, stPrinthex
cleq r0, r0, stSub
cleq r0, r0, stPrinthex
cleq r0, r0, stReadline
cleq r0, r0, dumpstack
halt
dumpstack:
load r0, SPStart+0
load r1, SPStart+1
store dumpstackPtr+0, r0
store dumpstackPtr+1, r1
dumpstackLoop:
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
load r2, SP+0
load r3, SP+1
brneq r0, r2, dumpstackDumpword
breq r1, r3, dumpstackEnd
dumpstackDumpword:
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
store loadByteHigh, r0
store loadByteLow, r1
cleq r0, r0, loadByte
cleq r0, r0, writehexByte
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
cleq r0, r0, incWord
store dumpstackPtr+0, r0
store dumpstackPtr+1, r1
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
store loadByteHigh, r0
store loadByteLow, r1
cleq r0, r0, loadByte
cleq r0, r0, writehexByte
load r0, dumpstackPtr+0
load r1, dumpstackPtr+1
cleq r0, r0, incWord
store dumpstackPtr+0, r0
store dumpstackPtr+1, r1
load r0, litcharSpace
store ffff, r0
breq r0, r0, dumpstackLoop
dumpstackEnd:
breq r0, r0, newline
dumpstackPtr: addr dumpstackPtr
stReadline:
; Current index starts at 0
xor r0, r0
xor r1, r1
cleq r0, r0, pushWord
readlineLoop:
; Read a byte of input
cleq r0, r0, stKey
; Is it enter?
cleq r0, r0, peekWord
load r0, litcharCR
breq r0, r1, readlineEnter
; Is it backspace?
load r0, litcharBS
breq r0, r1, readlineBackspace
; Is the buffer full?
cleq r0, r0, stOver
load r0, linebufSize+0
load r1, linebufSize+1
cleq r0, r0, pushWord
cleq r0, r0, stGtEq
brneq r0, r1, readlineBufFull
; Calculate address
cleq r0, r0, stOver
load r0, linebufStart+0
load r1, linebufStart+1
cleq r0, r0, pushWord
cleq r0, r0, stAdd
; Store the read byte
cleq r0, r0, stStoreByte
; Increase index
cleq r0, r0, stInc
breq r0, r0, readlineLoop
readlineEnter:
; Remove the CR byte off the stack, as we only want to return the length
cleq r0, r0, popWord
breq r0, r0, newline
readlineBackspace:
; Remove the BS byte off the stack
cleq r0, r0, popWord
; Are we at the beginning of the line?
cleq r0, r0, stDup
xor r0, r0
xor r1, r1
cleq r0, r0, pushWord
cleq r0, r0, stSwap
cleq r0, r0, stGtEq
brneq r0, r1, readlineLoop
; Decrease the index and erase the echoed character
cleq r0, r0, stDec
load r3, litcharSpace
store ffff, r3
load r3, litcharBS
store ffff, r3
breq r0, r0, readlineLoop
readlineBufFull:
; Drop the input byte and erase the echoed character
cleq r0, r0, popWord
load r3, litcharBS
store ffff, r3
load r3, litcharSpace
store ffff, r3
load r3, litcharBS
store ffff, r3
breq r0, r0, readlineLoop
; ==================================================================
; Stack-based functions
; ==================================================================
@ -42,6 +164,18 @@ stSub:
cleq r0, r0, subWord
breq r0, r0, pushWord
; a -- a+1
stInc:
cleq r0, r0, popWord
cleq r0, r0, incWord
breq r0, r0, pushWord
; a -- a-1
stDec:
cleq r0, r0, popWord
cleq r0, r0, decWord
breq r0, r0, pushWord
; a b --
; if a >= b then r0:r1 = 0001
; else r0:r1 = 0000
@ -98,6 +232,31 @@ stOver:
cleq r0, r0, tmp2LoadWord01
breq r0, r0, pushWord
; a b -- b a
stSwap:
cleq r0, r0, popWord
cleq r0, r0, tmpStoreWord01
cleq r0, r0, popWord
cleq r0, r0, tmp2StoreWord01
cleq r0, r0, tmpLoadWord01
cleq r0, r0, pushWord
cleq r0, r0, tmp2LoadWord01
breq r0, r0, pushWord
; ------------------------------------------------------------------
; Memory
; ------------------------------------------------------------------
; a p --
stStoreByte:
cleq r0, r0, popWord
store storeByteHigh, r0
store storeByteLow, r1
cleq r0, r0, popWord
xor r0, r0
or r0, r1
breq r0, r0, storeByte
; ==================================================================
; Low-level functions
; ==================================================================
@ -727,6 +886,9 @@ lit0d: data 0d
lit0e: data 0e
lit0f: data 0f
litcharSpace:
lit20: data 20
litchar0:
lit30: data 30
litchar1:
@ -783,8 +945,14 @@ lit72: data 72
litff: data ff
; ==================================================================
; Stack
; Data
; ==================================================================
SP: addr stackBase
stackBase:
linebufStart: addr end
linebufSize:
data 00
data 80
SP: addr end + 80
SPStart: addr end + 80
end: