CIS 351

Lab 6: Computer Instruction Types

Winter 2022


This lab is derived from a lab designed by Prof. Wolffe.

Pre-lab

To increase your chances of having the lab complete by the end of the day (and, therefore, not having to finish it later), have questions 1-5 complete when you arrive in lab.

Overview

In this lab, you will use MARS to examine the contents of memory and CPU registers and to show the machine instructions generated for each assembly language statement.

Resources

MIPS R2000 Architecture

Let's take a closer look at the registers in this processor. The PC register is, of course, the Program Counter. Upon loading a program, the PC will be initialized to the beginning of the text segment. (In other words, the program counter holds the address of the first instruction to be executed.) Registers are designated either by their number ($0..$31) or by their name (e.g. $a0, $sp). Register $0 is hard-wired to always hold the value 0. Register $at is reserved for use by the assembler (it is often used as an address register) and the $k registers are for the use of the operating system (kernel). Registers $v0and $v1 are used for system call argument passing and return and $a0..$a3 are used to pass arguments to functions. The "t" registers are for temporary storage and the "s" registers are used for semi-permanent storage (across function calls). Finally, there is the stack pointer $sp, the global pointer $gp, and the return address holder $ra.

Download, load, and assemble exampleCIT-2.s. This program simply declares two variables named val1 and and stores the value 42 in it. The .word directive declares val to be an entire memory word in size. Knowing its value, you should now be able to find where val is stored in memory (it will be located somewhere in the data segment). Recall that all values are in hexadecimal.

  1. What is the address of the first instruction that will be executed when running the program?
  2. Where did you find this information?
  3. Which memory locations contain the value for the variable val?
  4. Diagram and label a process' address space in Mars. By this, I mean, draw a column labeled 0x00000000 at the bottom and 0xffffffff at the top. Then, show which portions of this address space are used used for instructions, user data, the stack, kernel data, etc. Hint: You will have to look in different MARS windows to find these different areas. Also, the stack grows "down", meaning that each data item added to the stack has a smaller address than the previous item.
  5. What is the maximum program size for this configuration? (In other words, how many instructions can your program have before they overflow into data memory?)

Running a Program

Load and compile exampleCIT-3.s file. The run button will cause execution of your program.  However, you will usually want to step through you program one instruction at a time - to do this use the step button (an arrow with a "1" on it). Watch the value of the Program Counter as you step through the code.

Examining instructions

Closely examine the instructions corresponding to source code lines 10 through 13. Normally, each assembly language instruction corresponds to one machine-language instruction. In this case, the second addi and the two lw assembly instructions are broken into multiple machine-language instructions. In the case of the lw, the first instruction (lui) loads a value into register $1 (the(at) register - recall its use).  The second instruction actually performs the desired data transfer.

  1. What lines of code does the addi pseudo-instruction on line 11 produce? (Look in the "Basic" column in the Execute tab.)
  2. What does "lui" stand for? (Hint: Look on page A-57 in the SPIM guide.)
  3. Why do the two addi instructions result in different numbers of actual instructions? (Hint: Write each constant out in hex.)
  4. Explain how the designers of the MIPS CPU applied the "Make the common case fast" principle when designing the addi instruction.
  5. What is the hex representation of the "immediate" parameter to the lui instruction generated for line 13?
  6. Why is this the immediate value that is used (i.e., what does it represent)? Hint: Look in the Data Segment window.
  7. Now, look at the second machine instruction generated for line 13 (the second lw instruction). Notice that this instruction has three parameters. Describe the function of all three parameters. (Hint: Examine the description of lw on page A-67.)
  8. Are all three parameters necessary in order for lw to be able to access the entire 4GB memory space; or, could you eliminate the offset parameter? Imagine a hypothetical lw instruction that did not have an offset parameter (the constant). Would there be memory locations that could not be accessed using this hypothetical lw?
  9. Explain why the three-parameter version of lw is useful. Include an explanation of how can it be used to "make the common case fast." (In other words, how it can be used to reduce the number of instructions needed by the program.) (Hint: Look for redundant code in the execute window for exampleCIT-3.s.)
  10. Explain the cost of the three-parameter version of lw. In particular, include an explanation of how the third (i.e., offset) parameter can potentially slow the computer (as compared to the hypothetical two-parameter lw).

Now, load add_xy.s, which requests two integers from the user and prints their sum. Run and review this code until you remember / learn how it works. The system calls for performing I/O are discussed on page A-43 of the SPIM guide.

  1. How is the li pseudo-instruction implemented? In other words, which "real" instructions are used to implement the li pseudo-instruction? (Remember, register 0 always contains the value 0.)
  2. How does MIPS implement the move pseudo-instruction?
  3. Would a built-in move be faster than the MIPS implementation? Why or why not? Consider the effects on both the time for the individual instruction, and the overall speed of the processor.

Instruction formats

  1. For each instruction in add_xy.s marked with a "*" in the comment (17, 18, 29, 30, 41, 42, and 72) complete a table showing how the assembly-language instruction is mapped into a machine-language instruction. For pseudo-instructions, create one table for each machine instruction produced by the assembler. You may use this template for your tables. You may find this reference card helpful. Use the example below as a model. After filling out the table, double-check that the values in the table make sense given the instruction's parameters.
    Assembly instruction add $s2, $s0, $s1
    Machine instruction (hex) 0x02119020
    Machine instruction (binary) 000000 10000 10001 10010 00000 100000
    Instruction field (decimal) 0 16 17 18 0 32
    Field function opcode rs rt rd unused function
  2. Why does the la pseudo-instruction in line 17 generate two assembly instructions while the li pseudo instruction in line 18 generates only one?
  3. What is the value of the immediate parameter for the beq instruction on line 30?
  4. Where does this number come from (i.e., how does the assembler calculate it)?
  5. What is the value of the immediate parameter for the j instruction on line 72? Be careful, you need to look at the actual hex value of the instruction, not the number in the "Basic" column.
  6. Where does this number come from (i.e., how does the assembler calculate it)?

Submit the answers to questions 1 through 24.


Updated Tuesday, 8 February 2022, 2:58 PM

W3c Validation