Assignment #I Solution



  1. Read Section 2.13 “ A C Sort Example to Put It All Together” in the textbook to understand how the C code of swap() and sort() is translated to their respective MIPS assembly code.

  1. Read Appendix A.9 on Page A-40 about the SPIM simulator, in particular, on how to use system calls to conduct input and output.

  1. The C code for sort() in the textbook is wrong. A correct version is as follows:

for(int i = 1; i < n; i++) {

for(int j = 0; j < n – i; j++) {

if (v[j] > v[j+1])

swap(v, j);

Revise the assembly code for sort(), and insert your assembly code for sort() and swap() into the given program lab1.asm.

You need to use SPIM to debug and run the program. Note that swap() and sort() should be two separate functions in the assembly code program. You are strongly suggested to re-use the code in Fig 2.25 and Fig. 2.27 as much as possible and change them only when it is indeed necessary, such as for correcting the error in sort().