CS 340 Assignment 4

Assignment

As a programming exercise, translate the following Java method into MIPS assembly. Your top-level code should pass the base address of the array and its size to Max, and max will do all of the input and output before returning. (In Java, we wouldn't have to pass the size along with an array, since arrays provide a length field, but we need to pass it here since MIPS arrays don't know how long they are.)

float data[] = {`9.24, 1.15, 3.91, -2.30, 7.43, 5.79, 6.83, 3.14`}; int size = 8;

/**

  • Max is passed the base address of the array of floats and
  • its size. It prompts the user for the number of elements from
  • the array to inspect, it then finds the maximum value from in that * range, and prints the result.
{`

*/

public static void max(float nums[], int size) { Scanner scan = new Scanner(System.in);

System.out.println("How many should be inspected?"); int n = scan.nextInt();

if (n > size || size < 0){ // don't inspect more than there are

n = size;}

float max = nums[0]; for(int i=0; i<n; i++){ if(max < nums[i]){ max = nums[i];

}

}

System.out.println("The max number is " + max); }

`}

The floating-point values should be built into your program, just as they're built into the program above. You can use the .float directive to store the array values in the same

http://personal.stthomas.edu/sawi0750/classes/340/Asmts/04/index.html Page 1 of 2

CS 340 Assignment #4 4/4/16, 1:53 PM manner that you've stored integer arrays into memory with .word. You should store a .word representing size as well.

Your program will work with a combination of floating-point and integer values. Recall that all floating-point values must be kept in $f registers. You can move values from the CPU registers to $f registers via the mtc1 src, dst instruction ("move to coprocessor 1"). The src register can be any of the CPU registers, and the destination must be a $f register. Note that moving an integer from the CPU into a floating-point register does not convert it to floating-point representation. You can use cvt.s.w dst, src to convert integers to floats, but both the src and dst must be $f registers. (No, that's not a typo — the two instructions take their source and destination in the opposite order.) There's also a syscall for displaying floating-point values. See the MARS documentation or Appendix B for more details.

Of course, our program should observe all MIPS procedure calling conventions and the max procedure should properly maintain the stack.