punjabi4life
punjabi4life

Reputation: 705

C to MIPS conversion. Array load word offset?

I have a C code that I need help with in MIPS. The C code is as follows.

for (i=0; i<100; i++){
   a[i] = b[i] + c[i] - d[i];
}

I have converted this to MIPS but don't know what to put into the offset of load word.

addi $t0, $zero, 0                #i = 0

for_loop:
   bgt $t0, 100, for_loop_done     #i <100
   addi $t0, $t0, 1               #i++ or i = i+1


   lw $t4, __($s0)              # load a in t4
   lw $t1, __($s1)              # load b in t1
   lw $t2, __($s2)              # load c in t2
   add $t4, $t2, $t1                 # add b with c and store in a
   lw $t3, __($s3)              # load d in t3
   sub $t4, $t3, $t4                 # sub contents of a from d
   sw $t4, __($s0)              # store contents of t4 into a

   j for_loop                     # go to start of loop

for_loop_done:

We assume that a,b,c,d are in s0,s1,s2,s3,s4 respectively. Now what the code needs is how we can offset the load word and store word with the ever changing 'i' from the c code. Because as far as I know load word only uses a static value.

Upvotes: 1

Views: 2840

Answers (1)

user7583029
user7583029

Reputation:

The arrays are arranged with the lowest base address in memory and indexed to next element by an offset of 4 bytes.[Image from Harris D. M., Harris S. L. - Digital Design and Computer Architecture, 2nd Edition - 2012]

Harris D. M., Harris S. L. - Digital Design and Computer Architecture, 2nd Edition - 2012

 for_loop:
         bgt $t0, 100, for_loop_done     #i <100
         addi $t0, $t0, 1               #i++ or i = i+1


         lw $t4, 0($s0)              # load a in t4
         lw $t1, 4($s1)              # load b in t1
         lw $t2, 8($s2)              # load c in t2
         add $t4, $t2, $t1            # add b with c and store in a
         lw $t3, 12($s3)              # load d in t3
         sub $t4, $t3, $t4            # sub contents of a from d
         sw $t4, 0($s0)              # store contents of t4 into a

         j for_loop                   # go to start of loop

for_loop_done:

Upvotes: 1

Related Questions