Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
October 24th, 2011, 12:54 PM
 kevinj1977
Registered User

Join Date: Oct 2011
Posts: 2
Time spent in forums: 1 h 44 m 27 sec
Reputation Power: 0
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
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 "```

#2
October 24th, 2011, 06:03 PM
 E-Oreo
Lost in code

Join Date: Dec 2004
Posts: 7,931
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
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
How to program a basic, secure login system using PHP

Quote:
 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

#3
October 24th, 2011, 06:50 PM
 kevinj1977
Registered User

Join Date: Oct 2011
Posts: 2
Time spent in forums: 1 h 44 m 27 sec
Reputation Power: 0
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
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 "```

 Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > MIPS finding the largest two integers of 3 inputs