L1VM - assembly VOL II

Here I will show how to read more advanced assembly expressions.

This is a part of my “prog/hello-math.l1com” program:

// hello-math.l1com - Brackets - Hello world
//
// RPN math and infix math expressions
//
(main func)
	(set int64 1 zero 0)
	(set int64 1 x 23)
	(set int64 1 y 42)
 	(set int64 1 z 13)
	(set int64 1 foo 23)
	(set int64 1 ret 0)
	(set string 13 hello "Hello world!")
	// print string
	(6 hello 0 0 intr0)
	(7 0 0 0 intr0)
	// reversed polish notation (RPN):
	{ret = x y + z x * *}
	(4 ret 0 0 intr0)
	(7 0 0 0 intr0)

Lets take a look at:

{ret = x y + z x * *}

This is a math expression in RPN (reversed polish notation) with the operands after the numbers.

The assembly output is this:

.data
Q, 1, zero
@, 0Q, 0
Q, 1, x
@, 8Q, 23
Q, 1, y
@, 16Q, 42
Q, 1, z
@, 24Q, 13
Q, 1, foo
@, 32Q, 23
Q, 1, ret
@, 40Q, 0
B, 13, hello
@, 48Q, "Hello world!"
Q, 1, helloaddr
@, 61Q, 48Q
.dend
.code
:main
loada zero, 0, 0
loada helloaddr, 0, 1
intr0 6, 1, 0, 0
intr0 7, 0, 0, 0
loada x, 0, 2
loada y, 0, 3
addi 2, 3, 4
loada z, 0, 5
muli 5, 2, 6
muli 4, 6, 7
load ret, 0, 8
pullqw 7, 8, 0
loada ret, 0, 7
intr0 4, 7, 0, 0
intr0 7, 0, 0, 0

The “x” variable is load into register “2”. The “y” variable is load into register “3”:

loada x, 0, 2
loada y, 0, 3

Now “x” and “y” are added and the result is stored into register “4”:

addi 2, 3, 4

Then the variable “z” is load into register “5”:

loada z, 0, 5

The next code does the multiplications. The variable “z” is multiplied by “x” and the result is stored in register “6”:

muli 5, 2, 6

The next multiplication multiplies the register “4” by register “6” and stores the result in register “7”:

muli 4, 6, 7

The final step is to store the result into variable “ret”:

load ret, 0, 8
pullqw 7, 8, 0

Have some fun!

14: assembly VOL III