October 24th, 2011, 12:54 PM

MIPS finding the largest two integers of 3 inputs
Hi folks,
I am playing with some MIPS assembly language and am stuck on how to find the largest of 3 integers entered. I have figured out how to compare the first 2 with the branch instructions but I am unsure of the most efficient way to compare all 3. Here is what I had put together for code. The bge calls are where I am trying to figure out the best way to compare all 3. the first bge compares the first 2. Anyone have any insight?
Code:
.text
.align 2
.globl main
main:
# this program prints out the largest two of three numbers input
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5 # read keyboard into $v0 (number x is number to test)
syscall
move $t0,$v0 # first number in $t0
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5 # read keyboard into $v0 (number x is number to test)
syscall
move $t1,$v0 # second number in $t1
li $v0, 4
la $a0, prompt3
syscall
li $v0, 5 # read keyboard into $v0 (number x is number to test)
syscall
move $t2,$v0 # third number in $t2
bge $t1, $t0, L1
move $t1, $t0 # largest number in $t1
bge $t2, $t1, L1
move $t2, $t1
L1:
li $v0, 4 # print answer
la $a0, answer
syscall
li $v0, 1 # print integer function call 1
move $a0, $t1 # integer to print
syscall
end: jr $ra
.data
prompt1: .asciiz "Enter the first number "
prompt2: .asciiz "Enter the second number "
prompt3: .asciiz "Enter the third number "
answer: .asciiz "The largest number is "
October 24th, 2011, 06:03 PM

Code:
# effectively these two lines do: $t1 = max($t0, $t1)
bge $t1, $t0, CMP2
move $t1, $t0
CMP2:
# effectively these two lines do: $t1 = max($t2, $t1)
bge $t1, $t2, L1
move $t1, $t2
PHP FAQ
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
October 24th, 2011, 06:50 PM

Kudos to you. I have updated the code to reflect your suggestion and tested it successfully. Makes sense to me now. The next piece I am interested in adding would be to find the 2 largest of the 3 integers entered. My thoughts would be to add another CMP3 statement and compare them again minus the largest we've already identified and stored? Would a sort type statement work better than doing these separately? Here is what the updated code looks like after adding your suggestion:
Code:
.text
.align 2
.globl main
main: # this program prints out the largest two of three numbers input
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5 # read keyboard into $v0 (number x is number to test)
syscall move $t0,$v0 # first number in $t0
li $v0, 4 la $a0, prompt2
syscall
li $v0, 5 # read keyboard into $v0 (number x is number to test)
syscall
move $t1,$v0 # second number in $t1
li $v0, 4 la $a0, prompt3
syscall
li $v0, 5 # read keyboard into $v0 (number x is number to test)
syscall
move $t2,$v0 # third number in $t2
bge $t1, $t0, CMP2
move $t1, $t0 # largest number in $t1
CMP2:
bge $t1, $t2, L1
move $t1, $t2
L1:
li $v0, 4 # print answer
la $a0, answer
syscall
li $v0, 1 # print integer function call 1
move $a0, $t1 # integer to print
syscall
end: jr $ra
.data
prompt1: .asciiz "Enter the first number "
prompt2: .asciiz "Enter the second number "
prompt3: .asciiz "Enter the third number "
answer: .asciiz "The largest number is "