Week 04 Tutorial Answers

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

    All four are MIPS emulators which simulate the execution of a MIPS CPU and let you run MIPS assembler on any computer.

    mipsy has been developed at CSE by Zac Kologlu - and it designed to give students learning MIPS assembler extra help.

    mipsy is the best emulator to use to develop and debug your assembler programs.

    mipsy is very new so if you find bugs - please report in the course forum.

    spim is a similar (much older) program. You probably need to use spim for this weeks second challenge exercise because mipsy might not (yet) work for the unusual things you need to do in it.

    qtspim & xspim are GUI-based versions of spim. They show you the value in all registers and the state of memory which is helpful when debugging. Students usually prefer qtspim

    We hope to deploy a web-based version of mipsy in time for you to use this term. It should be better then 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.