From 38080674f5c536d7b1f413c7f49b11c0a51e4bca Mon Sep 17 00:00:00 2001 From: CrazyEttin <> Date: Fri, 5 Aug 2022 18:49:17 +0300 Subject: [PATCH] Clarify the readme, add the echo example program, and prettyfy the ascii example program --- examples/ascii.asm | 10 ++- examples/echo.asm | 197 +++++++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 3 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 examples/echo.asm diff --git a/examples/ascii.asm b/examples/ascii.asm index 451eb15..425cbf8 100644 --- a/examples/ascii.asm +++ b/examples/ascii.asm @@ -3,14 +3,19 @@ ;Print a prompt load r0, prompt store ffff, r0 + load r0, space + store ffff, r0 ;Read a character to r0 and load it to r2 load r0, ffff xor r2, r2 xor r2, r0 - ;Print a newline + ;Print a newline and align cleq r0, r0, newln + load r1, space + store ffff, r1 + store ffff, r1 ;Get the hexadecimal digit of the high nibble ror r0 @@ -119,9 +124,10 @@ dgtf: load r0, tbl1f n2hend: ret ;Characters -prompt: data 3e cr: data d lf: data a +space: data 20 +prompt: data 3e ;Mask mask: data f diff --git a/examples/echo.asm b/examples/echo.asm new file mode 100644 index 0000000..5e51c1a --- /dev/null +++ b/examples/echo.asm @@ -0,0 +1,197 @@ + ;String echo + + ;Store the buffer start address in buffst + ;High byte + load r0, chstor + 1 + store buffst, r0 + ;Low byte + load r0, chstor + 2 + store buffst + 1, r0 + + ;Print a prompt +start: load r0, prompt + store ffff, r0 + load r0, space + store ffff, r0 + + ;Initialise the character counter + xor r0, r0 + + ;Read a character +inloop: load r1, ffff + + ;Check for an escape + load r2, esc + breq r1, r2, escbr + + ;Check for a return + load r2, cr + breq r1, r2, inend + + ;Check for end of the buffer + load r2, buffsz + brneq r0, r2, chstor + + ;Backtrack if end of the buffer + load r2, bs + store ffff, r2 + breq r0, r0, inloop + + ;Store the character in the buffer +chstor: store buffer, r1 + + ;Increment the character counter and load it in r3 + load r2, one + cleq r0, r0, sum + xor r3, r3 + xor r3, r0 + + ;Increment the buffer address + ;High byte + load r0, chstor + 1 + load r2, one + cleq r0, r0, sum + store chstor + 1, r0 + ;Low byte + load r0, chstor + 2 + load r2, one + cleq r0, r0, sum + store chstor + 2, r0 + ;Add the overflow to the high byte + load r0, chstor + 1 + xor r2, r2 + xor r2, r1 + cleq r0, r0, sum + store chstor + 1, r0 + + ;Reload the character counter to r0 + xor r0, r0 + xor r0, r3 + + ;Read the next character + breq r0, r0, inloop + + ;Print a backslash and a newline +escbr: load r0, space + store ffff, r0 + load r0, bslash + store ffff, r0 + load r0, cr + store ffff, r0 + load r0, lf + store ffff, r0 + + ;Restore the buffer address to its start + ;High byte + load r0, buffst + store chstor + 1, r0 + ;Low byte + load r0, buffst + 1 + store chstor + 2, r0 + + ;Start a new input line + breq r0, r0, start + + ;Store a string-terminating zero in the buffer +inend: xor r0, r0 + load r1, chstor + 1 + store endsto + 1, r1 + load r1, chstor + 2 + store endsto + 2, r1 +endsto: store 0000, r0 + + ;Print a line feed and align with the input + load r0, lf + store ffff, r0 + load r0, space + store ffff, r0 + store ffff, r0 + + ;Load a character from the buffer +outlop: load r1, buffer + + ;Check for string end + xor r2, r2 + breq r1, r2, outend + + ;Print the character + store ffff, r1 + + ;Increment the buffer address + ;High byte + load r0, outlop + 1 + load r2, one + cleq r0, r0, sum + store outlop + 1, r0 + ;Low byte + load r0, outlop + 2 + load r2, one + cleq r0, r0, sum + store outlop + 2, r0 + ;Add the overflow to the high byte + load r0, outlop + 1 + xor r2, r2 + xor r2, r1 + cleq r0, r0, sum + store outlop + 1, r0 + + ;Print the next character + breq r0, r0, outlop + + ;Print a newline +outend: load r0, cr + store ffff, r0 + load r0, lf + store ffff, r0 + + ;Halt + halt + + ;Add r2 to r0 with the overflow stored in r1 + ;Reset overflow +sum: xor r1, r1 + store ovrflw, r1 + ;Copy the first argument to r1 +sumlop: xor r1, r1 + xor r1, r0 + ;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 +nvrflw: xor r1, r1 + breq r1, r2, sumend + ;Loop + breq r0, r0, sumlop + ;Load overflow and return +sumend: load r1, ovrflw + ret + + ;Constants +one: data 1 + + ;Characters +bs: data 8 +lf: data a +cr: data d +esc: data 1b +space: data 20 +prompt: data 3e +bslash: data 5c + + ;Variables +ovrflw: data 0 + + ;Buffer +buffst: data 0 + data 0 +buffsz: data f +buffer: diff --git a/readme.md b/readme.md index de70d84..9c89f22 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ Registers and Memory * 24-bit instruction register IR * 16-bit instruction and return pointers IP and RP * 8-bit general-purpose registers R0-R3 - * 8-bit memory addresses 0-FFFF + * 8-bit memory locations 0-FFFF Multi-byte values are big-endian. Memory addresses FFF0-FFFF are reserved for memory mapped devices; the instruction and return pointers