Week 03 Tutorial Questions

Objectives

  1. What is mipsy (and mipsy_web), and what do we use them for in DPST1092?

  2. The MIPS processor has 32 general purpose 32-bit registers, referenced as $0 .. $31. Some of these registers are intended to be used in particular ways by programmers and by the system. For each of the registers below, give their symbolic name and describe their intended use:

  3. Write a MIPS program that: 1. Loads the number 5 into register $t0. 2. Loads the number 7 into register $t1. 3. Adds the two numbers and stores the result in $t2. 4. Prints the result with a new line character '\n'. The program should print:
    12
    Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  4. Write a MIPS program that reads an integer from the user and then prints the integer followed by a new line character. The C equivalent is:
    #include<stdio.h>
    
    int main(void) {
        int x;
    
        scanf("%d", &x);
        printf("%d\n", x);
    
        return 0;
    }
    
    Your MIPS program should behave in the same way. For example:
    42
    42
    
    Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  5. Write a MIPS program that reads an integer from the user, doubles the integer, and then prints the result followed by a new line character. The C equivalent is:
    int main(void) {
        int x;
    
        scanf("%d", &x);
        x = x * 2;
        printf("%d\n", x);
    
        return 0;
    }
    
    For example, if the user enters:
    6
    
    The program should print:
    12
    
    Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  6. Write a MIPS program that defines an integer in the .data section, loads the integer from memory using lw, and then prints the integer followed by a new line character. The C equivalent is:
    #include<stdio.h>
    
    int x = 42;
    
    int main(void) {
        printf("%d\n", x);
    
        return 0;
    }
    
    Your MIPS program should store the integer in the .data section and use lw to load its value into a register before printing it. Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  7. Write a MIPS program that defines an integer in the .data section. Load the integer from memory, add 10 to it, and then store the new value back into memory using sw. Finally, load the value from memory again and print it followed by a new line character. The C equivalent is:
    int x = 10;
    
    int main(void) {
        x = x + 10;
        printf("%d\n", x);
    
        return 0;
    }
    
    Your MIPS program must use both lw and sw. Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  8. Write a MIPS program that swaps the values of two integers and then prints the two values, each followed by a new line character. The C equivalent is:
    int x = 10;
    int y = 20;
    
    int main(void) {
        int temp;
    
        temp = x;
        x = y;
        y = temp;
    
        printf("%d\n", x);
        printf("%d\n", y);
    
        return 0;
    }
    
    Your MIPS program should use the .data section to store the two integers and a temporary variable. Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  9. Write a MIPS program that defines two integers in the .data section, loads both integers using lw, adds them together, and prints the result followed by a new line character. The C equivalent is:
    int x = 10;
    int y = 20;
    
    int main(void) {
        printf("%d\n", x + y);
    
        return 0;
    }
    
    Your MIPS program must use lw to load both integers from memory. Run your program with 1092 mipsy. Do the answer slowly and step by step. Have a plan before you start the question.
  10. Write a MIPS program that prints Hello, world! and run it with 1092 mipsy.
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, world!\n");// this is going to be 2 syscalls
    }
    
    Do the answer slowly and step by step. Have a plan before you start the question.
  11. Translate the following C program into MIPS assembler and run it with 1092 mipsy.
    // Prints the square of a number
    
    #include <stdio.h>
    
    int main(void) {
        int x, y;
        printf("Enter a number: ");
        scanf("%d", &x);
    
        y = x * x;
    
        printf("%d\n", y);
    
        return 0;
    }
    
    Do the answer slowly and step by step. Have a plan before you start the question. Store variable x in register $t0 and store variable y in register $t1.
  12. Translate the following C program into MIPS assembler and run it with 1092 mipsy.
    #include<stdio.h>
    
    int main(){
        int x, y, z;
        printf("Enter the first number: ");
        scanf("%d", &x);
        printf("Enter the second number: ");
        scanf("%d", &y);
        printf("Enter the third number: ");
        scanf("%d", &z);
    
        int result = ((((x<<2) + y)>>1)+z);
    
        printf("%d\n", result);
        return 0;
    }
    
    Do the answer slowly and step by step. Have a plan before you start the question.
  13. Write MIPS code to take an 8-bit value (given by the user using a syscall). Then, set the middle 4 bits to 1 (bits 2 to 5, counting from the least significant bit as bit 0).

    Leave the other bits unchanged, and print the result to standard output. In order to do this we need to create the following mask 0b00111100 We learned two ways to create this mask. Use the following C code as a guide on how to create a mask.

    #include<stdio.h>
    
    int main(){
        int mask = 0x3c;
    	//OR the following method
        int mask = 1; //0000 0001
    	mask = mask << 4; //0001 0000
    	mask = mask - 1; // 0000 1111
    	mask = mask << 2; // 0011 1100
    }
    

Revision questions

The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.

  1. Write a MIPS program that is equivalent to the following c program
    int main(void) {
        int a;
        printf("Enter a: ");
        scanf("%d", &a);
    
        printf("%d is a ", a);
        if (a < 0) {
            if (a < -100) {
                printf("big");
            } else {
                printf("small");
            }
            printf(" negative");
        } else {
            if (a < 100) {
                printf("big");
            } else {
                printf("small");
            }
            printf(" positive");
        }
        printf(" number.\n");
    
        return 0;
    }
    
  2. Write a MIPS program that is equivalent to the following c program
    int main(void){
        int i, n;
        scanf("%d",&n);
        
        i = 0;
        while ( i < n ){
            printf("%d\n",i);
            i++;
        }
        return 0;
    }
    
  3. Write a MIPS program that is equivalent to the following c program
    int main(void){
        int  start, end;
        scanf("%d",&start);
        scanf("%d",&end);
          
        while ( start <= end ){
            printf("%d\n",start);
            start++;
        }
        return 0;
    }
    
  4. Write a MIPS program that is equivalent to the following c program
    int main(void) {
        int sum, n;
    
        sum = 0;
        scanf("%d", &n);
        while (n >= 0) {
            sum += n;
            scanf("%d",&n);
        }
    
        printf("Sum of the numbers is %d\n", sum);
        return 0;
    }
    
  5. Write MIPS assembler to evaluate the following C expression, leaving the result in register $v0.
    ((x*x + y*y) - x*y) * z
    

    Then try to write one version that minimises the number of instructions and another version that minimises the number of registers used (without using temporary memory locations).

    Assume that: all variables are in labelled locations in the .data segment; the labels are the same as the C variable names; all results fit in a 32-bit register (i.e. no need to explicitly use Hi and Lo).