Translate this code to MIPS assembly for i0 i

 Translate this code to MIPS assembly:  for (i=0; i<a; i++)    for (j=0; j<b; j++)       D[4*j] = i+j;  Assume the values of a, b, i and j are in registers $s0, $s1, $t0, and $t1. Assume the base address of the array D is in $s2. (Hint: Since memory is byte-addressed, you\'ll have to multiply the array index by 4 before accessing memory.  Use sll). 

Solution

    movl, $t0, -1                          //starting i from -1
loopi:
   add, $t0, $t0, 1                     // i++
   sub $t2, $t0, $s0                  // t2 = i - a
   bgez $t2, exit                       // if i >= a, exit
   movl, $t1, 0                          // j = 0
loopj:
   sub $t2, $t1, $s1                  //t2 = j-b
   bgez $t2, loopi                      //if j >= b, go to loopi
   sll, $t2, $t1, 4                       //calculate 4*4*j
   add, $t2, $s2, $t2                 //t2 = D[4*j]
   add, $t3, $t1, $t0                 //i+j
   sw, $t3, 0($t2)                     //D[4*j] = i+j
   add, $t1, $t1, 1                    //j++
   j loopj                            
exit:

 Translate this code to MIPS assembly: for (i=0; i<a; i++) for (j=0; j<b; j++) D[4*j] = i+j; Assume the values of a, b, i and j are in registers $s0, $s1,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site