Escolar Documentos
Profissional Documentos
Cultura Documentos
1. (10 points) If machine A has a higher MIPS (million instructions per second) than machine B
for one program, machine A should be faster than machine B. Is this true? Explain your
answer briefly.
This is in general not true. MIPS alone cannot determine the speed of a machine especially if
this is only from a single program. Refer to Section 1.8 of the textbook.
3. (15 points) Given two decimal numbers a=105 and b=-28, convert them into signed 8-bit
numbers in 2’s complement representation, and then perform a+b by hand. Determine
whether there is an overflow.
In 2’s complement, (105)10 = (01101001)2, (-28)10 = (11100100)2
01101001
+11100100
0 1 0 0 1 1 0 1 which is 7710
We are adding two numbers of different signs, and thus there is no overflow.
1
4. (30 points) What does the following code do? (You can simply write down the corresponding
code in any high-level programming language such as C. For convenience, you can define
six variables f, g, h, i, j, k which correspond to the content in register $s0, $s1, $s2, $s3, $s4,
$s5, respectively.)
bne $s5, $zero, C1
add $s0, $s3, $s4
j Exit
C1: addi $t0, $s5, -1
bne $t0, $zero, C2
add $s0, $s1, $s2
j Exit
C2: addi $t0, $s5, -2
bne $t0, $zero, C3
sub $s0, $s1, $s2
j Exit
C3: addi $t0, $s5, -3
bne $t0, $zero, Exit
sub $s0, $s3, $s4
Exit:
if (k==0) f = i + j;
else if (k==1) f = g + h;
else if (k==2) f = g - h;
else if (k==3) f = i - j;
2
5. (30 points) The following C subroutine compares two input strings of n characters in length
(each character occupies one byte) and report the result.
int string_compare (char str1[], char str2[], int n)
{
int i;
string_compare:
## First, save the “saved” registers. Note, this is good habit but for this particular piece of
## code, since it is a leaf routine, not doing the saving will not change the results. (No
## point was deducted whether or not you saved the “saved” registers.
addi $sp, $sp, -8 # move stack pointer
sw $fp, 4($sp) # save frame pointer
sw $ra, 0($sp) # save return address
addi $fp, $sp, 4 # establish frame pointer
## end of saving
loop:
beq $t0, $zero, exitcompare # $t0 reaches 0, exitcompare
lb $t3, 0($t1) # load the current character of string1
lb $t4, 0($t2) # load the current character of string2
subu $v0, $t3, $t4 # $v0 = $t3-$t4, usigned sub, so $v0=0 unless $t3=$t4
sltu $v0, $zero, $v0 # if t3 != $t4, $v0 = 1
addi $t1, $t1, 1 # new character of string1
addi $t2, $t2, 1 # new character of string2
addi $t0, $t0, -1 # subtract 1 from $t0
beq $v0, $zero, loop #if $v0 = 0, continue the loop, otherwise, exit loop
exitcompare:
lw $ra, 0($sp) # restore return address
lw $fp, 4($sp) # restore frame pointer
addi $sp, $sp, 8 # restore stack pointer
jr $ra # return