Version 3 of masm

Updated 2013-11-27 10:55:41 by RFox

Richard Suchenwirth 2013-11-26 - No matter how much you give to people, they will always want more.

Take me for example. Just yesterday I found out that Tcl 8.6 comes with an experimental tcl::unsupported::assemble command (see TAL (Tcl Assembly Language) for many details), and after playing with it for a while, I already wanted to change things.

For example: in TAL, you put a constant to the top of stack with

 push myconst

Fair enough. But to push the value of a local variable, you are supposed to use

 load myvar

Also simple, but somehow not in the simple spirit of Tcl...

 % set myvar 42
 42
 % asm {push $myvar}
 assembly code may not contain substitutions

But if everything is a string, can't I just do the substitutions in the string myself? After some experimentation, here comes..

the smallest macro-assembler in the world. First, just to reduce typing,

 interp alias {} asm    {} ::tcl::unsupported::assemble

Then, I thought macro definition and substitution should be in separate places. Makes two lines of code.

 set masm_subs {"push $" "load "}

 proc masm code {uplevel 1 [list asm [string map $::masm_subs $code]]}

#-- Testing... (equivalent to: proc lempty lst {expr {llength $lst == 0}} )

 proc lempty lst {masm {push $lst;listLength;push 0;eq}}
 puts [lempty {}]
 puts [lempty a] 

returns 1 resp. 0, as desired.

Now as I continue learning TAL, I may run into more useful substitutions.. and just lappend them to masm_subs ...


RFox - 2013-11-27 10:55:41

Given that you want assembly time substitution can't you just quote the assembly with "'s rather than {} and let the interpreter do the rest?