MIPS Question

Thread Solved

Join Date: Feb 2007
Posts: 3
Reputation: keithh is an unknown quantity at this point 
Solved Threads: 0
keithh keithh is offline Offline
Newbie Poster

MIPS Question

 
0
  #1
Feb 4th, 2007
So here's the question that I've been thinking over for a few hours:

MIPS to C. Assume $s3 = i, $s4 = j, $s5 = @A. Below is the MIPS code:
Loop: addi $s4,$s4,1 # j = j + 1?
add $t1,$s3,$s3 # $t1 = 2 * i
add $t1,$t1,$t1 # $t1 = 4 * i
add $t1,$t1,$s5 # $t1 = @ A[i]
lw $t0,0($t1) # $t0 = A[i]
addi $s3,$s3,4 # i = i + 1?
slti $t1,$t0,10 # $t1 = $t0 < 10?
beq $t0,$0, Loop # goto Loop if >=
slti $t1,$t0, 0 # $t1 = $t0 < 0?
bne $t0,$0, Loop # goto Loop if <
Below is part of the corresponding C code:
do j = j + 1
while (______);
What C code properly fills in the blank in loop on right?
A: [i++] >= 10?
B: A[i++] >= 10 | A[i] < 0?
C: A[i++] >= 10 & A[i] < 0?
D: A[i++] >= 10 || A[i] < 0?
E: A[i++] >= 10 && A[i] < 0?
F: None of the above

I am in my first couple of weeks in a computer architecture class so I am reletively new at MIPS. But this little bit of code that I've bolded seems incorrect. If you use the slti instruction to set $t1 to either a 1 or 0 based off of $t0's value wouldn't you then use the beq instruction based off of $t1 not $t0 like the code is doing? It seems pointless to set $t1 if you aren't going to use it for something. Is there some sort of technique being applied here that I'm not seeing?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: keithh is an unknown quantity at this point 
Solved Threads: 0
keithh keithh is offline Offline
Newbie Poster

Re: MIPS Question

 
0
  #2
Feb 4th, 2007
Since the two slti instructions seem to be irrelevant then I would want to look at the beq and bne statements that say:
if A[i] >= 0 | A[i] < 0 Then goto Loop

This would result in an infinite loop. Wouldn't it?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 39
Reputation: MacGyver Orca is an unknown quantity at this point 
Solved Threads: 4
MacGyver Orca's Avatar
MacGyver Orca MacGyver Orca is offline Offline
Light Poster

Re: MIPS Question

 
0
  #3
Feb 8th, 2007
Originally Posted by keithh View Post
So here's the question that I've been thinking over slti $t1,$t0,10 # $t1 = $t0 < 10?
beq $t0,$0, Loop # goto Loop if >=
slti $t1,$t0, 0 # $t1 = $t0 < 0?
bne $t0,$0, Loop # goto Loop if <
First off, I don't know C, or how to type, I suck at it, but I digress, I do know MIPS. basically, your first beq is checking the wrong register, it should be comparing $t1, since slti instructions translate to this: $t1 = $t0 < 10, where the result is a one or a zero based off whether the set less than was true or not, so essentially a boolean. Second, $0 doesn't exist, the register you're trying to use is $zero, that is the register with a hard-coded value of zero, which you will use a lot for beq's or bne's. if you have anymore questions feel free to ask.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: keithh is an unknown quantity at this point 
Solved Threads: 0
keithh keithh is offline Offline
Newbie Poster

Re: MIPS Question

 
0
  #4
Feb 8th, 2007
Great! Thanks that's what I thought I just wanted to make sure it wasn't a trick question. I appreciate your help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC