Updated 2018-04-04 12:57:56 by APN (Redirected from tcl::unsupported::assemble)
tcl::unsupported::assemble talScript

Assembles the TAL in talScript into bytecode, and then executes it.

See also edit

Examples edit

Ulam's 3n+1 problem
proc ulam {n} {
    assemble {
        load n;         # max
        dup;            # max n
        jump start;     # max n
    
        label loop;     # max n
        over 1;         # max n max
        over 1;         # max n max n
        ge;             # max n max>=n
        jumpTrue skip;  # max n

        reverse 2;      # n max
        pop;            # n
        dup;            # n n
    
        label skip;     # max n
        dup;            # max n n
        push 2;         # max n n 2
        mod;            # max n n%2
        jumpTrue odd;   # max n
    
        push 2;         # max n 2
        div;            # max n/2=n'
        jump start;     # max n
     
        label odd;      # max n
        push 3;         # max n 3
        mult;           # max 3*n
        push 1;         # max 3*n 1
        add;            # max 3*n+1=n'
            
        label start;    # max n
        dup;            # max n n
        push 1;         # max n n 1
        neq;            # max n n>1
        jumpTrue loop;  # max n
            
        pop;            # max
    }
}

Fibonacci numbers
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
  }
}