EOC-2- VirtualMachine-The Stack (1)
EOC-2- VirtualMachine-The Stack (1)
top
Basic operations
push: adds an element at the stack’s top
pop: removes the top element
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Stack
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Stack arithmetic
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Arithmetic operations
VM pseudocode (example)
// d = (2 – x) + (y + 9)
push 2
push x
sub
push y
push 9
add
add
21
pop d
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Arithmetic / Logical VM operations
Command Return value Return value
add x+y integer
sub x–y integer x
neg –y integer y
eq x == y boolean
gt x>y boolean
lt x<y boolean Each command pops as many
operands as it needs from the
and x And y boolean
stack, computes the specified
or x Or y boolean operation, and pushes the
not Not x boolean result onto the stack.
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 9
Logical operations
VM pseudocode (example)
// (x < 7) or (y == 8)
push x
push 7
lt
push y
push 8
eq
or
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Logical operations
VM pseudocode (example)
// (x < 7) or (y == 8)
push x
push 7
lt
push y
push 8
eq
or
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 11
The VM language (abstraction)
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 12
The big picture: Compilation
Source code (Jack) Compiled VM code
class Foo { ...
static int s1, s2; ...
function int bar (int x, int y) { ...
var int a, b, c; ...
push static 0
...
push argument 1
let c = s1 + y;
compiler add
...
pop local 2
}
} each variable has a kind: ...
local, argument, static, ...
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Memory segments
stack memory
segments
push
00
local Our VM model features 8
pop 11 argument abstract memory segments;
22 static
... constant The role of each segment:
this Later.
that
pointer
... temp
where i is a non-negative integer and segment is local, argument, static, ..., temp
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 14
The VM language: Abstraction
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 15
The VM language: Implementation
Answer
Arithmetic / Logical commands We’ll develop a VM translator:
• add, sub , neg A program that translates each VM command
• eq , gt , lt into machine language instructions
• and, or , not
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 16
Implementing push constant i
(example
values)
Implementation
push constant 17
The stack is stored in
the RAM, from
address 256 onward
The stack pointer is
stored in RAM[0]
(before) (after)
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 17
Implementing push constant i
(example
values)
Pseudocode
// push constant 17
RAM[SP] = 17
SP++
Implementation
Hack assembly
The stack is stored in // D = 17
the RAM, from @17
D=A
address 256 onward
// RAM[SP]= D
The stack pointer is @SP
stored in RAM[0] A=M
M=D
// SP++
(before) @SP (after)
M=M+1
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 18
Implementing push constant i
Abstraction Implementation
VM code Assembly code
push constant i VM translator // D = i
@i
D=A
// RAM[SP]= D
@SP
Notes A=M
M=D
1. constant is not a “real segment”
// SP++
(used for VM syntax consistency) @SP
M=M+1
2. There is no pop constant i command.
Nand to Tetris / www.nand2tetris.org / Chapter 7 / Copyright © Noam Nisan and Shimon Schocken Slide 19
Thank you