The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Other Programming Languages
|
MIPS finding the largest two integers of 3 inputs
Discuss MIPS finding the largest two integers of 3 inputs in the Other Programming Languages forum on Dev Shed. MIPS finding the largest two integers of 3 inputs A place for discussing programming languages not covered in specific forums such as Assembler, COBOL, etc. - you get the idea.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 24th, 2011, 12:54 PM
|
|
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
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
|
 |
Lost in code
|
|
|
|
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
|

October 24th, 2011, 06:50 PM
|
|
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
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 "
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|