Second 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.

Write your answers to the questions below in this Markdown template and submit it to Kit.

Hint: you can reset the Sub-MIPS Simulator back to its initial condition at any time by reloading the page.


Assembly → Machine Code Translation

The table below describes the Machine Code equivalencies (in decimal) for the 5 instructions in the Assembly program you see when you first load (or reload) the "K Sub-MIPS Simulator. Note that some instructions have their identifying code number at the beginning of the instruction (for example, LOAD and STORE), while some have their identifying code number at the end (for example ADD).

Instruction Name with
Example
Code Layout with
Example
LOAD:
   LOAD R5, M40
35 (LOAD) 0 regNum memLocation    (See Note 1)
Example: 35 0 5 40
COPY:
   COPY R5, R6
0 srcReg 0 destReg 0 32 (ADD)    (See Note 2)
Example: 0 5 0 6 0 32
ADD:
   ADD R2, R5, R6
0 srcReg1 srcReg2 destReg 0 32 (ADD)
Example: 0 5 6 2 0 32
STORE:
   STORE R2, M48
43 (STORE) 0 regNum memLocation
Example: 43 0 2 48
HALT all 1's

Note 1: In MIPS, Register 0 (R0 or $zero) always contains the value 0. In this particular simulator, we're using R0 to indicate the beginning of our memory space. M40 is 40 bytes into our memory space, so is at (0 + 40). If our memory space began at address 1000, we would use a register that contains 1000 and our M40 would actually be at (1000 + 40).
Note 2: The COPY instruction is really just a shortcut for an ADD instruction that adds 0 (the contents of R0) to the source register and puts the sum in the destination register, thus copying the source register to the destination register.

If you're interested, you can look at the more complete Assembly ←→ Machine Instruction Mappings or the full list of registers.

Understanding Machine and Assembly Language

  1. Generate Machine Code for the initial sample program using the version of the Pass 2 button that includes spaces. Verify that the generated machine code matches the table above.

    Hint: The six-bit binary representation for 0 is 000000, for 2 is 000010, for 5 is 000101, for 6 is 000110, for 35 is 100011, for 40 is 101000, for 43 is 101000, and for 48 is 101000.

  2. Why is the machine code for the third instruction (ADD R2, R5, R6) binary for 0 5 6 2 0 32 instead of 0 2 5 6 0 32? Compare the description of the assembly language instruction in the Edit tab with the description of the layout above.
  3. Using the table above, determine what sequence of assembly-language instructions corresponds to the following machine-language program.
            000000 01110 00111 00001 00000 100000      (0 14 7 1 0 32)
            101011 00000 00001 0000000000011000        (43 0 1 24)
            11111111111111111111111111111111 
  4. In English, what overall task does the previous machine-language program perform?
  5. What would this sequence of instructions do?
            000000 01110 00111 00001 00000 100000      (0 14 7 1 0 32)
            11111111111111111111111111111111
            101011 00000 00001 0000000000011000        (43 0 1 24) 
  6. What do you think would happen if you forgot to place a HALT instruction at the end of a machine-language program? How would the control unit react? Use the simulator to test your prediction, then report the results.

Writing More Assembly Language Code (Program Fragments)

Exercises 7 - 11 ask for sequences of assembly-language instructions (in other words, small program snippets rather than whole programs), so there is no need to add a HALT instruction to these exercises.

  1. Write a sequence of assembly-language instructions to cause the contents of registers R5, R6, R7, and R8 to be stored out to memory locations M24, M28, M32, and M36, respectively.
  2. Write the sequence of machine-language instructions (in decimal or binary) that corresponds to the assembly-language sequence you wrote for the previous question.
  3. Write a sequence of assembly-language instructions to add the contents of memory locations 40, 44, and 48 and then store the result in memory location 60.
  4. Write the sequence of machine-language instructions (in decimal or binary) that corresponds to the assembly-language program you wrote for the previous question.
  5. Write a sequence of assembly-language instructions that multiplies the contents of memory location 48 by four. For example if the number 10 were stored in memory location 48, executing your instructions would cause the simulator to store 40 there. Note: Although the CPU Simulator instruction set does not include a multiplication operation, a number can be multiplied via repeated additions. (Or, if you're feeling more adventurous, just as you can multiply a decimal number by 100 just by adding two 0's, you can multiply a binary number by 4 (00100) by shifting the number left by 2 digits.)