Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 24th, 2011, 12:54 PM
kevinj1977 kevinj1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 2 kevinj1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 "

Reply With Quote
  #2  
Old October 24th, 2011, 06:03 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
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

Reply With Quote
  #3  
Old October 24th, 2011, 06:50 PM
kevinj1977 kevinj1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 2 kevinj1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 "

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > MIPS finding the largest two integers of 3 inputs

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap