Exercise 3 -- Procedures

Writing a procedure

  1. In file p7.asm, write a procedure Sum that accepts two integers �a� and �c� as input parameters and returns the sum of the two values.
  2. Follow MIPS register conventions.
  3. After you have finished your procedure, open p7_partial.asm. Copy the main procedure and the data segment to p7.asm.
  4. Run your program. Does it behave as you expect it to?
  5. Fix your procedure, so that the program works if required. You are NOT allowed to modify the data section or �main�. Again, keep in mind that using MIPS register conventions will fix your problem.

 

Sorting an Array

  1. Modify your version of p5.asm from Exercise 2 so that it is a procedure named SwapMaxWithLast which takes 2 arguments - the location (address) of an array of words in memory and the length (in words) of the array. Be sure your code conforms with the MIPS procedure calling conventions (see pages A-22-A33 of your text).
  2. Download p8.asm to your local hard disk and examine it using your favorite text editor.
  3. Add your procedure to the bottom of p8.asm in the location indicated.
  4. Modify main so that it calls SwapMaxWithLast a single time with A and N as arguments. What output do you expect when you run p8.asm?
  5. Start PCSPIM and load your modified version of p8.asm.
  6. Run p8.asm. Is the actual output what you expected?
  7. Modify main so that it calls SwapMaxWithLast N-1 times and with each successive call the length of the array passed is decreased by 1.
    In pseudocode:

� �for (length=N; length>1; length--)

�� {���

�� � �SwapMaxWithLast(length, A);

�� }

What output do you expect when you run p8.asm?

  1. Run p8.asm. Is the actual output what you expected?