2018-03-18 09:13:21 -04:00
|
|
|
# Example of dst bytecode assembly
|
2018-03-16 14:34:48 -04:00
|
|
|
|
|
|
|
# Fibonacci sequence, implemented with naive recursion.
|
2020-11-04 10:16:02 +01:00
|
|
|
(def fibasm
|
|
|
|
(asm
|
|
|
|
'{:arity 1
|
2020-11-04 10:18:31 +01:00
|
|
|
:bytecode @[(ltim 1 0 0x2) # $1 = $0 < 2
|
|
|
|
(jmpif 1 :done) # if ($1) goto :done
|
|
|
|
(lds 1) # $1 = self
|
2020-11-04 10:16:02 +01:00
|
|
|
(addim 0 0 -0x1) # $0 = $0 - 1
|
2020-11-04 10:18:31 +01:00
|
|
|
(push 0) # push($0), push argument for next function call
|
|
|
|
(call 2 1) # $2 = call($1)
|
2020-11-04 10:16:02 +01:00
|
|
|
(addim 0 0 -0x1) # $0 = $0 - 1
|
2020-11-04 10:18:31 +01:00
|
|
|
(push 0) # push($0)
|
|
|
|
(call 0 1) # $0 = call($1)
|
|
|
|
(add 0 0 2) # $0 = $0 + $2 (integers)
|
2020-11-04 10:16:02 +01:00
|
|
|
:done
|
2020-11-04 10:18:31 +01:00
|
|
|
(ret 0) # return $0
|
2020-11-04 10:16:02 +01:00
|
|
|
]}))
|
2019-01-07 14:47:47 -05:00
|
|
|
|
|
|
|
# Test it
|
|
|
|
|
|
|
|
(defn testn
|
|
|
|
[n]
|
|
|
|
(print "fibasm(" n ") = " (fibasm n)))
|
|
|
|
|
|
|
|
(for i 0 10 (testn i))
|