CMPS-2240 Homework 5: MIPS Encoding, Pseudoinstructions, Branching

Read:

refer to as needed:


1. MIPS is a load/store architecture. What does this mean?

2. MIPS requires that most data to be aligned. What does this mean?

3. What are the restrictions on label names in MIPS?

4. What does this assembler directive accomplish?
    arr:   .byte 'a','b','c','d','e','g' 

5. What do these assembler directives accomplish?
    mem:   .space 15 
    mem2:  .word 0:10 

6. Since MIPS instructions are all one word (4 bytes), it is easy to compute the address of the next instruction even if the instructions vary by number of operands. Give the addresses of the next two instructions after addi.
 
      Address     Instruction         
      0x00400024  addi $t2, $t3, 5   
      __________  la  $a0, mystuff
      __________  b target 

7. It is easy to encode MIPS instructions since there are so few instruction types (I, R and J). The encoding format for this type I instruction with opcode 8
          opcode  rt, rs,   imm
          addi   $t1, $zero, 13  # initialize $t1 to decimal 13 
is shown here (see Appen A pg. A-51), noting that rs and rt are reversed:
          8      rs      rt      imm  
          ====== ======  ======  =======
          6 bits 5 bits  5 bits  16 bits   = 32 bits.
Addi is opcode 8, rt is register target, rs is register source, and imm is the immediate value. What is the binary encoding of this addi instruction? (The opcode for addi is 8. See quick guide for register numbers. )

8. Pseudoinstructions are macros added to MIPS to make life easier for the programmer. The assembler translates pseudos into actual instructions; e.g., this pseudo:
        move $t1, $s3   # $t1 = $s3
is really an add operation. Provide the equivalent MIPS add. Why provide a macro for a single instruction?

9. Most pseudoinstructions are macros for multiple MIPS instructions. For example, the bgt pseudo is really three instructions: beq, slt and bne. Explain the meaning of the bgt below and translate it into its equivalent three MIPS instructions beq, slt and bne.
            bgt $t0, $s2, Label 

10. Using Appen A-10 or the quick guide as a reference, write a brief comment for each line of MIPS code below. Then explain what the code is doing in general. Assume that $s2 holds the starting address of an array of five integers 1,2,3,4,5.
 1       li    $s0, 5
 2       move  $v0, $zero
 3       move  $t0, $zero    
 4 Loop: beq   $t0, $s0, Exit 
 5       sll   $t1, $t0, 2   
 6       add   $t1, $t1, $s2   
 7       lw    $t2, 0($t1)      
 8       add   $v0, $v0, $t2      
 9       addi  $t0, $t0, 1     
10       j Loop
11 Exit: 

11. The following is the machine language of a small MIPS program.
What does the program do?

00110100000001000000000000001010
00100000100001000000000000010100
00110100000000100000000000000001
00000000000000000000000000001100



For the following 3 questions, do not write a complete program.
Write only the statements asked for.
Apply  your best handwriting  to this part of the homework.


12. Write MIPS statements that will branch to a label named BIG if the value stored in register $v0 is greater than 1-million.

13. Write MIPS statements that will branch to a label named LARGE if the value stored at the address in register $a0 is greater than 1-million.

14. Your MIPS program has an array of integers defined like so:

   .data
   arr: .space 100

Each integer is stored as a word (4-bytes).
Write MIPS statements that will branch to a label named start if the value stored in the first array element is the value 123.