Change the adder to a calculator capable of both adding and subtracting and move error messages to their own lines in it
This commit is contained in:
parent
674ba2df08
commit
394137950f
|
@ -1,4 +1,4 @@
|
||||||
;Two-digit hexadecimal adder
|
;Hexadecimal adder and subtractor for two-digit unsigned values
|
||||||
|
|
||||||
;Print a prompt
|
;Print a prompt
|
||||||
start: load r0, prompt
|
start: load r0, prompt
|
||||||
|
@ -13,9 +13,16 @@ start: load r0, prompt
|
||||||
;Print a newline
|
;Print a newline
|
||||||
cleq r0, r0, newln
|
cleq r0, r0, newln
|
||||||
|
|
||||||
;Print a plus sign
|
;Read the operation sign
|
||||||
load r0, plus
|
load r0, ffff
|
||||||
store ffff, r0
|
load r1, plus
|
||||||
|
breq r0, r1, storsg
|
||||||
|
load r1, minus
|
||||||
|
breq r0, r1, storsg
|
||||||
|
;Error and halt if the character is not a sign
|
||||||
|
breq r0, r0, error
|
||||||
|
storsg: store sign, r0
|
||||||
|
;Print a space
|
||||||
load r0, space
|
load r0, space
|
||||||
store ffff, r0
|
store ffff, r0
|
||||||
|
|
||||||
|
@ -34,19 +41,55 @@ start: load r0, prompt
|
||||||
load r3, equals
|
load r3, equals
|
||||||
store ffff, r3
|
store ffff, r3
|
||||||
|
|
||||||
;Add the arguments and load the result to r3
|
;Check the operation
|
||||||
|
load r3, sign
|
||||||
|
load r1, minus
|
||||||
|
breq r1, r3, brsub
|
||||||
|
|
||||||
|
;Add and load the result to r3
|
||||||
|
;Addition
|
||||||
cleq r0, r0, adder
|
cleq r0, r0, adder
|
||||||
|
;Load the result to r3
|
||||||
|
xor r3, r3
|
||||||
|
xor r3, r0
|
||||||
|
;Skip the subtraction code
|
||||||
|
breq r0, r0, nosign
|
||||||
|
|
||||||
|
;Subtract and load the result to r3
|
||||||
|
;Subtraction
|
||||||
|
brsub: cleq r0, r0, sub
|
||||||
|
;Load the result to r3
|
||||||
xor r3, r3
|
xor r3, r3
|
||||||
xor r3, r0
|
xor r3, r0
|
||||||
|
|
||||||
;Convert and print the result
|
;Print a minus sign and unnegate the result if negative
|
||||||
;Overflow
|
;Check the sign
|
||||||
xor r0, r0
|
xor r0, r0
|
||||||
|
breq r0, r1, nosign
|
||||||
|
;Print a minus sign
|
||||||
|
load r0, minus
|
||||||
|
store ffff, r0
|
||||||
|
;Un-negate the result
|
||||||
|
store arg1, r3
|
||||||
|
load r2, one
|
||||||
|
cleq r0, r0, sub
|
||||||
|
nand r0, r0
|
||||||
|
;Reload the result to r3
|
||||||
|
xor r3, r3
|
||||||
|
xor r3, r0
|
||||||
|
;Skip printing the first digit
|
||||||
|
breq r0, r0, skip
|
||||||
|
|
||||||
|
;Print the overflow for addition or zero for positive results of
|
||||||
|
;subtraction
|
||||||
|
nosign: xor r0, r0
|
||||||
xor r0, r1
|
xor r0, r1
|
||||||
cleq r0, r0, n2hex
|
cleq r0, r0, n2hex
|
||||||
store ffff, r0
|
store ffff, r0
|
||||||
|
|
||||||
|
;Convert and print the result
|
||||||
;Restore the result back to r0
|
;Restore the result back to r0
|
||||||
xor r0, r0
|
skip: xor r0, r0
|
||||||
xor r0, r3
|
xor r0, r3
|
||||||
;High nibble
|
;High nibble
|
||||||
shr r0
|
shr r0
|
||||||
|
@ -68,6 +111,13 @@ start: load r0, prompt
|
||||||
;Loop
|
;Loop
|
||||||
breq r0, r0, start
|
breq r0, r0, start
|
||||||
|
|
||||||
|
;Error
|
||||||
|
error: cleq r0, r0, newln
|
||||||
|
load r0, qmark
|
||||||
|
store ffff, r0
|
||||||
|
cleq r0, r0, newln
|
||||||
|
halt
|
||||||
|
|
||||||
;Read and convert an argument and load it to r0
|
;Read and convert an argument and load it to r0
|
||||||
;First digit
|
;First digit
|
||||||
rarg: load r0, ffff
|
rarg: load r0, ffff
|
||||||
|
@ -89,10 +139,7 @@ rarg2: load r0, ffff
|
||||||
breq r3, r1, rarg2
|
breq r3, r1, rarg2
|
||||||
load r1, cr
|
load r1, cr
|
||||||
breq r3, r1, rarg3
|
breq r3, r1, rarg3
|
||||||
load r0, qmark
|
breq r0, r0, error
|
||||||
store ffff, r0
|
|
||||||
cleq r0, r0, newln
|
|
||||||
halt
|
|
||||||
;Combine the digits
|
;Combine the digits
|
||||||
rarg3: or r0, r2
|
rarg3: or r0, r2
|
||||||
;Return
|
;Return
|
||||||
|
@ -151,11 +198,8 @@ hex2n: load r1, tbl01
|
||||||
breq r0, r1, vale
|
breq r0, r1, vale
|
||||||
load r1, tbl2b
|
load r1, tbl2b
|
||||||
breq r0, r1, valf
|
breq r0, r1, valf
|
||||||
;Print a question mark and halt if the character is not a digit
|
;Error and halt if the character is not a digit
|
||||||
load r0, qmark
|
breq r0, r0, error
|
||||||
store ffff, r0
|
|
||||||
cleq r0, r0, newln
|
|
||||||
halt
|
|
||||||
;Load the value of the hexadecimal digit
|
;Load the value of the hexadecimal digit
|
||||||
val0: load r0, tbl00
|
val0: load r0, tbl00
|
||||||
breq r0, r0, h2nend
|
breq r0, r0, h2nend
|
||||||
|
@ -219,6 +263,25 @@ nvrflw: breq r2, r3, addend
|
||||||
addend: load r1, ovrflw
|
addend: load r1, ovrflw
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
;Subtract r2 from arg1 and store the result in r0 with the sign
|
||||||
|
;bit stored in r1
|
||||||
|
;Negate the second argument
|
||||||
|
sub: load r0, one
|
||||||
|
nand r2, r2
|
||||||
|
cleq r0, r0, adder
|
||||||
|
;Load the first argument to r2
|
||||||
|
load r2, arg1
|
||||||
|
;Subtract
|
||||||
|
cleq r0, r0, adder
|
||||||
|
;Invert the sign bit
|
||||||
|
xor r3, r3
|
||||||
|
breq r3, r1, submin
|
||||||
|
xor r1, r1
|
||||||
|
breq r0, r0, subend
|
||||||
|
submin: load r1, one
|
||||||
|
;Return
|
||||||
|
subend: ret
|
||||||
|
|
||||||
;Get the hexadecimal digit of a nibble
|
;Get the hexadecimal digit of a nibble
|
||||||
;Extract the low nibble
|
;Extract the low nibble
|
||||||
n2hex: load r1, mask
|
n2hex: load r1, mask
|
||||||
|
@ -296,6 +359,7 @@ n2hend: ret
|
||||||
space: data 20
|
space: data 20
|
||||||
prompt: data 3e
|
prompt: data 3e
|
||||||
plus: data 2b
|
plus: data 2b
|
||||||
|
minus: data 2d
|
||||||
equals: data 3d
|
equals: data 3d
|
||||||
qmark: data 3f
|
qmark: data 3f
|
||||||
backsp: data 8
|
backsp: data 8
|
||||||
|
@ -357,6 +421,7 @@ tbl29: data 65
|
||||||
tbl2a: data f
|
tbl2a: data f
|
||||||
tbl2b: data 66
|
tbl2b: data 66
|
||||||
|
|
||||||
;First argument
|
;Variables
|
||||||
ovrflw: data 0
|
ovrflw: data 0
|
||||||
arg1: data 0
|
arg1: data 0
|
||||||
|
sign: data 0
|
Loading…
Reference in New Issue