17. What happens when we run code?#

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:

  1. Load Program

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

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

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

17.8. Questions After Today’s Class#

17.8.1. What would assembly code look like for a computer with a good memory management system (not being able to access @0 or @1 without checks that things exist there of the correct type)?#

That does not exist, the high level language does that and passes it to assembly already corrected. This is one of the reasons we have high level languages, to abstract away these details so that we do not have to think about the low level details of the computer all the time.

17.8.2. Are we able to use old memory after we over write it?#

Yes!

17.8.3. What should I understand when simulating as I was not grasping the concepts#

Apply your tracing skills, follow what the steps are. Try to answer the following questions:

  • How does information flow through the computer?

  • How does the assembly compare to a high level programming language?

17.8.4. How prevelent is this in the working world ?#

A more complex version of this this happens every time any code is executed. If the question is how many jobs work at this level, any jobs working at the hardware interface, building new devices woudl work with these ideas directly.

However even higher level things like optimizing your own code, even with the assistance of an optimizer relies on you understanding that all fo this happens. This simplified version is enough for you get ideas and look up details later.

17.8.5. How do different assembly instructions interact with each component in more complex programs?#

A more complex program would compile into a longer assembly instruction list.

17.8.6. For purposes of efficiency, when the developers are writing built in libraries for their languages, do they write these in assembly?#

Typically, no, you still write it in at least a C or Rust type high level language and then use an optimized copmiler to distribute it optimized. People who work most with assembly would be those writing compilers.

17.8.7. is it likely we have to write in assembly in our future jobs#

No, the goal here is that you should know that your code will be expanded into more steps and what the types of operations that the computer can actual execute are so that you can make good choices.

For example, even if using an AI to write code you might use a tool like copilot arena to get multiple options from LLMs and then you need to choose which one is best. The more you understand about what the computer does the better you will be at choosing a good one, or knowing when to reject what the AI gave you and ask it for something better.