Você está na página 1de 3

CSE230/EEE230 Computer Organization & Assembly Language Programming

Midterm Exam 1, September 30th, 2010


Arizona State University
Rules
1. The exam is open book and open notes, you may use your laptop to access the lecture notes. However,
you are not allowed to use your laptop for communication or Internet access (e.g., no email, no
chatting, no Google search).
2. Work on all the problems. They add up to 100 points total.
3. Whenever appropriate, you should try to show how you solved the problem (e.g., writing down
intermediate steps leading to your final answer) instead of supplying only the final answer, so that you
may earn partial credit even if your final answer is incorrect.
4. Your answers must be legible and organized.

Student Name: ________________________________ ASU ID: ________________________

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.

2. (15 points) After performing the following sequence of instructions,


lui $t2, 0x0101
ori $t2, $t2, 0x0010
sub $t2, $t2, 0x01010010
what is the content of $t2? (Show all the bits of $t2 or simply use the hexadecimal format.)

after lui, $t2 = 0x01010000


after ori, $t2 = 0x01010010
after sub, $t2 = 0x00000000

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:

In C, the above is equivalent to:

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;

for (i = 0; i < n; i++){ {


if (str1[i] != str2[i]) return 1;
}
return 0;
}
Assume that the base addresses of str1 and str2 are in $a0 and $a1 respectively, and n is in $a3
and i in $t1. Also, assume that the return values (either 1 or 0) will be stored in $v0. Write the
corresponding assembly code for the above routine.
Following is one implementation, for your reference only. There are different ways of achieving the
same goal. In grading this problem, you may get full mark if your code has the correct structure even if
it might not be run directly on a simulator (e.g., for some minor syntactic errors).

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

move $t0, $a3 # $t0 = the length of the string initially


move $t1, $a0 # $t1 = the address of string1
move $t2, $a1 # $t2 = the address of string2
move $v0, $zero #initialize the answer

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

Você também pode gostar