1. Convert the following C statements into MIPS assembly codes.

while (ary[i] == k)

i += j ;

Assume that i, j, and k correspond to registers $s1, $s2, and $s3 and the

base of the array ary is in register $s4.

(a) In the MIPS assembly codes, you could use one jump and one

branch instructions within the loop. (10%)

Loop: sll $t1, $s1,2 # $t1=i*4

add $t1, $s4,$t1 # $t1 = address of ary[i]

lw $t1, 0($t1) # $t1 = ary[i]

bne $t1, $s3, Exit # goto Exit if ary[i] ≠ k

add $s1, $s1, $s2 # i = i + j

j Loop

Exit:

(b) In the MIPS assembly codes, you could use only one branch

instruction within the loop. (10%)

sll $t1, $s1,2 # $t1=i*4

add $t1, $s4,$t1 # $t1 = address of ary[i]

lw $t1, 0($t1) # $t1 = ary[i]

bne $t1, $s3, Exit # goto Exit if ary[i] ≠ k

Loop: add $s1, $s1, $s2 # i = i + j

sll $t1, $s1,2 # $t1=i*4

add $t1, $s4,$t1 # $t1 = address of ary[i]

lw $t1, 0($t1) # $t1 = ary[i]

beq $t1, $s3, Loop # goto Loop if ary[i] = k

Exit:

2. Convert the following MIPS assembly codes into MIPS machine rogram starting at location 80000 in memory. (12%)

loop: lw $v1, 0($a0)

addi $v0, $v0, 1

sw $v1, 0($a1)

addi $a0, $a0, 1

addi $a1, $a1, 1

bne $v1,$zero, loop

Address op rs rt rd shamt funct

80000 35 4 3 0

80004 8 2 2 1

80008 43 5 3 0

80012 8 4 4 1

80016 8 5 5 1

80020 5 3 0 FFFA

3. Consider the following fragment of C code:

for (i=0;i<=100; i+=1)

a[i] = b[i] + c;

Assume that a and b are arrays of words and the base address of a is in $a0 and the base address of b is in $a1. Register $t0 is associated with variable i and register $s0 with the value of c. Its MIPS assembly code is as follows.

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

slti $t1, $t0, 401 # $t1=1 if $t0<401, i.e., $t0<=400

beq $t1, $zero, Exit # if $t0>400, goto Exit

Loop: add $t2, $a1, $t0 # $t2 = address of b[i]

lw $t2, 0($t2) # $t2 = b[i]

add $t2, $t2, $s0 # $t2 = b[i]+c

add $t1, $a0, $t0 # $t1 = address of a[i]

sw $t2, 0($t1) # a[i] = b[i]+c

addi $t0, $t0, 4 # i += 1

slti $t1, $t0, 401 # $t1=1 if $t0<401, i.e., $t0<=400

bne $t1, $zero, Loop # if $t0<=400, goto Loop

Exit:

(a) How many instructions are executed during the running of this code if there are no array out-of-bounds exceptions thrown? (8%)

3 + 8 * 101 = 3 + 808 = 811 (instructions)

(b) How many memory data references will be made during execution? (8%)

2 * 101 = 202 (memory data references)

(c) Convert the MIPS assembly code into MIPS machine program starting at location 160000 in memory. (22%)

Address op rs rt rd shamt funct

160000 0 0 0 8 0 32

160004 10 8 9 401

160008 4 9 0 8

160012 0 5 8 10 0 32

160016 35 10 10 0

160020 0 10 16 10 0 32

160024 0 4 8 9 0 32

160028 43 9 10 0

160032 8 8 8 4

160036 10 8 9 401

160040 5 9 0 FFF8

160044

4. The followings are C code.

int i ;

void set_array(int num)

{

int ary[10] ;

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

{

ary[i] = compare(num, i) ;

}

}

int compare(int a, int b)

{

if (sub(a, b) >= 0)

return 1;

else

return 0 ;

}

int sub(int a, int b)

{

return a-b ;

}

The variable code font is allocated on the stack, and i corresponds to register $s0.

(a) Convert the C procedure sub into MIPS assembly code. (5%)

sub: sub $v0, $a0, $a1 # $v0 = $a0 - $a1 = a – b

jr $ra

(b) Convert the C procedure compare into MIPS assembly code. (10%)

compare: addi $sp, $sp, -4

sw $ra, 0($sp) # save $ra

jal sub

slt $t0, $v0, $zero # $t0=1 if $v0<0

slti $v0, $t0,1 # v0=1 if $v0>=0

lw $ra, 0($sp) # restore $ra from $sp

addi $sp, $sp, 4

jr $ra

(c) Convert the C procedure set_array into MIPS assembly code. (15%)

set_array:

addi $sp, $sp, -52 # make 13 spaces

sw $ra, 48($sp) # save $ra

sw $s1, 44($sp) # save $s1

sw $s0, 40($sp) # save $s0

add $s0, $zero, $zero # $s0= i = 0

add $s1, $a0, $zero # $s1 = num

Loop: add $a0, $s1, $zero # $a0 = num

add $a1, $s0, $zero # $a1 = i

jal compare

sll $t1, $s0, 2 # $t1 = i * 4

add $t1, $sp, $t1 # $t1 = address of ary[i]

sw $v0, 0($t1) # ary[i] = $v0

addi $s0, $s0, 1 # i = i + 1

slti $t1, $s0, 10 # $t1=1 if i < 10

bne $t1, $zero, Loop # goto Loop if i<10

lw $ra, 48($sp) # restore $ra

lw $s1, 44($sp) # restore $s1

lw $s0, 40($sp) # restore $s0

addi $sp, $sp, 52

jr $ra