aboutsummaryrefslogtreecommitdiff
path: root/examples/fib.asm
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-03 21:49:40 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-03 21:49:40 +0000
commit32ee84ef7012b05add6003be6730d8a0bb0ffe00 (patch)
tree724a9ee12be500ba77fb430e82f9ade68c1c3502 /examples/fib.asm
parentbc4c57b243eaad8ef054d6ab7de7602b76b22f0d (diff)
downloadovm-32ee84ef7012b05add6003be6730d8a0bb0ffe00.tar.gz
ovm-32ee84ef7012b05add6003be6730d8a0bb0ffe00.tar.bz2
ovm-32ee84ef7012b05add6003be6730d8a0bb0ffe00.zip
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.
Diffstat (limited to 'examples/fib.asm')
-rw-r--r--examples/fib.asm64
1 files 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