Home > Mobile >  Finding the largest element in an array in MIPS
Finding the largest element in an array in MIPS

Time:10-07

I'm supposed to write a MIPS program that first takes an input number, then asks for that many of ints and stores them in an array. After this, it prints the array in reverse, then prints the largest element. The trick is that I am only allowed to use two loops. I used my first loop for building the array, and the next one I am using to print the array in reverse while simultaneously checking for the largest int. The code I have currently does the first two requirements perfectly,

It will take input such as:

5 <- Number of elements in the array

0

1

3

2

5

Then, it will print:

The array in reverse is: 5, 2, 3, 1, 0

Thank you and have a nice day!

I still have not coded in the portion of the second loop that calculates the largest int. I simply cannot figure out how to do this in MIPS without messing up what I have already done for printing the array in reverse.

The truly desired output would be:

The array in reverse is: 5, 2, 3, 1, 0

The largest element is: 5

Thank you and have a nice day!

Here is the code that I have so far: mipsCode

Any samples of how this can be done, whether working with my code or not would be greatly appreciated. Thanks!

CodePudding user response:

  1. Allocate an integer variable and set it to zero, call it max_value.
  2. As you print out the list in reverse, compare each value with with max_value
  3. If it's > max_value, store it in max_value

When all the integers have been printed out, max_value will contain the largest you found.

CMP will do the comparison and the carry flag will be set if A > B so you can do a branch on carry set/clear to skip the replace code.

  • Related