19. What happens when we run code?#

Today we are going to use a CPU emulator to see what happens when we run code. This also lets us start to learn some of the components in a computer in a little bit more detail.

19.1. Start the emulator#

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.

  • Navigate to where you downloaded nand2tetris in File exporer

  • go in the tools folder`

  • Double click on the CPUEmulator.bat file

19.2. Load a program#

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

  1. File

  2. Load Program

  3. Navigate to nand2tetris/projects/05

  4. select add.hack

  5. click load ROM

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

19.3. How does the computer actually add constants?#

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.

The simulator has a few key parts:

  • address register

  • program counter

  • ALU

  • ROM

  • RAM

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, it does not ever get written to RAM

  • 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

For more detail 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)

19.4. High level code#

This code is equivlatent to:

x = 2+3

or

int x = 2+3

Specifically note:

  • this program does not put the 2 and 3 into RAM before adding them

  • it stores the result, but does not print it

19.5. Modifications to try#

  • Change the program to add 3+4

  • Change the program to store the result in memory location 16 instead of 0

  • Change the program to store the two numbers to RAM first, then add them.

19.6. 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)?

19.7. Be careful with variable names#

Warning

This was a bit of a tangent, but is a useful to know thing

python
Python 3.11.4 (main, Jul  5 2023, 09:00:44) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> sum([3,4])
7
>>> sum =3+4
>>> sum([3,4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> sum
7
>>> exit()

If we assign sum as a variable, Python lets us even though sum is a builtin function.

then, we cannot use the sum funciton again until we restart the interpreter.

python
Python 3.11.4 (main, Jul  5 2023, 09:00:44) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> sum([3,4])
7
>>> sum_ =3+4
>>> sum([3,4])
7
>>> my_sum =3+4
>>> exit()

19.8. Prepare for Next Class#

  1. 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

19.9. Review today’s class#

  1. 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. Explain how max.hack works in detail.
2. Write code in a high level language that would compile into this program.
3. What does this max.hack assume has happened that it doesn't include in its body?

19.10. More Practice#

  1. 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. Explain how max.hack works in detail.
4. Write code in a high level language that would compile into this program. Try writing multiple different versions.
5. What does this max.hack assume has happened that it doesn't include in its body?

19.11. Experience Report Evidence#

Add in your experience report the modifcations to the code as above.

19.12. Questions After Today’s Class#

19.12.1. Will there be a detailed breakdown of today’s class in the notes?#

It’s an overview, but there are links to the textbook chapter for more detail. If you have a specific question, open an issue and I can add that specific detail here.

19.12.2. Can this be used for more than just seeing how things get stored in memory?#

You can use the emulator for also testing how how this type of assembly code works.

19.12.3. Why are the roms .hack files?#

this CPU is a simplified CPU that is just the essentials for the sake of learning. Hack is the name of the machine language that this CPU uses.

19.12.4. What would you say is the most complex operation a CPU can do?#

This is a hard question. The CPU does all of the thing sthat our computers do other than the actual display and input. However, what is the most complex single instruction, is a little bit more clear.

We can think of complexity in different ways, but I will, for the sake of this answer, say that the largest circuit is th emost complex. In that case, it is probably multiplication.

19.12.5. Does the process become more complex with more processors?#

The base is the same, but there are added steps to schedule and assign work to separate processors.

19.12.6. Are there more that one ALUs in modern CPUs? If so, does the number have some relation to the number of CPU cores or threads?#

Yes, CPU “cores” are roughly equivalent to ALUs. Occaionally there are additional ALUs that are used as if they are simpler because of the way mass production and economy of scale works…

Threads are a software level abstraction. The number of threads that execute in parallel truly is the one per core, but you can have more threads than cores, and they’ll get scheduled partially in parallel and partially in sequence.

19.12.7. What is the CPU Emulator#

It is a program that is designed to simulate what happens in a CPU which is the centrl processing unit of a computer.

19.12.8. what do the first two likes of rect.hack do?#

@0 puts 0 in the address register. D=M loads the value from the memory location indicated in the address register to the D register. Together this means that it loads the value of the RAM at location 0 to the D register.