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.
|
2018-07-01 15:52:15 +00:00
|
|
|
(def fibasm (asm '{
|
2018-03-16 18:34:48 +00:00
|
|
|
arity 1
|
|
|
|
bytecode [
|
2018-07-01 15:52:15 +00:00
|
|
|
(ltim 1 0 0x2) # $1 = $0 < 2
|
2018-05-08 23:40:28 +00:00
|
|
|
(jmpif 1 :done) # if ($1) goto :done
|
2018-03-16 19:45:24 +00:00
|
|
|
(lds 1) # $1 = self
|
2018-03-16 18:34:48 +00:00
|
|
|
(addim 0 0 -0x1) # $0 = $0 - 1
|
2018-03-23 22:36:56 +00:00
|
|
|
(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)
|
2018-12-27 18:05:29 +00:00
|
|
|
(add 0 0 2) # $0 = $0 + $2 (integers)
|
2018-03-16 19:45:24 +00:00
|
|
|
:done
|
2018-03-16 18:34:48 +00:00
|
|
|
(ret 0) # return $0
|
|
|
|
]
|
|
|
|
}))
|
2019-01-07 19:47:47 +00:00
|
|
|
|
|
|
|
# Test it
|
|
|
|
|
|
|
|
(defn testn
|
|
|
|
[n]
|
|
|
|
(print "fibasm(" n ") = " (fibasm n)))
|
|
|
|
|
|
|
|
(for i 0 10 (testn i))
|