I am trying to translate the C code below to MIPS assembly language, I kind of understand most of it however, I am lost as to what the equivalent of the first line is in assembly…
int ary[3] = {2,3,4};
I’d appreciate it if someone can take a look at my C to assembly ‘translation’ and verify I am on the right track.
C Code
int ary[3] = {2,3,4};
int i=0;
//loop to double array values
for(i=0; i < 3; i++){
ary[i] = ary[i]*2;
}
What I tried:
add $t0, $s0, $zero #get base address of the array 'ary' (dont understand this part)
addi $t1, baseAddress, 8 #cut off point to stop the loop; array[2]
addi $t1, $zero, $zero #initialize i=0
Start:
lw $t2, base(offset)
sll $t2, $t0, 1 #mutiply $t2 by 2
sw $t2, base(offset)
addi $t0, $t0, 4 # Increment the address to the next element
bne $t0, $t1, Start # $t0 will keep increasing until reaches stopping point $t1
Exit:
If that’s a local array, you allocate space for it on the stack, then initialize it from code.
A possible asm translation of the C code may look like:
Your asm code was taking a slightly different approach, the C version would have looked like:
And the fixed asm version for that is: