From 32ee84ef7012b05add6003be6730d8a0bb0ffe00 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 3 Nov 2023 21:49:40 +0000 Subject: Used more subroutines in fib.asm to make code clearer Looks way more high level but parses down to a very simple bytecode. However, because of lack of inline code processing, it relies on the call stack quite heavily. With inline labels this would be a much more compact bytecode. --- examples/fib.asm | 64 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/examples/fib.asm b/examples/fib.asm index 4d61d5c..85df000 100644 --- a/examples/fib.asm +++ b/examples/fib.asm @@ -7,6 +7,9 @@ ;; Setup entrypoint global main main: + ;; Setup iterator i + push.word 1 + mov.word 2 ;; Setup initial REG[0] = 1 and REG[1] = 1 push.word 1 mov.word 0 @@ -15,19 +18,7 @@ main: ;; Print REG[0] and REG[1] loopback: - push.byte '\t' - print.char - push.reg.word 0 - print.word - push.byte '\n' - print.char - - push.byte '\t' - print.char - push.reg.word 1 - print.word - push.byte '\n' - print.char + call print_pair ;; REG[0] += REG[1] push.reg.word 0 @@ -47,3 +38,50 @@ loopback: ;; Jump to `#` jump.if.byte loopback halt + +print_pair: + push.byte '\t' + print.char + call print_i + push.byte ':' + print.char + push.byte ' ' + print.char + call print_reg_0 + push.byte '\n' + print.char + call increment_i + push.byte '\t' + print.char + call print_i + push.byte ':' + print.char + push.byte ' ' + print.char + call print_reg_1 + push.byte '\n' + print.char + call increment_i + ret + +increment_i: + push.reg.word 2 + push.word 1 + plus.word + mov.word 2 + ret + +print_i: + push.reg.word 2 + print.word + ret + +print_reg_0: + push.reg.word 0 + print.word + ret + +print_reg_1: + push.reg.word 1 + print.word + ret -- cgit v1.2.3-13-gbd6f