17. What happens when we run code?#
Important
We reviewed the solution to updating your assigmnment action and how to create issues for missing dates
17.1. Starting the CPU Emulator#
In your terminal:
cd nand2tetris/tools
and then
bash CPUEmulator.sh
CPUEmulator.bat
or in file explorer, Double click on the CPUEmulator.bat
file
and it will open a new window
For more on how the emulator works see the CPU Emulator Tutorial.
For much more detail about how this all works chapter 4 of the related text book has description of the machine code and assembly language.
17.2. Loading a program#
We’re not going to do project 5, which is to build a CPU, but instead to use the test programs designed to check if your personally build CPU works with the CPU Emulator they provide to understand a simple canonical CPU.
We’re going to use the test cases from the book’s project 5:
Load Program
Navigate to
nand2tetris/projects/05
17.3. How does the computer actually add constants?#
We’ll use add.hack
first.
This program adds constants, 2+3.
It is a program, assembly code that we are loading to the simulator’s ROM, which is memory that gets read only by the CPU.
Run the simulator and watch what each line of the program does.
Notice the following:
to compute with a constant, that number only exists in ROM in the instructions
to write a value to memory the address register first has to be pointed to what where in the memory the value will go, then the value can be sent there
The simulator has a few key parts:
address register the
A
program counter the
PC
the ALU
the D register
If you prefer to read, see section 5.2.1- 5.2.6 of nan2tetris book
This program the first instruction puts 2 in the address register from the instructions in ROM.
Then it moves the 2 through the ALU to the data register (D)
then it puts 3 on the address register
then it adds the numbers at D and A
then it puts 0 on the address register
then it write the output from the ALU (D) to memory (at the location indicated by the A register)
17.4. The max between two numbers#
To compute the maximum value max.hack
:
subtracts the two numbers
compares the difference to 0
jumps to different lines based on if that difference is >0 or not
assigns the address register a value accordingly
copies one of the two values to the new location
Note
This program assumes the two values to compare already exist in memory, the program does not set them. To test it, try changing the values in memory locations 0 and 1.
17.5. Prepare for Next Class#
there will be time to work on this at the start of class
In fractionalbinary.md use 8 bits to represent the following numbers by using 4 bits as usual (8,4,2,1) and the other 4 bits are 1/2, 1/4, 1/8, 1/16th:
1.5
12.75
7.5625
15.9375
17.6. Badges#
Run and examine how rect.hack and max.hack in the
nand2tetris/projects/05/
folder work. Make notes and answer the questions below in assemblyexplore.md.1. Write an excerpt of code in a high level compiled language that would compile into this `max.hack``. 1. What does rect.hack do? 1. What did you learn trying to figure out how it works?
Run and examine how rect.hack and max.hack in the
nand2tetris/projects/05/
folder work. Make notes and answer the questions below in assemblyexplore.md.1. What does rect.hack do? 2. What did you learn trying to figure out how it works? 3. Write an excerpt of code in a high level compiled language that would compile into this `max.hack``. Try writing multiple different versions.
17.7. Experience Report Evidence#
no separate evidence