Machine Cycle Lab
This lab uses this web page which simulates a CPU to demonstrate the machine cycle as described in your book. We'll experiment with it a bit to see how it works. Along the way, you will be asked to save the browser screen to a file. You should use the browser's print-to-file function.

The CPU web page has the five parts described in your text. At the left is is the memory, which has 16 cells, each holding 16 bits. (Your PC is a bit larger, having some two billion or more cells of 8 bits each.) You can click on any of the memory cells and change its value. The address for each cell is given in decimal and hexadecimal. In the center are the control unit and the ALU, each with the their needed registers. There also an I/O unit which presents a very simplified notion of peripheral devices. It contains input and output boxes which can read or write numbers or characters. (The devices are laid out on the page according to its size. The memory unit should always be on the top left, but the others may vary quite a bit.)

  1. Open the CPU simulator. It will open in a new tab (or window). Be careful not to close this tab, or use it to visit another page, while you are working, because if you have to re-open the machine, it will forget everything you've done.
  2. First, type your name in the Notes box so it will show up on the screen saves you are going to make. (If your name is removed by a page reload or some such, just enter it again.)
  3. To begin, let's practice a bit with registers. The Memory Unit, Controller and ALU all contain registers. Registers are just places to store a single binary value. Initially, they all contain zero.
    1. Click on memory location 13, and enter the number 122. A control will appear for entering the number when you click on the cell, then it will display the number entered.
    2. Now, right-click on the location 13, and you will see a list of display formats. Click on “Binary number.” The display format does not change the value of the cell, just the way that the browser shows it to you. Each register always contains 16 bits, however they are displayed.
    3. Right-click on 13 again, and choose “Base 16 number”. You will see the value displayed in hexadecimal.
    4. Right-click on 13 yet again, and return to “Decimal number”. See, nothing actually changed.
    5. Still yet again, right-click on cell 13. Choose Text. You should see the letter z in the cell. It seems that 122 is the ASCII code for the lower-case letter z.
    6. Now, click on memory location 6, and enter the assembler instruction sub 3,6,1. It will appear in the cell.
    7. Right-click on 6 and choose binary. You will see that the instruction is also recorded as a binary number. Everything is. So how does sub 3,6,1 become 0010001101100001? There is a full description of the coding scheme under the “Instruction Set” tab to the left of the memory unit, but briefly, the bit pattern 0010 0011 0110 0001 is made up of 0010 which is the code for subtract, and the other groups are the binary for 3, 6 and 1, respectively.
    8. Now right-click on location 6 and choose decimal number. Just because it's an instruction doesn't mean it can't be 9057.
    9. Finally, right-click 6 again and choose text. Sixteen bits is enough for two ASCII codes, and this pattern is a hash mark followed by lower-case a.
    10. Now, memory cell 15 will contain zero, since it hasn't been changed since the page was loaded. Right-click on it, and change the format to instruction. It will show nop, which is the instruction coded as zero. Nop stands for “no operation”, and doesn't do anything.
    11. Now save the browser screen to a file using the print-to-file procedure mentioned above. You'll send this to your instructor in the last step of the lab.
  4. Now, let's look at how the machine cycle executes a single add instruction.
    1. Click on memory location 10 and enter any integer you like (besides zero).
    2. Likewise, enter some other integer in memory location 11.
    3. Click on memory location 4, and type this assembly instruction as the value: add 12,10,11. This value will appear in the cell.
    4. Look in the controller unit and find the Program Counter (PC). Click on it and set it to 4. This means the instruction which you entered in memory location 4 will be the next one the CPU executes.
    5. Recall from the textbook that the instruction add 12, 10, 11 will add the contents of memory locations 10 and 11 together, and store the sum in location 12. Locate the Control Panel. Use the STEP button to slowly execute this instruction and watch that happen. Each step in the instruction cycle takes two button presses, so it will take ten presses to complete the instruction. The Controller shows you which step of the instruction cycle it is working on. Press the step button and notice how each step of the machine cycle is performed, particularly:
      • Notice how the instruction is loaded into the Instruction Register (IR) of the Controller during the IF step.
      • Notice how during the Instruction Decode (ID) step, the Controller breaks the instruction up into its parts.
      • Observe the arrival of the data values from memory slots 10 and 11 in the Arithmetic and Logic Unit (ALU) during the Data Fetch (DF) step.
      • Notice when the the numbers are added together by the ALU during the Instruction Execute (IE) step.
      • Observe the return of the the sum to location 12 during Result Return (RR).
      • Also notice when the PC value is incremented at the end of IF, and how that makes the machine ready to fetch the next instruction.
    6. When the instruction execution has completed, print the browser screen to a file. You need to create four files, so make sure you use a different name for each one.
  5. Now try running a short program. It is quit possible to enter your own program into the memory, one slot at a time. But there is also the “Load Pgm” drop-down at the bottom of the Control Panel. It can load any of several small programs into memory.

    Use Load Pgm to place the “Input and Sum” program into memory. The first two instructions input decimal values and store them in cells 10 and 11, respectively. The third instruction (cell 2) adds them together and stores the result in cell 9. The fourth instruction displays the contents of cell 9 in the output area, and the fifth instruction stops the CPU. Without that, it would try to execute the data following the instructions.

    1. Press the start button to run the program.
    2. When the input box in the IO Unit turns green, the CPU is ready for you to type a number. Do so, and press enter (the button in the IO Unit, not the key an the real keyboard). Enter any number you like. Notice when and where it is stored in memory.
    3. Enter any other number for the second input, when the CPU requests it, and notice where it is stored. Later, the sum will appear in the “Output” area.
    4. There is a small scale on the Control Panel to change the speed of the execution. This adjusts the clock rate. You can also press the stop button if you want to examine the machine. Pressing start next will simply resume execution.
    5. After observing how the program works, modify it to subtract the two input values instead of add them. (Use a sub instruction instead of add.) Run the modified program to see that it subtracts.
    6. Immediately after running your modified program (doesn't need to be the first time), save the browser window to a file under some new file name.
  6. Now let's observe a little more complex program. Load the “Counter” program using the “Load Pgm” drop-down, and press start to run it. (Again, feel free to use the Control Panel to adjust the speed.)

    Observe that the program displays a count (zero to ten) in the Output area. The first instruction, sti 10, 0, sets the initial value of the counter by storing a zero in cell 10, and the second one shows the contents of cell 10 in the display. The third instruction adds one to the counter. The fourth instruction, jlt 1, 10, 5 is something we haven't seem before. It compares the contents of cells 10 and 5, and if the value in cell 10 is less, jlt sets the program counter to 1, otherwise it does nothing. That is, if the counter in cell 10 is less than the limit in cell 5, we go back to the instruction in cell 1 and continue execution from there.

    This is how we repeat the display and increment, but stop when we reach the limit. Each time we repeat, the counter increases so we can print something different, but the jlt stops repeating when the counter gets large enough. Now

    1. If you haven't already, push start and run the program to observe its behavior.
    2. Push reset and then start to begin the program running again. Push the stop button when you see the PC reach the value 3. Then push step until the jlt 1, 10, 5 is loaded into the IR. Step through the execution of the instruction. What does the ALU do during the execute step? When result return executes, where is the data stored? What effect does that have?
    3. Modify the program so that, instead of counting, it displays powers of two. You will need to make these changes:
      • The instruction in memory cell zero sets the initial counter value to zero. You should change it to start the counter at 1, so change cell zero to contain sti 10, 1.
      • The limit is stored in cell 5. Change it to 1025. The counter stays under the limit, so this will make the counter go through 1024.
      • Presently, the counter is incremented by the instruction in cell 2, which adds one to the contents of cell 10. Replace this with an instruction which doubles cell 10, such as add 10, 10, 10, which adds cell 10 to itself and stores the result back into 10.
    4. Run your modified program (reset then start) and see that it goes through the powers of two rather than counting. (You can use it the next time you need to do binary conversions.)
    5. When the modified program finishes, save the screen again. Again, use a new file name so you can keep all four.
  7. You should have printed the browser page into files at the ends of parts 3, 4, 5 and 6. Send them to me using this form.