Assume that the integer variables f g and i are assigned to

Assume that the integer variables f, g and i are assigned to registers $s0, $sl and $s2 respectively. Assume that the base addresses of integer arrays A and B are in register $s3 and $s4, respectively. Convert the following C program to MIPS assembly code f = B[A[i])-g; Convert the following C program to MIPS assembly code a=0.0; i=10; while (i > 0) (i=i-1; a=a*B[i];) where a is a single-precision floating-point number stored in register $f 0, i is an integer stored in register $s0. and B is a single-precision floating-point array with starting address stored in register $s1.

Solution

1. Once we get B[A[i]], the job becomes easy. Note that, sll is shift logical left operator.

sll $t0, $s2, 2                            #calculate i*4

add $t0, $s3, $t0                      #find address to load A[i]

lw $t1, 0($t0)                           #A[i] loaded into t1 register

sll $t0, $t1, 2                           #t0 register contains A[i]*4

add $t0, $s4, $t0   

lw $t1, 0($t0)                           #B[A[i]] loaded into t1 register

sub $s0, $t1, $s1                     #subtracted g from B[A[i]] and store in f

2.  

move $f0, 0                              # set a = 0

move $s0, 10                           # i = 10

while_label:

ble $s0, 0, Exit                         # if i <= 0, exit while loop

sub $s0, $s0, 1                        # i = i - 1

sll $t0, $s0, 2                          

add $t0, $s1, $t0                    #find base_add( B ) + 4*i

ls $f1, 0($t0)                           #load single precision floating pointe in f1 register

mul.s $f0, $f0, $f1                  #multiply it by a

j while_label                            #continue next iteration

Exit:

 Assume that the integer variables f, g and i are assigned to registers $s0, $sl and $s2 respectively. Assume that the base addresses of integer arrays A and B

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site