0
votes

as part of a homework assignment we're required to convert the following code snippet from C to MIPS Assembly:

for (i=0; i<A; i++) { 
    for (j=0; j<B; j++) {  
        C[4 * j] = i + j;  
        }  
} 

I believe I have the logic down and most of the code is correct. I'm just stuck at the line: D[4 * j] = i + j;. I know how to get the sum of i and j, but I'm not sure how to actually put it into a multiple of its j address. Could someone please tell me what I'm missing in loop2? Thanks

.data
A: .word 2
B: .word 5
D: .word 0 : 100
size: .word 100

.text
    .globl  main
main:

    lw, $s0, A      #$s0 = A
    addi $s0, $s0, -1   #Allows for loop to properly run
    lw $s1, B       #$s1 = B
    addi $s1, $s1, -1   #Allows for loop to properly run
        la $s5, C           # load address of array
        li $t0, 0       #i = 0
        li $t1, 0       #j = 0
        
loop1:
    
    blt $s0, $t0, Exit  #for(i = 0; i < A; i++)
    addi $t0, $t0, 1    #i++
    li $t1, 0       #Sets j to 0 before calling loop2
    j loop2         #Calls loop2

loop2:

    blt $s0, $t0, loop1 #for(j = 0; j < B; j++)
    addi $t2, $t0, -1   #Allows to properly perform loop
    {Where I'm confused what to do}

    
    
    {Last two steps of loop2}
    addi $t1, $t1, 1    #j++
    j loop2
    
Exit:
      # The program is finished. Exit.
      li   $v0, 10          # system call for exit
      syscall               # Exit!
blt $s0, $t0, loop1 is wrong because you want $s1 for B. addi $t2, $t0, -1 is useless as $t2is not mentioned anywhere else. As to your question remember that A[x] is just *(A+x) and that you need to scale by item size. - Jester