COSC 321 HW 9/18/08 Solutions

advertisement
COSC 321 HW
9/18/08 Solutions
Do the following problems from P&H
2.6
Logical shifts will drag in 0s. Shift left to dump bits 23 - 31, then shift right to end up in
right place
sll
srl
$t0, $t3, 9
$t0, $t0, 14
2.15 Coming later -- I need a computer
2.30
#
#
#
#
#
#
a0 is base of a[], a1 is base of b[]
a2 holds # elements of a[],
$a3 holds # elements of b[]
$t0 will index a[], $t1 will index b[]
$t4 holds address of next element in a[]
$t3 holds address of next element in b[]
sll
sll
$a2, $a2, 2
$a3, $a3, 2
# convert word to byte count
# same thing
add $v0, $zero, $zero
outer:
inner:
skip:
# clear counter
# outer loop steps through
add $t0, $zero, $zero
#
add $t4, $a0, $t0
#
lw $t4, 0($t4)
#
a[]
$t0 = 0
$t4 -> a[current]
get a[current]
# inner loop steps through
add $t1, $zero, $zero
#
add $t3, $a1, $t1
#
lw $t3, 0($t3)
#
bne $t3, T4, skip
#
#
addi $v0, $v0, 1
#
#
addi $t1, $t1, 4
#
bne $t1, $a3, inner
#
# done with inner loop
b[]
$t1 = 0
$t3 -> b[current]
get b[current]
if values unequal,
skip over next instr
values were equal,
so increment counter
incrmnt index into b[]
more to do in b[]?
addi $t0, $t0, 4
bne $t0, $a2, outer
# done with outer loop
# incrmnt index into a[]
# more to do in a[]?
This code counts the number of times each element in the first array appears in the
second array. In high level language:
v = 0;
for (i=0; i<a.length; i++)
for (j=0; j<b.length; j++)
if (a[i] == b[j]) v++;
2.31
Count
instruction
1
1
sll
sll
1
add $v0, $zero, $zero
$a2, $a2, 2
$a3, $a3, 2
# convert word to byte count
# same thing
# clear counter
# outer loop steps through a[]
1
add $t0, $zero, $zero
# $t0 = 0
2500outer:
add $t4, $a0, $t0
# $t4 -> a[current]
2500
lw $t4, 0($t4)
# get a[current]
# inner loop steps through
2500
add $t1, $zero, $zero
#
2
2500 inner:
add $t3, $a1, $t1
25002
lw $t3, 0($t3)
#
2
2500
bne $t3, T4, skip
#
#
25002
addi $v0, $v0, 1
#
#
25002skip: addi $t1, $t1, 4
#
2
2500
bne $t1, $a3, inner
#
# done with inner loop
2500
2500
addi $t0, $t0, 4
bne $t0, $a2, outer
b[]
$t1 = 0
# $t3 -> b[current]
get b[current]
if values unequal,
skip over next instr
values were equal,
so increment counter
incrmnt index into b[]
more to do in b[]?
# incrmnt index into a[]
# more to do in a[]?
Class 1 (add, addi, sll) instructions are executed:
1+1+1+1+2500+25002+25002+2500+2500= 4 + 7500 + 12500000 =
12507504 inst * 1 ticks/inst = 12.5 * 106 tick
Class 2 (lw, bne) instructions are executed:
2500 + 25002 + 25002 + 25002 + 2500 =
18,755,000 inst * 2 tick/inst = 37.5 * 106 ticks
Total # tick is 50*106 ticks
50*106 tick / 2*106 tick/sec -> 100 sec
Download