1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 22:24:49 +00:00
janet/examples/assembly.dst

22 lines
657 B
Plaintext
Raw Normal View History

2018-03-18 13:13:21 +00:00
# Example of dst bytecode assembly
2018-03-16 18:34:48 +00:00
# Fibonacci sequence, implemented with naive recursion.
(def fibasm (asm '{
arity 1
bytecode [
(ldi 1 0x2) # $1 = 2
2018-03-16 18:34:48 +00:00
(lt 1 0 1) # $1 = $0 < $1
2018-03-30 16:17:03 +00:00
(jmpif 1 :done) # if ($1) goto :done
(lds 1) # $1 = self
2018-03-16 18:34:48 +00:00
(addim 0 0 -0x1) # $0 = $0 - 1
(push 0) # push($0), push argument for next function call
2018-03-16 18:34:48 +00:00
(call 2 1) # $2 = call($1)
(addim 0 0 -0x1) # $0 = $0 - 1
(push 0) # push($0)
(call 0 1) # $0 = call($1)
(addi 0 0 2) # $0 = $0 + $2 (integers)
:done
2018-03-16 18:34:48 +00:00
(ret 0) # return $0
]
}))