From b4fce03177df15f200950591922298892f62a60e Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 3 Nov 2023 07:55:23 +0000 Subject: Made test.asm an example asm program --- examples/memory-print.asm | 64 +++++++++++++++++++++++++++++++++++++++++++++++ test.asm | 62 --------------------------------------------- 2 files changed, 64 insertions(+), 62 deletions(-) create mode 100644 examples/memory-print.asm delete mode 100644 test.asm diff --git a/examples/memory-print.asm b/examples/memory-print.asm new file mode 100644 index 0000000..ed32b7a --- /dev/null +++ b/examples/memory-print.asm @@ -0,0 +1,64 @@ +;;; memory-print: An example program that features a subroutine for +;;; printing a memory buffer, of any length, as characters. + ;; Allocate a buffer of 3 characters + malloc.byte 3 + mov.word 0 + ;; Setup the buffer to be equivalent to "abc" + push.reg.word 0 + push.byte 'a' + mset.byte 0 + push.reg.word 0 + push.byte 'b' + mset.byte 1 + push.reg.word 0 + push.byte 'c' + mset.byte 2 + + ;; Save buffer to W[8] because the first 8 registers should be + ;; reserved for library routines as it may be overwritten + push.reg.word 0 + mov.word 8 + + push.reg.word 0 + ;; Call the routine + call print_cptr + + ;; Delete allocated buffer + push.reg.word 8 + mdelete + + halt + +;;; print_cptr: Prints pointer to a buffer of characters. Pointer +;;; should be on the stack as a word. +print_cptr: + ;; Save pointer in layout to W[0], P -> W[0] + mov.word 0 + ;; iterator I -> W[1] + push.word 0 + mov.word 1 + ;; (W[0])[W[1]] -> P[I] + push.reg.word 0 ; <-- # + push.reg.word 1 + mget.stack.byte + print.char + + ;; I += 1 + push.reg.word 1 + push.word 1 + plus.word + mov.word 1 + + ;; if I != |P| ... + push.reg.word 1 + push.reg.word 0 + msize + eq.word + not.byte + ;; then go to # + jump.if.byte *-13 + ;; else print a newline + push.byte '\n' + print.char + ;; return back to the caller + ret diff --git a/test.asm b/test.asm deleted file mode 100644 index 8131003..0000000 --- a/test.asm +++ /dev/null @@ -1,62 +0,0 @@ - ;; Allocate a buffer of 3 characters - malloc.byte 3 - mov.word 0 - ;; Setup the buffer to be equivalent to "abc" - push.reg.word 0 - push.byte 'a' - mset.byte 0 - push.reg.word 0 - push.byte 'b' - mset.byte 1 - push.reg.word 0 - push.byte 'c' - mset.byte 2 - - ;; Save buffer to W[8] because the first 8 registers should be - ;; reserved for library routines as it may be overwritten - push.reg.word 0 - mov.word 8 - - push.reg.word 0 - ;; Call the routine - call print_cptr - - ;; Delete allocated buffer - push.reg.word 8 - mdelete - - halt - -;;; print_cptr: Prints pointer to a buffer of characters. Pointer -;;; should be on the stack as a word. -print_cptr: - ;; Save pointer in layout to W[0], P -> W[0] - mov.word 0 - ;; iterator I -> W[1] - push.word 0 - mov.word 1 - ;; (W[0])[W[1]] -> P[I] - push.reg.word 0 ; <-- # - push.reg.word 1 - mget.stack.byte - print.char - - ;; I += 1 - push.reg.word 1 - push.word 1 - plus.word - mov.word 1 - - ;; if I != |P| ... - push.reg.word 1 - push.reg.word 0 - msize - eq.word - not.byte - ;; then go to # - jump.if.byte *-13 - ;; else print a newline - push.byte '\n' - print.char - ;; return back to the caller - ret -- cgit v1.2.3-13-gbd6f