6. Survey of Hardware

6.1. Where does assembly come from?

In order to watch what happens in hardware when a program runs, which is our goal today, we will execute a compiled program. It is written in assembly code. Typically, we do not write assembly, but instead it is produced by the compiler.

Technically assembly instructions and the values we operate on are represented in binary on hardware, but these more readable level instructions, though basic, are a useful abstraction. These instructions are also hardware nonspecific.

6.2. Using the simulator

On MacOS:

cd path/nand2tetris/tools
bash CPUEmulator.sh

On Windows: Double click on the CPUEmulator.bat file

We’re going to use the test cases from the book’s project 5:

  1. Load Program

  2. Navigate to nand2tetris/projects/05

We’re not going to do project 5, which is to build a CPU, but instead to use the test.

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.

6.3. Adding 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

Try it yourself

Write code in a high level language that would compile into this program. Try writing two different variations.


for itme in list:

6.4. Using a variable

Next use the max.hack.

This one compares the values at two memory locations. In order to use it, you have to write values to the RAM 0 and RAM 1 manually.

It first takes the value from each location and passes the first value to D, then uses the ALU to assign the difference between the two values to D. Then, if the value is greater than 0, it jumps to line to line 10 in the ROM (of the instructions). Line 10, sets A to 0 and 11 sets D to the value from RAM 0. If, instead, the value is less than 0, A is set 1, then and D is set to that value. Then the program points A to 2 and writes the value from D there.

Try it yourself

Write code in a high level language that would compile into this program. Try writing multiple different versions.

What does this program assume has happened that it doesn’t include in its body.

6.5. Using output

The rect.hack program writes to output, by using a specific memory location that is connected to the output.

Try it yourself

Try working through this program using the tools to understand what it does

6.6. Prepare for Next Class

  1. Read these notes and practice with the hardware simulator, try to understand its assembly and walk through what other steps happen. Make notes on what you want to remember most or had the most trouble with in hardwaresurvey.md

  2. Review and update your listing of how data moves through a program in your abstraction.md. Answer reflection questions below.

  3. Review the commit history and git blame of a repo in browser- what must be in the .git directory for GitHub to render all of that information? (be prepared to discuss this in class)

  4. Fill in the Know and Want to know columns for the new KWL chart rows below.

  5. Begin your grading contract, bring questions to class Tuesday.

6.6.1. Abstraction reflection

1. Did you initially get stuck anywhere?
1. How does what we saw with the hardware simulators differ from how you had thought about it before?
1. Are there cases where what you previously thought was functional for what you were doing but not totally correct? Describe them.

6.6.2. New KWL chart rows

|file system | _ | _ |_ |
|bash | _ | _ | _ |
|abstraction | _ | _ | _ |
|programming languages | _ | _ | _ |

6.7. More Practice

  1. Complete the Try it Yourself blocks above in your hardwaresurvey.md.

  2. Expand on your update to abstraction.md: Can you reconcile different ways you have seen memory before?

6.8. Questions After Class

6.8.1. Is assembly then converted to binary and if so, why isn’t the code translated straight to binary instead of from code to assembly to binary?

Yes, assembly is converted to binary in order to write it to the hardware. The Emulator that we used today showed the numbers in RAM and the instructions both in higher level representations than binary. The advantage to assembly is that it is hardware independent and human readable. It is low level and limited to what the hardware can do, but it is a version of that that can be run on different hardware.

6.8.2. What does A mean in CPU Emulator?

6.8.3. When the prepare for next class says things like “organize your thoughts on …” is it expected that we make a .md file somewhere on GitHub to show this work?

For things I expect to review, the instructions will tell you where it goes. Other things, we will bring up in a future class, so you should have it handy, but you can put it in a repo or a private set of notes.