Compare commits
4 Commits
117a2e24c2
...
c89d8e9850
Author | SHA1 | Date |
---|---|---|
Juhani Krekelä | c89d8e9850 | |
Juhani Krekelä | cc933eeac1 | |
Juhani Krekelä | b63232169d | |
Juhani Krekelä | 34c3fa70e3 |
132
calculator.asm
132
calculator.asm
|
@ -1,8 +1,6 @@
|
|||
org 0
|
||||
|
||||
start:
|
||||
cleq r0, r0, initStack
|
||||
|
||||
load r0, lit01
|
||||
load r1, litff
|
||||
cleq r0, r0, pushWord
|
||||
|
@ -37,17 +35,27 @@ stAdd:
|
|||
|
||||
; a b -- a-b
|
||||
stSub:
|
||||
cleq r0, r0, stNeg
|
||||
breq r0, r0, stAdd
|
||||
|
||||
; n -- -n
|
||||
stNeg:
|
||||
cleq r0, r0, popWord
|
||||
nand r0, r0
|
||||
nand r1, r1
|
||||
cleq r0, r0, incWord
|
||||
cleq r0, r0, tmpStoreWord01
|
||||
cleq r0, r0, popWord
|
||||
cleq r0, r0, tmpLoadWord23
|
||||
cleq r0, r0, subWord
|
||||
breq r0, r0, pushWord
|
||||
|
||||
; a b --
|
||||
; if a >= b then r0:r1 = 0001
|
||||
; else r0:r1 = 0000
|
||||
stGtEq:
|
||||
cleq r0, r0, popWord
|
||||
cleq r0, r0, tmpStoreWord01
|
||||
cleq r0, r0, popWord
|
||||
cleq r0, r0, tmpLoadWord23
|
||||
cleq r0, r0, subWord
|
||||
xor r0, r0
|
||||
xor r1, r1
|
||||
or r1, r2
|
||||
ret
|
||||
|
||||
; ------------------------------------------------------------------
|
||||
; I/O
|
||||
; ------------------------------------------------------------------
|
||||
|
@ -83,6 +91,8 @@ tmpWordLow: data 0
|
|||
|
||||
; out:
|
||||
; r0:r1 = word
|
||||
; clobbers:
|
||||
; r2, r3
|
||||
peekWord:
|
||||
cleq r0, r0, popWord
|
||||
cleq r0, r0, tmpStoreWord01
|
||||
|
@ -112,7 +122,7 @@ tmpLoadWord23:
|
|||
|
||||
; in:
|
||||
; r0:r1 = word
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r0, r1, r2, r3
|
||||
pushWord:
|
||||
store pushWordLow, r1
|
||||
|
@ -124,7 +134,7 @@ pushWord:
|
|||
|
||||
; out:
|
||||
; r0:r1
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r2, r3
|
||||
popWord:
|
||||
cleq r0, r0, popByte
|
||||
|
@ -140,33 +150,33 @@ popWord:
|
|||
|
||||
; in:
|
||||
; r0 = byte
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r0, r1, r2, r3
|
||||
pushByte:
|
||||
load r2, SPHigh
|
||||
load r2, SP+0
|
||||
store storeByteHigh, r2
|
||||
load r3, SPLow
|
||||
load r3, SP+1
|
||||
store storeByteLow, r3
|
||||
cleq r0, r0, storeByte
|
||||
|
||||
load r0, SPHigh
|
||||
load r1, SPLow
|
||||
load r0, SP+0
|
||||
load r1, SP+1
|
||||
cleq r0, r0, incWord
|
||||
store SPHigh, r0
|
||||
store SPLow, r1
|
||||
store SP+0, r0
|
||||
store SP+1, r1
|
||||
|
||||
ret
|
||||
|
||||
; out:
|
||||
; r0 = byte
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r1, r2, r3
|
||||
popByte:
|
||||
load r0, SPHigh
|
||||
load r1, SPLow
|
||||
load r0, SP+0
|
||||
load r1, SP+1
|
||||
cleq r0, r0, decWord
|
||||
store SPHigh, r0
|
||||
store SPLow, r1
|
||||
store SP+0, r0
|
||||
store SP+1, r1
|
||||
|
||||
store loadByteHigh, r0
|
||||
store loadByteLow, r1
|
||||
|
@ -201,7 +211,7 @@ loadByte:
|
|||
; out:
|
||||
; r0:r1 = result
|
||||
; r2 = carryout
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r3
|
||||
addWord:
|
||||
store addWordFirstHigh, r0
|
||||
|
@ -230,9 +240,48 @@ addWord:
|
|||
addWordSecondHigh: data 0
|
||||
addWordResultLow: data 0
|
||||
|
||||
; in:
|
||||
; r0:r1 = minuend
|
||||
; r2:r3 = subtrahend
|
||||
; out:
|
||||
; r0:r1 = result
|
||||
; r2 = carryout
|
||||
; clobbers:
|
||||
; r3
|
||||
subWord:
|
||||
store subWordMinuendHigh, r0
|
||||
store subWordSubtrahendHigh, r2
|
||||
|
||||
; Subtract low bytes
|
||||
; a - b = a + (-b) = a + ~b + 1
|
||||
xor r0, r0
|
||||
or r0, r3
|
||||
nand r0, r0
|
||||
load r2, lit01
|
||||
cleq r0, r0, addByte3
|
||||
store subWordResultLow, r0
|
||||
|
||||
; Subtract high bytes with carry-in in r1
|
||||
load r0, subWordMinuendHigh
|
||||
load r2, subWordSubtrahendHigh
|
||||
nand r2, r2
|
||||
cleq r0, r0, addByte3
|
||||
|
||||
; Move carryout to r2
|
||||
xor r2, r2
|
||||
or r2, r1
|
||||
|
||||
load r1, subWordResultLow
|
||||
|
||||
ret
|
||||
|
||||
subWordMinuendHigh: data 0
|
||||
subWordSubtrahendHigh: data 0
|
||||
subWordResultLow: data 0
|
||||
|
||||
; in/out:
|
||||
; r0:r1 = word
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r2, r3
|
||||
incWord:
|
||||
store incWordHigh, r0
|
||||
|
@ -255,7 +304,7 @@ incWord:
|
|||
|
||||
; in/out:
|
||||
; r0:r1 = word
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r2, r3
|
||||
decWord:
|
||||
store decWordHigh, r0
|
||||
|
@ -284,7 +333,7 @@ decWord:
|
|||
; out:
|
||||
; r0 = result
|
||||
; r1 = carryout
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r2, r3
|
||||
addByte3:
|
||||
store addByte3ThirdAddend, r2
|
||||
|
@ -309,7 +358,7 @@ addByte3:
|
|||
; out:
|
||||
; r0 = result
|
||||
; r1 = carryout
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r2, r3
|
||||
addByte:
|
||||
; Initiliaze carryout to 0
|
||||
|
@ -356,7 +405,7 @@ addByte:
|
|||
; r0 = number in range [0, 0xf]
|
||||
; out:
|
||||
; r0 = ascii character corresponding to input
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r3
|
||||
nybble2hex:
|
||||
xor r3, r3
|
||||
|
@ -447,7 +496,7 @@ nybble2hex:
|
|||
; r0 = ascii hex digit
|
||||
; out:
|
||||
; r0 = corresponding nybble
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r3
|
||||
hex2nybble:
|
||||
load r3, litchar0
|
||||
|
@ -552,7 +601,7 @@ hex2nybble:
|
|||
|
||||
; in:
|
||||
; r1:r0 = word
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r0, r2, r3
|
||||
writehexWord:
|
||||
cleq r0, r0, writehexByte
|
||||
|
@ -564,7 +613,7 @@ writehexWord:
|
|||
|
||||
; in:
|
||||
; r0 = byte
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r0, r2, r3
|
||||
writehexByte:
|
||||
; Store copy of the byte (as r0 is modified by nybble2hex)
|
||||
|
@ -591,7 +640,7 @@ writehexByte:
|
|||
; Common output routines
|
||||
; ------------------------------------------------------------------
|
||||
|
||||
; trashes:
|
||||
; clobbers:
|
||||
; r3
|
||||
newline:
|
||||
load r3, litcharCR
|
||||
|
@ -695,18 +744,5 @@ litff: data ff
|
|||
; Stack
|
||||
; ==================================================================
|
||||
|
||||
SPHigh: data 0
|
||||
SPLow: data 0
|
||||
|
||||
; trashes:
|
||||
; r3
|
||||
initStack:
|
||||
load r3, initStackDummyLoad+1
|
||||
store SPHigh, r3
|
||||
load r3, initStackDummyLoad+2
|
||||
store SPLow, r3
|
||||
ret
|
||||
|
||||
initStackDummyLoad: load r0, stackBase
|
||||
|
||||
SP: addr stackBase
|
||||
stackBase:
|
||||
|
|
Loading…
Reference in New Issue