First MIPS CPU Simulator Exercise

 


⇒ Bring up the "K Sub-MIPS Simulator referred to in these instructions in a separate, side-by-side browser window, if you haven't already.

Download this Markdown template to record your answers to the questions below and submit them to Kit.

The "K Sub-MIPS Simulator" demonstrates how an actual computer runs at a low level. It is based on a particular computer chip architecture called "MIPS". Phones, set-top boxes, and embedded IoT appliances run MIPS or similar architectures, and the fundamental ideas are the same for computers of all sizes. This simulator handles a significant subset of MIPS functionality, which is why it is called the "K Sub-MIPS" simulator.


Getting Started with the Simulator

The simulator has three tabs or modes: Edit Assembly Code, Generate Machine Code, and Run Simulator.


Editing Assembly Code

When you load the page, the Edit pane is showing, with a sample program already entered. You can switch to a different sample program or type in a program of your own. For now, we will use the first sample program.

⇒ Switch to the "COMP 230 Version" of the first sample program by clicking on the appropriate button at the bottom of the "Assembly Lang Program" area. (The COMP 101 sample program includes several instructions that aren't actually MIPS instructions, but have been simplified for that class.)

Tip: If you want to see the comments for an assembly language program, "save" it to the Saved Output area, where you can see the whole width of the assembly language source.
Tip: you can reset the Sub-MIPS Simulator back to its initial condition at any time by reloading the page.

Generating Machine Code

⇒ Click on the Generate Machine Code tab to move to the next pane.

Computers store and execute programs as binary data — long strings of 1's and 0's, known as Machine Code or Machine Language. This is very hard for humans to read, though. Assembly Language is a human-readable version.

Machine Language instruction   Assembly Language version
00000000101001100001000000100000 ADD R2, R5, R6

We can only write programs in Assembly language, though, if we have a program that can translate our programs to Machine language. That program is called an Assembler (and the translation process is often called "assembling").

In the Generate Machine Code tab you can run the Assembler (a translator), to translate your code to binary machine code. An Assembler takes 2 passes through the program. The first pass looks for any leading labels and notes where it found them. The second pass does the actual translation.

Question 1: Experimenting with the Assembler

⇒ Click on the Pass1 button. What happens?

⇒ Click on the Pass2 button. What happens? (There are actually 2 variations for Pass2. Click on one and then click on the other. What's the difference in the output?)


Running the Simulator

⇒ Click on the Run Manual Simulator tab to move to the next pane.

In the Simulator tab, you can run your program. The Central Processing Unit, or CPU (often just called a "processor" these days) is the heart of any computer. It cycles through the instructions in a program. In each cycle, it reads in the next instruction (the "fetch" step) and then performs the operation (the "execute" step). For this simulator, you will manually fetch and then execute each instruction.

Question 2: The "Fetch/Execute" Cycle

⇒ What is the value of the Next Instruction Address (also known as the Program Counter, or PC) when you arrive in this pane?

⇒ Click on the Fetch button. What happens?

Before you click on the Execute button, write down what you think will happen. Also write down the values in any registers or memory addresses that you think might change as a result of executing this instruction. (You can go back to the Edit tab and look at examples in the Assembly Language Reference sheet if you want to read a description of what the first instruction — or any instruction — is expected to do.)

⇒ Now execute the instruction by clicking on the Execute button. What happened? Write down your actual results and see if they match your expected results.

⇒ Do the same for the second instruction. Write down your expected results, then your actual results and see if they match your expected results.

Expected Results Actual Results
   
   
   
Question 3:

⇒ Continue to Fetch and Execute until the simulator halts. How many fetch/execute cycles are required? What is the program doing?

Note: You can change the value in M40 from the Edit Mode page to test your program with several inputs. You don't even need to re-generate the machine code (because you didn't change the program); just go back to simulator and re-run.
(By the way, real programs do not generally end with an instruction to HALT the computer (very dangerous!). Instead they clean up and return control to the operating system.)

Changing the Program

Question 4: Two Minor Changes

⇒ Add two comments at the top of the first COMP 230 sample program, with your name and an explanation of what the program does. (Use copy-and-paste rather than re-typing the program!) Generate the new machine code and run to test. What does the Assembler do with comments? (It could ignore comments, but it would make it harder for the Simulator to highlight the current Assembly Language instruction when the program is running if the Assembly Language and Machine Language instructions don't line up.)

Tip: You can edit assembly programs by changing the contents of the text fields in the Assembly Lang Program area directly, or you can "save" an existing program to the Saved Output area and then copy it into the text area on the right, edit it there, and click on the "Read in to Assembly Lang Program Area" button.

⇒ Insert a value into M44. Change the program to read two values from M40 and M44 into registers, add them, and store the result. Using $t0 as storing the beginning of your data area in memory, read in the two values using different offsets — 0($t0) and 4($t0).

Tip: You can change the values in M40 and other memory locations from the Edit Mode page to test your program with several inputs.

"Save" and then copy your modified program into the Markdown file.

Writing New Programs

Question 5: Prediction

⇒ Think about what it would take to write a program that takes 3 values from memory, adds them together, and stores the sum in M60. Once you have loaded the memory values into registers, how can you add three register values to get a sum? How many lines of code do you think it will take? (Thoughtful guess - don't worry about being right.)

Question 6: New Program — "Sum 3"
⇒ Write and test your program. "Save" and copy your program into the Markdown file.

Question 7: New Program — "Copy Memory Location"
⇒ Write a program that reads the value from M40 and copies it to M44, then halts. You can do this in 4 lines, including the HALT. Test your program, then "Save" and copy it into the Markdown file.

Playing with the Next Instruction Address Field

This section is just to explore. You don't need to include your answers to these questions in your Markdown submission.

Click on the "Run Simulator" tab to restart the Simulator (set the PC back to 0) or reload the page to reset the program.

Question 8:

⇒ In the Run Simulator tab, notice that you can change the "Next Instruction Address" field. What happens if you start at address 4, instead of address 0? Run the program until it halts. What did it do?

Question 9:

⇒ What happens now if you set the Next Instruction Address back to 0 and try to Fetch?

Question 10:

⇒ Reload the page (or just click on the Run Manual Simulator tab) and re-translate the program. Fetch and execute the first 2 instructions, but skip the 3rd by changing the Next Instruction Address to Fetch and Execute the 4th instruction (STORE). Now, instead of fetching the HALT instruction, set the Next Instruction Address back to 0. What does your program do?

Question 11:

⇒ What happens now if you set the Next Instruction Address to 20? To 30?

Question 12:

⇒ What happens if you Fetch multiple times before you click the Execute button?