Y13 Unit 0 - Class Structure
Y13 Unit 1 - Searching Algorithms
Y13 Unit 2 - Abstract Data Structures (HL)
Y13 Unit 3 - Computer Organization (Binary)
Y13 Unit 4 - Computer Organization (Architecture)
Y13 Unit 5 - Resource Management (HL)
Y13 Unit 6 - Control (HL)
Paper 3
1 of 2

Machine Instruction Cycle

Fetch

The CPU is responsible for knowing which instruction it needs to take from the primary memory in order to operate correctly. To do that it sends the appropriate address through the memory (address) bus to the primary memory. The instruction that resides in the specific address is then copied into the data bus and sent to the control unit (CU).

Decode

The instruction that has been received by the CU is then decoded. Decoding an instruction allows the CPU to be aware of any additional data that are necessary for the execution of the instruction. Any required data that need to be loaded from the primary memory in order for the instruction to be executable are then fetched. The addresses of these data are placed into the memory (address) bus and the data from these addresses are received by the CPU through the data bus.

Execute

The CPU executes the instruction using the necessary data that have been loaded and calculates a result. Depending on the result, additional data may be needed. These data are fetched from the primary memory for further calculations. As before, the addresses of these data are placed into the memory (address) bus, and the data from these addresses are received by the CPU through the data bus.

Store

After executing the instruction and computing the result the CPU then stores the result in the primary memory. To do so, it specifies the address where the result will reside in the primary memory, using the memory (address) bus and sends the data through the data bus. The CPU then checks for the next instruction and repeats the steps described above by fetching, decoding, executing and finally storing the result.

Status: Its Complicated…

So, in *real life* the CPU has many registers, not just two. There are a 16 registers that we care about in the MDR. These hold data that we need for our CPU and ALU to do their job. Think about each as a specific place on the kitchen counter. Instead of having the MDR hold the ingredients with his hands, he can place them in any of 16 spots and grab them as needed. These are shown in the table below.

The MAR is still just one register, for our purposes, and is called %rip, or register instruction pointer.

Each register can hold up to 64 bits, but if you only want to access a smaller amount of the data it holds you can use aliases for its name. For example, if you want to read the data passed through the first argument to a function, you should look into the %rdi register. If you only want to read 32 bits, then look at the %edi register, which “lives” inside the %rdi register. If you only want to read 16 bits, the look at the %si register, and so on.

Instruction Syntax

In the screenshot above you can see two windows. The top window contains information in the registers. Each register either contains data or an address to an instruction. Sometimes they contain an address to data as well. You can see that this data or addresses are represented in hex form in the middle column. On the right column, you will find the decimal equivalent of the hex, if this is a piece of data, or you will find the same hex value which means that it is an address to somewhere else in memory (perhaps to another instruction)

The bottom window contains the current instruction in the MAR. This instruction is highlighted and has not been performed yet. On the left-most column, you will find an address to the instruction, akin to a page and line number in the cookbook. The next column has the name of the function we are currently performing. Next, we have the instruction to be performed followed by the data for the instruction (or a reference to which register holds the data).

To understand what each instruction does, it will be helpful for you to look at the documentation for this architecture in a nice cheat sheet form:

https://cs.brown.edu/courses/cs033/docs/guides/x64_cheatsheet.pdf

Let’s take a look at what’s happening above in more detail:

Line 1

  1. <phase_1> — means that this is the start of the phase function. The start of the recipe in the cookbook. The MAR is pointing at the first instruction to be performed by the chef(ALU).
  2. sub — this is the instruction itself. we are going to subtract some data from some other data.
  3. $0x8, %rsp — these are the raw ingredients and a register reference. We will subtract hex 0x8 from whatever data is at the %rsp register.

Line 2

In the debugger we could perform one operation by using the stepi command. This will perform the operation and move to the next instruction.

  1. <phase_1+4> — This is the place in the cookbook where we can read this instruction. It can be found at four spaces after the first instruction for this function.
  2. lea — load effective address. This means that we are going to look into one address (say, a spot in the fridge) and we’ll get the data at that address and then transfer it to a register on our kitchen counter.
  3. 0x1581(%rip), %rsi —
    • let’s look at the first argument, 0x1581(%rip). Remember that %rip is the same as our MAR kitchen helper. He’s the one holding the current instruction. This instruction says, from where the %rip is right now, add 0x1581 spaces and you will find the data that you want in the fridge(primary memory).
    • %rsi — if you look at the purpose for each register in the table above, you’ll notice that we use %rsi to place the 2nd argument to a function. If we look one instruction ahead we’ll notice that we will call a function called <strings_not_equal>. So we took some data from 0x1581(%rip) and we are going to transfer it, or load it, into our %rsi register. The function declaration might look like this in code: strings_not_equal(String stringOne, String stringTwo); So %rsi will contain whatever string we’re comparing stringOne to. Since %rdi contains the string used in the first argument, what do you think each of these values holds? Which will be the user input and which will be the secret password to detonate phase one of the bomb?

Line 3

In this line we are using the call instruction and we are telling the computer to move execution to a new address (0x5599b292168e) in the cookbook. We’re now going to perform a new part of a recipe, you could call it a sub-recipe. This sub-recipe is called <strings_not_equal>.

If you’re a chef, phase_1 could be “Make pizza” and <strings_not_equal> would be “how to make pizza dough”.