Go to the first, previous, next, last section, table of contents.


Assembling Local Variables

Another major class of programming language functionality which cannot easily be expressed simply as loading a constant or calling a function is local variables: What C afficionados call "automatic" variables.

The Muq assembler supplies the following functions in support of local variables:

Let's assemble code for : square -> x x x * ; as an example:

stack:
"square" --> *fun*$s.name
stack:
*asm* reset
stack:
"x" *asm* assembleVariableSlot --> *x*
stack:
*x* *asm* assembleVariableSet
stack:
*x* *asm* assembleVariableGet
stack:
*x* *asm* assembleVariableGet
stack:
'* *asm* assembleCall
stack:
nil -1 *fun* *asm* finishAssembly --> *cfn*
stack:
*cfn* 'square setSymbolFunction
stack:
2 square
stack: 4

This time, a peek at the disassembly shows:

constants:
code bytes:
00: 2d 01 38 00 36 00 36 00 0e 2e
code disassembly:
000:      2d 01       VARS   1
002:      38 00       SETv   0
004:      36 00       GETv   0
006:      36 00       GETv   0
008:      0e          MUL    
009:      2e          RETURN 

By now, you can probably divine most of the coding rules by inspection:

Some fine points:


Go to the first, previous, next, last section, table of contents.