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 a student’s CPU works with the CPU Emulator they provide to understand a simple canonical CPU.
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.
We’re going to use the test cases from the book’s project 5:
Click the folder icon in the ROM section
Navigate to
nand2tetris/projects/05
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
program counter
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 it is >0 or not
assigns the address regiter a value accordingly
copies one of the two values to the new location
Explore ideas¶
Create some experiments in the simulator and write new programs. Here are some ideas, but you can try anything you are curious about.
What line do you change to change where it outputs the data?
How could you add a third number?
How could you add two pairs, saving the intermediate numbers?
How could you do (4+4)*(3+2)?
Prepare for Next Class¶
Think about what kinds of things could go wrong if you divided the work of a problem across multiple ALUs. Think about the timing aspects that we have talked about so far in particular.
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¶
Questions After Today’s Class¶
Can a computer have more than 3 registers?¶
Yes! and they do, there are just 3 roles of registers in the CPU emulator we saw today.
Who choses or how does the computer know that that biniary is that instruction?¶
The hardware designers decide.
So say if you need 12 review and 12 practice badges for you grade goal, they cannot be from the same date?¶
Correct, that would be 24 total different days.
How exactly does the jump decide where to go—like what determines the line it jumps to?¶
It jumps based on the address register value.
What are some topics in computing that assembly is used a lot for?¶
People don’t write assembly much by hand (even before AI), but to optimize a compiler, or write a new one you have to write code that writes assembly.
Will a modern compiler always optimize things into the least cpu cycles, or does it depend on complexity¶
it depends on complexity and the compiler optimization settings.