Você está na página 1de 3

2.33.1.

a)
copy:
move $t4, $zero
sll $t5, $a2, 2
for1tst: slt $t6, $t4, $t5
slt $t7, $t5, $t4
bne $t6, $zero, loop1
bne $t7, $zero, loop1
j exit1
loop1: move $t0, $t4
add $t1, $a0, $t0
add $t2, $a1, $t0
lw $t3, 0($t2)
sw $t3, 0($t1)
addi $t4, $t4, 4
j for1tst
exit1: jr $ra

#set i = 0
#$t5 = n * 4
#$t6 = ( i < n * 4 )
#$t7 = ( n * 4 < i )
#if t6 != 0 (i < n * 4), jump to loop1
#if t7 != 0 (n * 4 < i), jump to loop1
#jump to exit1 (i = n * 4)
#set $t0=$t4 (t0=i)
#$t1 = address of a[i]
#$t2 = address of b[i]
#load the contents of b[i] into $t3
#store the contents of $t3 into a[i]
#increment i by 4
#jump to loop test for1tst
#return to caller

2.33.2.a)
void copy(int *a, int *b, int n){
int *p;
int *q;
q = &b[0];
for(p = &a[0]; p != &a[n]; p = p+1){
*p = *q;
q = q+1;
}
}
2.33.3.a)
copy:
move $t3, $a0
move $t4, $a1
sll $t5, $a2, 2
add $t6, $a0, $t5
for1tst: slt $t1, $t3, $t6
slt $t2, $t6, $t3
bne $t1, $zero, loop1
bne $t2, $zero, loop1
j exit1
loop1: lw $t0, 0($t4)
sw $t0, 0($t3)
addi $t3, $t3, 4
addi $t4, $t4, 4
j for1tst
exit1: jr $ra

#p = a[0]
#q = b[0]
#$t5 = n * 4
#$t6 = &a[n*4]
#$t1 = (p < &a[n*4])
#$t2 = (&a[n*4] < p)
#if $t1 != 0 (p < &a[n*4]) go to loop1
#if $t2 != 0 (&a[n*4] < p) go to loop1
#jump to exit (p = &a[n*4])
#$t1 = *q
#*p = $t1
#shift p to next address
#shift q to next address
#jump back to loop
#return to caller

2.33.4.a)
The array based program has 10 instructions per worst-case, non-last loop iterat
ion. The pointer based program has 7 instructions.
2.33.5.a)

The array based program took 8 temporary registers, while the pointer based prog
ram took only 7.
2.33.6.a)
I'm not sure how this change would impact the number of executed instructions -I don't think it would.
2.33.1.b)
shift:
move $t5, $zero
addi $t6, $a1, -1
sll $t6, $t6, 2
for2tst: slt $t7, $t5, $t6
slt $t8, $t6, $t5
bne $t7, $zero, loop2
bne $t8, $zero, loop2
j exit2
loop2: addi $t0, $t5, 1
sll $t1, $t5, 2
sll $t0, $t0, 2
add $t3, $a0, $t1
add $t2, $a0, $t0
lw $t4, 0($t2)
sw $t4, 0($t3)
addi $t5, $t5, 1
j for2tst
exit2: jr $ra

#set i = 0
#store $a1-1 to $t6 (t6=n-1)
#$t6 = (n - 1)*4
#$t7 = ( i < (n-1) * 4 )
#$t8 = ( (n-1) * 4 < i )
#if t7 != 0 (i < (n-1) * 4), jump to loop1
#if t8 != 0 ((n-1) * 4 < i), jump to loop1
#go to exit1 if $t0!=0 (i=n)
#store $s0+1 to $t0 (t0=i+1)
#set $t1=$s0*4 (t1=i*4)
#set $t0=$t0*4 (t0=(i+1)*4)
#$t3 = address of a[i]
#$t2 = address of a[i+1]
#load the contents of a[i+1] into $t4
#store the contents of $t4 into a[i]
#increment i by 1
#jump to loop test for1tst
#return to caller

2.33.2.b)
void shift(int *a, int n)
int *p;
int *q;
for(p = &a[0], p != &a[n]; p = p+1){
q = p+1;
*p = *q;
}
}
2.33.3b)
shift:
move $t3, $a0
#p = a[0]
sll $t5, $a2, 2
#$s2 = n * 4
addi $t5, $t5, -4
#$s2 = (n * 4) - 4 (previous address)
add $t6, $a0, $t5
#$s3 = &a[n*4-4]
for2test: slt $t1, $t3, $t6 #$t1 = (p < &a[n*4-4])
slt $t2, $t6, $t3
#$t2 = (&a[n*4-4] < p)
bne $t1, $zero, loop2
#if $t1 != 0 (p < &a[n*4-4]) go to else
bne $t2, $zero, loop2
#if $t2 != 0 (&a[n*4-4] < p) go to else
j exit2
#jump to exit (p = &a[(n-1)*4])
loop2: addi $t4, $t3, 4
#q = p + 1 address
lw $t0, 0($t4)
#$t0 = *q
sw $t0, 0($t3)
#*p = $t0

move $t3, $t4


j for2test
exit2: jr $ra

#p = p + 1 address
#jump back to for loop
#return to caller

2.33.4.b)
The array based program has 12 instructions per worst-case, non-last loop iterat
ion. The pointer based program has 8 instructions.
2.33.5.b)
The array based program took 9 temporary registers, while the pointer based prog
ram took only 7.
2.33.6.b)
I'm not sure how this change would impact the number of executed instructions -I don't think it would.

Você também pode gostar