Notes for Nand2Tetris: Virtual Machine I: Stack Arithmetic
published at 2023-07-19
nand2tetris
programming-language
software-engineering
vm-abstraction
stack-machine
note
This is a note for Nand2Tetris unit 7 (Part II, Unit 1).
Unit 7.0
Jack program:
class Main {
function void main() {
do Output.printString("Hello World!");
do Output.println(); // New line.
return;
}
}
What makes the abstraction work?
- Assembler
- Virtual machine
- Compiler
- Operating system
Nand2Tetris Part II is more demanding than Nand2Tetris Part I.
Unit 7.1
Module 1: Virtual Machine
- Understanding the abstraction
- Building the implementation
Module 1: Take Home Lessons
- Compilation (big picture)
- Virtualization
- VM abstraction
- Stack processing
- VM implementation
- Pointers
- Programming
Unit 7.2
VM Abstraction: the Stack
Stack operations:
- push: add a plate to the stack’s top
- pop: remove the top plate
Applying a function
- pops the argument(s) from the stack
- computes
on the arguments - pushes the result onto the stack
Stack arithmetic:
high-level:
x = 17 + 19
After compilation:
low-level:
push 17
push 19
add
pop x
Stack machine, manipulated by:
- Arithmetic / logical commands
- Memory segment commands
- Branching commands
- Function commands
Unit 7.3
VM Abstraction: Memory Segments
Memory segments:
- local
- argument
- this
- that
- constant
- static
- pointer
- temp
Syntax:
- push
: Where is: argument, local, static, constant, this, that, pointer, or temp and is a non-negative integer - pop
: Where is: argument, local, static, this, that, pointer, or temp and is a non-negative integer
Unit 7.4
Pointer manipulation
asterisk: *
Stack machine
…
VM translator perspective
VM Translator:
- A program that translates VM code into machine language
- Each VM command generates several assembly commands
Unit 7.5
Implementing local