From 9474cb786427bf2a5987efac13c4e3d73d774f6e Mon Sep 17 00:00:00 2001 From: CrazyEttin <> Date: Wed, 3 Aug 2022 20:04:50 +0300 Subject: [PATCH] Add looping and ability to correct input to the adder --- examples/adder.asm | 76 ++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/examples/adder.asm b/examples/adder.asm index 05aac70..2bacdc8 100644 --- a/examples/adder.asm +++ b/examples/adder.asm @@ -1,23 +1,12 @@ ;Two-digit hexadecimal adder ;Print a prompt - load r0, prompt +start: load r0, prompt store ffff, r0 - ;Read and convert the first argument and load it to r2 - ;First digit - load r0, ffff - cleq r0, r0, hex2n - xor r2, r2 - xor r2, r0 - shl r2 - shl r2 - shl r2 - shl r2 - ;Second digit - load r0, ffff - cleq r0, r0, hex2n - or r2, r0 + ;Read and convert the first argument and store it to arg1 + cleq r0, r0, rarg + store arg1, r0 ;Print a newline cleq r0, r0, newln @@ -26,20 +15,13 @@ load r0, plus store ffff, r0 - ;Read and convert the second argument and load it to r0 - ;First digit - load r0, ffff - cleq r0, r0, hex2n - xor r3, r3 - xor r3, r0 - shl r3 - shl r3 - shl r3 - shl r3 - ;Second digit - load r0, ffff - cleq r0, r0, hex2n - or r0, r3 + ;Read and convert the second argument and load it to r2 + cleq r0, r0, rarg + xor r2, r2 + xor r2, r0 + + ;Reload the first argument to r0 + load r0, arg1 ;Print a newline cleq r0, r0, newln @@ -74,8 +56,38 @@ ;Print a newline cleq r0, r0, newln - ;Halt + ;Loop + breq r0, r0, start + + ;Read and convert an argument and load it to r0 + ;First digit +rarg: load r0, ffff + cleq r0, r0, hex2n + xor r2, r2 + xor r2, r0 + shl r2 + shl r2 + shl r2 + shl r2 + ;Second digit +rarg2: load r0, ffff + load r1, backsp + breq r0, r1, rarg + cleq r0, r0, hex2n + ;Confirm + load r3, ffff + load r1, backsp + breq r3, r1, rarg2 + load r1, cr + breq r3, r1, rarg3 + load r0, qmark + store ffff, r0 + cleq r0, r0, newln halt + ;Combine the digits +rarg3: or r0, r2 + ;Return + ret ;Print a newline newln: load r1, cr @@ -265,6 +277,7 @@ prompt: data 3e plus: data 2b equals: data 3d qmark: data 3f +backsp: data 8 cr: data d lf: data a @@ -319,3 +332,6 @@ tbl28: data e tbl29: data 65 tbl2a: data f tbl2b: data 66 + + ;First argument +arg1: data 0