Fib in TAL

Fibonacci numbers in TAL ,by kbk ,2012-08-21

at paste.tclers.tk

namespace path ::tcl::mathop

interp recursionlimit {} 120000

proc fib {n} {
    ::tcl::unsupported::assemble {
        load n                        ;# n
        dup                        ;# n n
        push 1                        ;# n n 1
        gt                        ;# n n>1
        jumpFalse done                ;# n

        push 1                        ;# n 1
        sub                        ;# n-1
        push fib                ;# n-1 fib
        dup                        ;# n-1 fib fib
        over 2                        ;# n-1 fib fib n-1
        invokeStk 2                ;# n-1 fib fib(n-1)

        reverse 3                ;# fib(n-1) fib n-1
        push 1                        ;# fib(n-1) fib n-1 1
        sub                        ;# fib(n-1) fib n-2
        invokeStk 2                ;# fib(n-1) fib(n-2)

        add                        ;# fib(n)

      label done                ;# result
    }
}
puts [time {set result [fib 34]}]
puts "fib(34) = $result"