Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

What happens when code runs?

CPU emulator

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:

  1. Click the folder icon in the ROM section

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

The simulator has a few key parts:

If you prefer to read, see section 5.2.1- 5.2.6 of nan2tetris book

The max between two numbers

To compute the maximum value max.hack:

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.

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
Practice
  1. Review the notes from today

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

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.