Translate the following C code to MIPS assembly code Use a m

Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the array D.

for(i=0; i<a; i++)

for(j=0; j<b; j++)

D[4*(i*b + j)] = i + j

Solution

   .globl   main
$LFB0 = .
   .set   nomips16
   .ent   main
   .type   main, @function
main:
   .frame   $fp,432,$31       # vars= 416, regs= 1/0, args= 0, gp= 8
   .mask   0x40000000,-4
   .fmask   0x00000000,0
   .set   noreorder
   .set   nomacro
  
   addiu   $sp,$sp,-432
$LCFI0:
   sw   $fp,428($sp)
$LCFI1:
   move   $fp,$sp
   movz   $31,$31,$0
$LCFI2:
   li   $2,2           # 0x2
   sw   $2,12($fp)
   li   $2,2           # 0x2
   sw   $2,8($fp)
   sw   $0,20($fp)
   b   $L2
   nop

$L5:
   sw   $0,16($fp)
   b   $L3
   nop

$L4:
   lw   $3,20($fp)
   lw   $2,8($fp)
   nop
   mult   $3,$2
   mflo   $3
   lw   $2,16($fp)
   nop
   addu   $2,$3,$2
   sll   $2,$2,2
   lw   $4,20($fp)
   lw   $3,16($fp)
   nop
   addu   $3,$4,$3
   sll   $2,$2,2
   addiu   $4,$fp,8
   addu   $2,$4,$2
   sw   $3,16($2)
   lw   $2,16($fp)
   nop
   addiu   $2,$2,1
   sw   $2,16($fp)
$L3:
   lw   $3,16($fp)
   lw   $2,8($fp)
   nop
   slt   $2,$3,$2
   andi   $2,$2,0x00ff
   bne   $2,$0,$L4
   nop

   lw   $2,20($fp)
   nop
   addiu   $2,$2,1
   sw   $2,20($fp)
$L2:
   lw   $3,20($fp)
   lw   $2,12($fp)
   nop
   slt   $2,$3,$2
   andi   $2,$2,0x00ff
   bne   $2,$0,$L5
   nop

   move   $2,$0
   move   $sp,$fp
   lw   $fp,428($sp)
   addiu   $sp,$sp,432
   j   $31
   nop

   .set   macro
   .set   reorder
   .end   main

------------------------------------------------------------------------------------------------------------------------------------

Respective c code

main(){
   int i,j,a=2,b=2;
   int D[100];
   for(i=0; i<a; i++)
   for(j=0; j<b; j++)
   D[4*(i*b + j)] = i + j;
}

Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1,
Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that 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