This repository has been archived on 2025-11-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ovm/examples/fib.asm
Aryadev Chavali 32ee84ef70 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.
2023-11-03 21:49:40 +00:00

88 lines
1.4 KiB
NASM

;;; fib.asm: A program that generates the fibonacci numbers up to a
;;; very large bound (~UINT64_MAX). Using the registers to store the
;;; pairs of fibonacci numbers, we ensure only a finite amount of
;;; memory is necessary for this program to function, unlike a pure
;;; stack version.
;; 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
push.word 1
mov.word 1
;; Print REG[0] and REG[1]
loopback:
call print_pair
;; REG[0] += REG[1]
push.reg.word 0
push.reg.word 1
plus.word
mov.word 0
;; REG[1] += REG[0]
push.reg.word 0
push.reg.word 1
plus.word
mov.word 1
push.word 12200160415121876738
push.reg.word 1
gte.word
;; 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