0

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
  • Is there a question here? It looks like you've answered the question in the title (or at least one way for each to do it) – Chris Dodd Dec 08 '21 at 02:49
  • @ChrisDodd Overall, its a check for understanding post. Other, more in particular questions include: 1) What do comments look like for Stack Machines and 3AC Machines? 2) What does 'sub' do for for Stack Machines? Ie is it 8 - 3 or 3 - 8 – ThreeRingBinder Dec 08 '21 at 17:28
  • machines don't have comments, assemblers have comments. Assembler comments are all over the map `#`, `;`, `/*`..`*/`, `(*`..`*)`, `//`... – Chris Dodd Dec 08 '21 at 19:54
  • stack machine binary ops are almost invariably equivalent to 'tmp = pop(); top-of-stack = top-of-stack op tmp;', but the precise semantics depends on the machine – Chris Dodd Dec 08 '21 at 19:56

0 Answers0