Week 02 Tutorial Questions

Objectives

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

  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
    8. $29
    9. $31
  4. Translate the following C program into MIPS assembler and run it with 1521 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.