7.2.VM结构

\(7.2.\)VM Abstraction——the Stack

1. The stack machine

  • The stack machine is an abstraction that consists of an architecture, a stack and a set of operation which we can apply to the architecture.

  • It can be considered an array which offers direct access to every element in the memory, as oppose to the traditional stack which only offers a stack pointer.
  • When we do push x, the address of x will be push into the stack:

  • When we do pop y:

    • the current top value in the stack will be removed,
    • the popped value will override y (in this example, when 7 is popped, 3 becomes the new top value).

2.Stack arithmetic

  If we want to apply a certain function on the stack, we have to do three different things:

  1. Pop as many required operands in the stack as possible.
  2. Apply the function to these operands in a separate process and get the result.
  3. Push the result back into the stack.

  The obstruction supports all these operations inherently, all we have to do it to call add or neg, and these things will take place.

3.VM commands

  • Arithmetic commands:
1
2
3
4
5
6
7
8
9
// d = (2 - x) + (y + 9)
push 2
push x
sub
push y
push 9
add
add
pop d

  • Logic commands:
    1
    2
    3
    4
    5
    6
    7
    8
    // (x < 7) or (y == 8)
    push x
    push 7
    lt
    push y
    push 8
    eq
    or

  Where do these commands come from? It's compiled from the high-level language: