⇒ 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.
Write your answers to the questions below in this Markdown template and submit 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.
The simulator has three tabs or modes: Edit Assembly Code, Generate Machine Code, and Run Simulator.
Tip: you can reset the Sub-MIPS Simulator back to its initial condition at any time by reloading the page.
⇒ 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.
⇒ 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?)
Note: We probably won't include labels in any of the simple programs in this class. You can skip Pass1 for programs without labels.
⇒ 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.
⇒ 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.
Expected Results | Actual Results | |
---|---|---|
⇒ 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.
⇒ Change the second line of the given Assembly Language program to read a second input into R6, rather than making it a copy of what is in R5. (For example, you could read the current value of M48 into R6, or you could insert a value into M44 and read that.)
What does the program do now? Is that what you expected?
Note: You can change the values in M40 and other memory locations from the Edit Mode page to test your program with several inputs.
Copy your program into the Markdown file.
Tip: The easiest way to copy your program to a Markdown file is to first "Save" your modified program to the Output area using the Save button beneath the Assembly Language Program. You can then copy and paste the program easily.
Side Question: Why are the "LOAD" instructions necessary? Look at the three arithmetic instructions on the Assembly Language Reference sheet in the Edit tab. Do any of the three appear to work with values in Main Memory, or do they only work with register values? (Side question to the side question: Looking at the code you started with and the information in the Assembly Language Reference sheet, does this simulator appear to care about capitalization? In other words, does it care whether the "LOAD" instruction is written as "load"?)
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
⇒
Write and test your program. Copy your program into the Markdown file.
Reload the page to go back to original sample program. In the Generate Code tab, run Pass 2, then move on to the 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? (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.)
Question 8:⇒ What happens now if you set the Next Instruction Address back to 0 and try to Fetch?
Question 9:⇒ 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 10:⇒ What happens now if you set the Next Instruction Address to 20? To 30?
Question 11:⇒ What happens if you Fetch multiple times before you click the Execute button?