Week 04 Tutorial Questions

  1. What are mipsy, spim, qtspim & xspim.

  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:

    1. $0
    2. $1
    3. $2
    4. $4
    5. $8
    6. $16
    7. $26
    8. $29
    9. $31
  3. Translate this C program to MIPS assembler
    // print 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.
  4. 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, y;
        printf("Enter a number: ");
        scanf("%d", &x);
    
        if (x > 46340) {
            printf("square too big for 32 bits\n");
        } else {
            y = x * x;
            printf("%d\n", y);
        }
    
        return 0;
    }
    
  5. 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.
    #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);
    }
    
  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) {
        for (int x = 24; x < 42; x += 3) {
            printf("%d\n",x);
        }
    }
    
  7. Translate this C program so it uses goto rather than if/else.

    Then translate it to MIPS assembler.

    // print a triangle
    #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;
    }
    
  8. 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.