L1VM - course 15
L1VM - assembly VOL IV
Here we will create the “square” calc function of VOL III in inline assembly.
Here is the Brackets code:
(square func)
(set double 1 num 0.0)
(set double 1 square 0.0)
(num stpopd)
{square = num num *}
(square stpushd)
(funcend)
To write inline assembly we need the begin and end command:
(ASM)
assembly code
(ASM_END)
Thirst wee need to pull the variable from stack. Then we do the multiplication on the variable. And as a last step we need to push the result on the stack.
Here we go:
(square func)
(ASM)
stpopd 1
muld 1, 1, 2
stpushd 2
(ASM_END)
(funcend)
Note: the end of the function: “(funcend)” sets the needed “rts” assembly code for us! And the cool thing is: we did not need a single variable to do the calculation! It’s all done in registers for this simple code!
Have some fun!