Starting the CPU Emulator¶
We used the CPU Emulator from nand2tetris
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.
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:
Click the folder above the ROM section
Navigate to
projects/05load add.hack
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
Aprogram counter the
PCthe ALU
the data register
D
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)
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
Prepare for Next Class¶
Refresh your knowledge of logical operations/ logic gates: and, or, xor, not. For an example reference see the logicemu emulator we will use in class
Badges¶
Review the notes from today
Run and examine how rect.hack and max.hack in the
projects/05/folder of the CPU Emulator 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?
Review the notes from today
Run and examine how rect.hack and max.hack in the
projects/05/folder of the CPU Emulator 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? 2. Write an excerpt of code in a high level compiled language that would compile into this `max.hack`. Try writing multiple different versions.Use the simulator to figure out what the program
mystery.hackthat is on the notes for today does.
Experience Report Evidence¶
no separate evidence