So I have an assignment problem which asks for a for loop in C to be converted into MIPS. My professor moves extremely fast so I couldn't catch half of what he said. Here is the code:
[B]for (i=0; i<10; i++)[/B][B]{
a[i] = b[i] + c[i];
}
This fragment is stored in memory starting from location 00000100 Hex.
Convert this code to MIPS and provide numeric offsets for each branch or jump instruction used.
[/B]
I don't quite understand the use of offsets. From the lecture slides given to us, it seems load word and store word commands are the only ones that need offset, so do I need them in this case? Below is something I put together. I'm hoping it's at least going in the right direction. Any help would be appreciated.
# a in $s0
# b in $s1
# c in $s2
# i in $t0
Loop:
bgt $t0,9,exit #loop from 0 to 9
addi $t0,$t0,1 #increment i
add $s0,$s1,$s2 #add b and c and store into a
j loop
Exit:
Perhaps he wants you to identify the locations of Loop and Exit relative to the locations of the instructions that target them. Did he go over how long instructions are, measured in bits?
Seems like instructions are 32 bits ([URL="https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/pseudojump.html"]with the exception of pseudoinstructions[/URL], like your "bgt"), so on that first branch instruction, you could add a comment like "if t0>9, branch to +12 bytes".
I'm not REALLY sure this is what he's looking for.
Yes the instructions are 32 bits. Thanks for suggestions. I'm not sure exactly what he wants unfortunately but this will have to do for now.
From my understanding, $s0-$s2 contains the BASE ADDRESS for array A,B,C.
For each itheration, You need to:
-fetch the content at each address first
-add them together and store it back into a's current address
-increment the addresses (+4) for all of a, b, c for the next iteration.
Your code currently does none of that
In terms of fragment, you need to ask your prof to clarify what he meant by that. It could either the addresses to the next instructions, or the arrays themselves.
[QUOTE=aNTohnEE;49809078]So I have an assignment problem which asks for a for loop in C to be converted into MIPS. My professor moves extremely fast so I couldn't catch half of what he said. Here is the code:
[B]for (i=0; i<10; i++)[/B][B]{
a[i] = b[i] + c[i];
}
This fragment is stored in memory starting from location 00000100 Hex.
Convert this code to MIPS and provide numeric offsets for each branch or jump instruction used.
[/B]
I don't quite understand the use of offsets. From the lecture slides given to us, it seems load word and store word commands are the only ones that need offset, so do I need them in this case? Below is something I put together. I'm hoping it's at least going in the right direction. Any help would be appreciated.
# a in $s0
# b in $s1
# c in $s2
# i in $t0
Loop:
bgt $t0,9,exit #loop from 0 to 9
addi $t0,$t0,1 #increment i
add $s0,$s1,$s2 #add b and c and store into a
j loop
Exit:[/QUOTE]
I'd like to conjecture here, but without his slides I might just fuck it up. Still, here's my take.
<Rewriting this motherfucker here, just had a better read of your post. Hold on.>
[code]#It's important to note here that the 4-byte allocation I chose wasn't deliberate,
#since the compiler I'm using aligns everything into 32bit addresses. This allows me
#to avoid needing more temporaries to get addresses of everything
.data
a: .byte 0, 0, 0, 0 #0..3
b: .byte 'a', 'b', 'c', 'd' #4..7
c: .byte 3, 3, 3, 3 #8..11
.text
main:
addi $t0, $0, 4 #tmp0 = 4
addi $t1, $0, 0 #tmp1 = 0
la $t2, a #tmp2 = &a[0]
beq $t0, $t1, 44 #while (tmp0 != tmp1) //branch forwards 44 bytes when tmp0 == tmp1, leaving the loop
nop #/*branch delay slot*/ {
lb $t3, 4($t2) # tmp3 = b[i] //more accurately, tmp3 = *((byte*)tmp2+3), where &b[0] "is" the immediate '4' (offset from a to b)
lb $t4, 8($t2) # tmp4 = c[i] //more accurately, tmp3 = *((byte*)tmp2+6), where &c[0] "is" the immediate '8' (offset from a to c)
nop #/*load delay slot*/
add $t5, $t3, $t4 # tmp5 = tmp3+tmp4
sb $t5, 0($t2) # a[i] = tmp5 //more accurately, *((byte*)tmp2+0) = tmp5, where &a[0] "is" the immediate '0' (offset from a to a)
addi $t1, $t1, 1 # tmp1++
addi $t2, $t2, 1 # tmp2 += 1 (1 for 8bit, 2 for 16bit, etc..)
beq $0, $0, -40 #} //branch backwards 40 bytes when 0 == 0, iterating once. You should use j here, but your lecturer wants relative addresses??
[/code]
[editline]28th February 2016[/editline]
I'm out for lunch, I'll tackle the changes later.
Sorry, you need to Log In to post a reply to this thread.