How do Stack Machines and 3AC Machines solve expressions?

35 views Asked by At

So, I'm struggling a little on how Stack Machines & 3AC (Register) Machines interpret expressions. Take this expression for an example:

4 * 2 - 3

As a 3AC Machine (instruction sets feature three-operands, a type of Register Register Machine), I think it would look something like this

(Note - I use '#' for comments, pretty sure that this isn't the correct syntax):

LOAD 4, r0 # register 0 stores 4

LOAD 2, r1 # register 1 stores 2

MUL r0, r1, r0 # register 0 now stores 8 (4 x 2)

LOAD 3, r1 # register 1 now stores 3

SUB r0, r1, r0 # register 0 stores 5 (8 - 3)

Sorry about the formatting. I don't know how to get subscripts in code blocks.

I think a Stack Machine would look something like this:

LOAD 4
LOAD 2
MUL
LOAD 3
SUB # would this be 8 - 3 or 3 - 8?
NEG # if its 3 - 8 then I need to negate the top
PRINT # outputs the top of the stack
HALT
0

There are 0 answers