diff --git a/examples/adder.asm b/examples/adder.asm index 2bacdc8..264c0a8 100644 --- a/examples/adder.asm +++ b/examples/adder.asm @@ -3,6 +3,8 @@ ;Print a prompt start: load r0, prompt store ffff, r0 + load r0, space + store ffff, r0 ;Read and convert the first argument and store it to arg1 cleq r0, r0, rarg @@ -14,6 +16,8 @@ start: load r0, prompt ;Print a plus sign load r0, plus store ffff, r0 + load r0, space + store ffff, r0 ;Read and convert the second argument and load it to r2 cleq r0, r0, rarg @@ -26,16 +30,21 @@ start: load r0, prompt ;Print a newline cleq r0, r0, newln + ;Print an equals sign + load r3, equals + store ffff, r3 + ;Add the arguments and load the result to r3 cleq r0, r0, adder xor r3, r3 xor r3, r0 - ;Print an equals sign - load r0, equals - store ffff, r0 - ;Convert and print the result + ;Overflow + xor r0, r0 + xor r0, r1 + cleq r0, r0, n2hex + store ffff, r0 ;Restore the result back to r0 xor r0, r0 xor r0, r3 @@ -90,10 +99,10 @@ rarg3: or r0, r2 ret ;Print a newline -newln: load r1, cr - store ffff, r1 - load r1, lf - store ffff, r1 +newln: load r3, cr + store ffff, r3 + load r3, lf + store ffff, r3 ret ;Get the value of a hexadecimal digit @@ -183,21 +192,32 @@ valf: load r0, tbl1e ;Return h2nend: ret - ;Add r2 to r0 + ;Add r2 to r0 with the overflow stored in r1 + ;Reset overflow +adder: xor r3, r3 + store ovrflw, r3 ;Copy the first argument to r1 -adder: xor r1, r1 +addlop: xor r1, r1 xor r1, r0 - ;Calculate the sum and carry + ;Calculate the sum and carry and copy the pre-shift carry to r1 xor r0, r2 and r2, r1 + xor r1, r1 + xor r1, r2 shl r2 + ;Check for overflow + rol r1 + breq r1, r2, nvrflw + ;Store overflow + load r1, one + store ovrflw, r1 ;Check for no carry - xor r3, r3 - breq r2, r3, addend +nvrflw: breq r2, r3, addend ;Loop - breq r0, r0, adder - ;Return -addend: ret + breq r0, r0, addlop + ;Load overflow and return +addend: load r1, ovrflw + ret ;Get the hexadecimal digit of a nibble ;Extract the low nibble @@ -273,6 +293,7 @@ dgtf: load r0, tbl1f n2hend: ret ;Characters +space: data 20 prompt: data 3e plus: data 2b equals: data 3d @@ -284,6 +305,9 @@ lf: data a ;Mask mask: data f + ;One +one: data 1 + ;Hexadecimal table ;Numeric digits tbl00: data 0 @@ -334,4 +358,5 @@ tbl2a: data f tbl2b: data 66 ;First argument +ovrflw: data 0 arg1: data 0