0

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
lw $t0,0($t1) # $t0 = A
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 < 0?
C: A[i++] >= 10 & A < 0?
D: A[i++] >= 10 || A < 0?
E: A[i++] >= 10 && A < 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?

2
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by keithh
0

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 >= 0 | A < 0 Then goto Loop

This would result in an infinite loop. Wouldn't it?

0

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.

0

Great! Thanks that's what I thought I just wanted to make sure it wasn't a trick question. I appreciate your help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.