PLAIN – Math & Expressions

We have so far demonstrated PLAIN Assembly with little difference to an actual 3.gen language. We will see the low-level assembly – instruction by instruction – a bit more as we dig into math and expressions.

As we are an assembler we need to maintain a principle of 1:1 between code and actual instructions. The challenge is that I would still like to write code like this:

If (A > B+C/D)
    // do something
End

A classic assembly would not allow these expressions but rather force us to do math with separate instructions and we could have implemented something like this:

DIV R1,C,D
ADD R2,B,R1
IFGT A,R2
...

This would actually be slow in a VM since we are an interpreter, but also to close to a native assembly language for my taste. The result I want to try out is a horse trade – I accept that my IF statement use 2 instructions and create a new instruction dedicated to calculate Math and boolean expressions.

Assign R1 = B+C/D
If (A > R1)
            // do something
END

This would be the actual assembly, but I am happy to let the assembler insert Assign statements in advance of If, Call and Raise as needed as a  trade-off.

The assign instruction is dedicated to calculate math expressions written in a natural, readable format. The assembler will parse the expression and build a math table that in run-time calculate the expression that is stored in R1 – the math table is the 2nd parameter in the Assign instruction. We will still need to interpret the Math table, but as this now is dedicated for Math I can also do tricks to optimize speed. But, Math and Expressions are a VM’s weakest link with regards to performance.

Leave a Reply