Compare commits
5 Commits
c89d8e9850
...
a993cab542
Author | SHA1 | Date |
---|---|---|
Juhani Krekelä | a993cab542 | |
Juhani Krekelä | 7a4c6295f3 | |
Juhani Krekelä | 95c29fc2e0 | |
Juhani Krekelä | 109cdf5a3a | |
Juhani Krekelä | cbfbbbcf54 |
262
calculator.asm
262
calculator.asm
|
@ -1,21 +1,143 @@
|
||||||
org 0
|
org 0
|
||||||
|
|
||||||
start:
|
start:
|
||||||
load r0, lit01
|
cleq r0, r0, stReadline
|
||||||
load r1, litff
|
cleq r0, r0, dumpstack
|
||||||
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
|
|
||||||
|
|
||||||
halt
|
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
|
; Stack-based functions
|
||||||
; ==================================================================
|
; ==================================================================
|
||||||
|
@ -42,6 +164,18 @@ stSub:
|
||||||
cleq r0, r0, subWord
|
cleq r0, r0, subWord
|
||||||
breq r0, r0, pushWord
|
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 --
|
; a b --
|
||||||
; if a >= b then r0:r1 = 0001
|
; if a >= b then r0:r1 = 0001
|
||||||
; else r0:r1 = 0000
|
; else r0:r1 = 0000
|
||||||
|
@ -62,7 +196,7 @@ stGtEq:
|
||||||
|
|
||||||
; n -- n
|
; n -- n
|
||||||
stPrinthex:
|
stPrinthex:
|
||||||
cleq r0, r0, peekWord
|
cleq r0, r0, popWord
|
||||||
cleq r0, r0, writehexWord
|
cleq r0, r0, writehexWord
|
||||||
breq r0, r0, newline
|
breq r0, r0, newline
|
||||||
|
|
||||||
|
@ -78,6 +212,51 @@ stEmit:
|
||||||
store ffff, r1
|
store ffff, r1
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
; ------------------------------------------------------------------
|
||||||
|
; Stack manipulation
|
||||||
|
; ------------------------------------------------------------------
|
||||||
|
|
||||||
|
; a - a a
|
||||||
|
stDup:
|
||||||
|
cleq r0, r0, peekWord
|
||||||
|
breq r0, r0, pushWord
|
||||||
|
|
||||||
|
; a b -- a b a
|
||||||
|
stOver:
|
||||||
|
cleq r0, r0, popWord
|
||||||
|
cleq r0, r0, tmpStoreWord01
|
||||||
|
cleq r0, r0, peekWord
|
||||||
|
cleq r0, r0, tmp2StoreWord01
|
||||||
|
cleq r0, r0, tmpLoadWord01
|
||||||
|
cleq r0, r0, pushWord
|
||||||
|
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
|
; Low-level functions
|
||||||
; ==================================================================
|
; ==================================================================
|
||||||
|
@ -88,16 +267,8 @@ stEmit:
|
||||||
|
|
||||||
tmpWordHigh: data 0
|
tmpWordHigh: data 0
|
||||||
tmpWordLow: data 0
|
tmpWordLow: data 0
|
||||||
|
tmpWord2High: data 0
|
||||||
; out:
|
tmpWord2Low: data 0
|
||||||
; r0:r1 = word
|
|
||||||
; clobbers:
|
|
||||||
; r2, r3
|
|
||||||
peekWord:
|
|
||||||
cleq r0, r0, popWord
|
|
||||||
cleq r0, r0, tmpStoreWord01
|
|
||||||
cleq r0, r0, pushWord
|
|
||||||
breq r0, r0, tmpLoadWord01
|
|
||||||
|
|
||||||
; in:
|
; in:
|
||||||
; r0:r1 = word
|
; r0:r1 = word
|
||||||
|
@ -120,6 +291,36 @@ tmpLoadWord23:
|
||||||
load r3, tmpWordLow
|
load r3, tmpWordLow
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
; in:
|
||||||
|
; r0:r1 = word
|
||||||
|
tmp2StoreWord01:
|
||||||
|
store tmpWord2High, r0
|
||||||
|
store tmpWord2Low, r1
|
||||||
|
ret
|
||||||
|
|
||||||
|
; out:
|
||||||
|
; r0:r1 = word
|
||||||
|
tmp2LoadWord01:
|
||||||
|
load r0, tmpWord2High
|
||||||
|
load r1, tmpWord2Low
|
||||||
|
ret
|
||||||
|
|
||||||
|
; out:
|
||||||
|
; r0:r1 = word
|
||||||
|
; clobbers:
|
||||||
|
; r2, r2
|
||||||
|
peekWord:
|
||||||
|
cleq r0, r0, popWord
|
||||||
|
store peekWordHigh, r0
|
||||||
|
store peekWordLow, r1
|
||||||
|
cleq r0, r0, pushWord
|
||||||
|
load r0, peekWordHigh
|
||||||
|
load r1, peekWordLow
|
||||||
|
ret
|
||||||
|
|
||||||
|
peekWordHigh: data 0
|
||||||
|
peekWordLow: data 0
|
||||||
|
|
||||||
; in:
|
; in:
|
||||||
; r0:r1 = word
|
; r0:r1 = word
|
||||||
; clobbers:
|
; clobbers:
|
||||||
|
@ -685,6 +886,9 @@ lit0d: data 0d
|
||||||
lit0e: data 0e
|
lit0e: data 0e
|
||||||
lit0f: data 0f
|
lit0f: data 0f
|
||||||
|
|
||||||
|
litcharSpace:
|
||||||
|
lit20: data 20
|
||||||
|
|
||||||
litchar0:
|
litchar0:
|
||||||
lit30: data 30
|
lit30: data 30
|
||||||
litchar1:
|
litchar1:
|
||||||
|
@ -741,8 +945,14 @@ lit72: data 72
|
||||||
litff: data ff
|
litff: data ff
|
||||||
|
|
||||||
; ==================================================================
|
; ==================================================================
|
||||||
; Stack
|
; Data
|
||||||
; ==================================================================
|
; ==================================================================
|
||||||
|
|
||||||
SP: addr stackBase
|
linebufStart: addr end
|
||||||
stackBase:
|
linebufSize:
|
||||||
|
data 00
|
||||||
|
data 80
|
||||||
|
SP: addr end + 80
|
||||||
|
SPStart: addr end + 80
|
||||||
|
|
||||||
|
end:
|
||||||
|
|
Loading…
Reference in New Issue