Reputation: 153
Here's the C code:
int A[10];
int sum = 0;
int i = 0;
while (i<10){
sum += A[i++];
sum *= 2;
}
Here's my take at converting into MIPS:
**Reg. Allocation Table:
A = $s1
sum = $s2
i = $s3
10 = $s4**
loop: beq $s3, $s4, endloop
"here's where i get stuck, inside the while loop."
j: loop
endloop:
I understand that in a basic while loop such as: i = $s1
, 5 = $s3
i=0;
while(i != 5)
i=i+1;
addi $s1, $zero, 0 #i=0
loop: beq $s1, $s3, endloop
add $s1, $s1, 1
j loop:
endloop:
I'm just having trouble, or having a hard time comprehending a slightly more difficult loop where sum+= A[i++];
and sum*= 2;
are introduced.
Any help is greatly appreciated. I'm not looking for a complete solution, so please help me think.
Thank you!
Upvotes: 3
Views: 10222
Reputation: 4961
Break it down:
sum += A[i++];
becomes sum = sum + A[i]; i = i + 1;
which in MIPS assembly could be expressed:
add $t0 $s3 $s1 #index A at i
lw $t1 0($t0) #load A at i
add $s2 $s2 $t1 #add A[i] to sum
addi $s3 $s3 1 #increment i
sll $s2 $s2 1 #double sum
Upvotes: 4