Monash University > School of Computer Science and Software Engineering > CSE1303 > Part B > Tutorials > Tutorial B4

CSE1303 Computer Science
Semester 3 (summer), 2003
Part B
Tutorial B4

This tute covers material from lectures B10 to B11.

Attempt the questions marked (*) like this before the tutorial. If you have specific questions about unmarked questions, you can ask the tutor about them during the tutorial.

  1. Arithmetic

    1. Write the following infix expressions in Reverse Polish Notation. Write "get-X" if a variable X is read from, and "put-X" if it is assigned to.
      1. (*) 2 * 3
      2. (*) 1 + 2 * 3
      3. 2 * 3 + 1
      4. C = 5 * (F - 32) / 9
      5. (*) F = C * 9 / 5 + 32
      6. n * (n + 1) / 2
      7. s = (L >> 31) & 1
    2. Write MIPS code to evaluate the following expressions (the same as above). Assume that all variables are 32-bit signed globals. Try very hard to conserve register usage; that is, don't use $t2 unless $t0 and $t1 contain values you need, and re-use registers at the first possible opportunity. Remember that you can use a source register as the destination register in the same instruction. Avoid using immediate arithmetic instructions; instead, place the immediate value in a register with the li instruction.
      1. (*) 2 * 3
      2. (*) 1 + 2 * 3
      3. 2 * 3 + 1
      4. C = 5 * (F - 32) / 9
      5. (*) F = C * 9 / 5 + 32
      6. n * (n + 1) / 2
      7. s = (L >> 31) & 1
    3. (*) Compare your answers to the above two questions. Take special note of the order of operations for a given expression. Fill in the following table mapping Reverse Polish operations with the corresponding MIPS opcodes.
      RPN operationMIPS opcode
      +add
      -sub
      * 
      / 
      number 
      get-var 
      put-var 
    4. (*) Examine the MIPS code versions of your expressions. Look at the three-register arithmetic instructions (add, mul, etc.). What do you notice about the two source registers? What do you notice about the destination register?
    5. (*) Suggest an algorithm which, given a Reverse Polish Notation expression, can produce MIPS code that can compute it.
  2. Input/output

    1. (*) Write a MIPS program which prints your name (a constant string), then exits.
    2. (*) Write a MIPS program which reads a number from the keyboard, then prints the equivalent negative number.
    3. (*) Consider the following line of C. Write MIPS code that does the same thing. Assume that n is a global variable.
      printf("The answer is %d.\n", n);
  3. Memory

    1. Draw memory diagrams corresponding to these C declarations. Assume that all variables are global, and that int is the same size as long.
      1. (*) 
        int x = 5;
        int y;
        
      2. (*) 
        int a[4] = { 0, 1, 4, 9 };
        
      3. char str[16] = "String example";
        
      4. char *str = "String example";
        
    2. Draw memory diagrams corresponding to these C declarations. Assume that all variables are local, and that int is the same size as long. Show the values of $sp and $fp and where they point. Assume that the value of $sp before the function begins is 0x7FFF0000.
      1. (*) 
        int x = 5;
        int y;
        
      2. (*) 
        int i = 42;
        int j = 104;
        int *iptr = &i;
        
      3. int a[4] = { 0, 1, 4, 9 };
        
  4. Instructions

    1. Consider the following instruction:
      la $t0, -8($fp)

      Explain why this la pseudoinstruction is equivalent to this addiu instruction:

      addiu $t0, $fp, -8
  5. Local variables

    1. (*) Translate the following C code into MIPS assembly language. Try to make your translation as faithful as possible; that is, do not try to optimize the code.
      #include <stdio.h>
      #include <stdlib.h>
      
      int main() {
          int a;
          int b;
          int q;
          int r;
      
          puts("Enter two numbers: ");
          scanf("%d", &a);
          scanf("%d", &b);
      
          q = a / b;
          r = a % b;
      
          printf("Quotient is %d\n", q);
          printf("Remainder is %d\n", r);
      
          exit(0);
      }
      

[ Top | Home ]

Last modified 2002-07-03