I was reading my book on MIPS and found this code in C translated in MIPS but was not sure about some line of code.
I have written my question and my understanding in red next to code. If anyone can explain me how this works, I would really appreciate it.
Code in C
void swap (int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
code in MIPS
$a0, $a1, $t0 #This would be register allocations. $a0, $a1 for v[] and k, since they are parameters & $t0 for temp.[/color]
sll $t1, $a1, 2 [color=#FF0000]1)why are we multiplying with 2. I know that a word in MIPS is 4 bytes, so for addressing we need to multiply by 4 but the code is multiplying by 2. How can we multiply by any number without knoing its actual value?
2)So how would we know if a variable (as in this case) is a multiple of 4 or not just by looking at it?
3)Also how did register $t0 become $t1?
4) Lastly, why are we shifting left? Why can't we just multiply by 4 if we need proper addressing? [/color]
add $t1, $a0, $t1
lw $t0, 0($t1)
lw $t2,, 4($t1)
sw $t2, 0($t1)
sw $t0, 4($t1)
Thanks.
This post has been edited by jingoria: 08 February 2009 - 05:15 PM

New Topic/Question
Reply




MultiQuote




|