Week 03 Tutorial Questions

Objectives

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

  2. Often when writing large MIPS programs, you will make accidental errors that cause your program to misbehave.

    Discuss what tools are available to help debug broken MIPS code.

  3. 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:

    1. $0
    2. $1
    3. $2
    4. $4
    5. $8
    6. $16
    7. $26
  4. 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;
    }
    
    Store variable x in register $t0 and store variable y in register $t1.
  5. Translate this C program so it uses goto rather than if/else.

    Then translate it to MIPS assembler.

    // Squares a number, unless its square is too big for a 32-bit integer.
    // If it is too big, prints an error message instead.
    
    #include <stdio.h>
    
    #define SQUARE_MAX 46340
    
    int main(void) {
        int x, y;
    
        printf("Enter a number: ");
        scanf("%d", &x);
    
        if (x > SQUARE_MAX) {
            printf("square too big for 32 bits\n");
        } else {
            y = x * x;
            printf("%d\n", y);
        }
    
        return 0;
    }
    
  6. Translate this C program so it uses goto rather than if/else.

    Then translate it to MIPS assembler.

    #include <stdio.h>
    
    int main(void) {
        int x;
        printf("Enter a number: ");
        scanf("%d", &x);
    
        if (x > 100 && x < 1000) {
            printf("medium\n");
        } else {
            printf("small/big\n");
        }
    }
    
    Consider this alternate version of the above program, use its approach to produce simpler MIPS assembler.
    // A simple program demonstrating how to represent a implementing an && in an
    // if-statement in MIPS.
    // This version: C, but a slightly less nice approach.
    
    #include <stdio.h>
    
    int main(void) {
        int x;
        printf("Enter a number: ");
        scanf("%d", &x);
    
        char *message = "small/big\n";
        if (x > 100 && x < 1000) {
            message = "medium";
        }
    
        printf("%s", message);
    }
    
  7. Translate this C program so it uses goto rather than if/else.

    Then translate it to MIPS assembler.

    // Print every third number from 24 to 42.
    #include <stdio.h>
    
    int main(void) {
        // This 'for' loop is effectively equivalent to a while loop.
        // i.e. it is a while loop with a counter built in.
        for (int x = 24; x < 42; x += 3) {
            printf("%d\n", x);
        }
    }
    
  8. Translate this C program so it uses goto rather than if/else.

    Then translate it to MIPS assembler.

    // Prints a right - angled triangle of asterisks, 10 rows high.
    
    #include <stdio.h>
    
    int main(void) {
        for (int i = 1; i <= 10; i++) {
            for (int j = 0; j < i; j++) {
                printf("*");
            }
            printf("\n");
        }
        return 0;
    }
    
  9. Translate this C program so it uses goto rather than if/else.

    Then translate it to MIPS assembler.

    // Simple factorial calculator - without error checking
    
    #include <stdio.h>
    
    int main(void) {
        int n;
        printf("n  = ");
        scanf("%d", &n);
    
        int fac = 1;
        for (int i = 1; i <= n; i++) {
            fac *= i;
        }
    
        printf("n! = %d\n", fac);
        return 0;
    }
    

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