You are on page 1of 15

King Fahd University of Petroleum and Minerals

College of Computing and Mathematics


Computer Engineering Department

COE 301: Computer Organization


Term 232 (Spring 2024)
Midterm Exam
Saturday, March 9th , 2024

Time: 120 minutes,


Total Pages: 15 (including the cover page)
Total of points is 37.

Name:

ID:

Section:

Section Instructor Time


02 Dr. ALI ALSUWAIYAN 09:00-09:50
03 Dr. KHALID AL-HAWAJ 10:00-10:50
04 Dr. ALI ALSUWAIYAN 11:00-11:50
F01 Dr. ABDULAZIZ TABBAKH 11:00-11:50

Instructions:
• Smart devices (including mobile phones, tablets, headphones, smart watches, earbuds,
etc.) are not allowed in the Exam Hall
• Only basic calculators are allowed, i.e., those that can perform addition, subtraction,
multiplication, and division only.
• Do not open the exam book until instructed
• This is a closed-book exam, no external material is allowed
• No one can leave before half time
COE301 Midterm Exam - Page 2 of 15 3/9/2024

Question 1: This question contains MCQs in which every question has a single correct an-
swer. Choosing more than one answer voids your credit for the corresponding question.
To select an answer, fill in the circle next to the answer you want to choose.

(a) (1 point) One property of assembly language is that

❍ It is executed directly by the hardware without translation.

❍ It is interpreted at runtime.

❍ It uses mnemonics rather than 0s and 1s.

❍ It supports object-oriented programming

❍ It consists entirely of binary code: 1s and 0s


(b) (1 point) What components constitute the Disk Access Time for magnetic disk
storage?

❍ Seek Time, Rotation Latency, and Transfer Time.

❍ Seek Time and Transfer Time only.

❍ Rotation Latency and Seek Time only.

❍ Transfer Time and Buffer Time.

❍ Seek Time, Rotation Latency, Transfer Time, and Cache Time.

(c) (1 point) Which of the following statements is NOT true regarding assembly language di-
rectives?

❍ The .TEXT directive defines the code segment of a program.

❍ The .DATA directive is used to define the entry point of the program.

❍ The .GLOBL directive can be used to declare the main procedure of a program.

❍ The .DATA directive is used to define variables.

❍ The .TEXT directive contains the program’s instructions.


COE301 Midterm Exam - Page 3 of 15 3/9/2024

(d) (1 point) Consider the following non-leaf C function foo


int foo ( int a , int b )
{
int ary [10]; // array of 10 integers
int i ;
ary [0] = a + b ;
for ( i = 1; i < 10; i ++) {
ary [ i ] = ary [ i - 1] + bar ( ary [ i - 1] , bar (a , b ) ) ;
}
return ary [9];
}

When translating this foo function to assembly, the minimum size of the stack
frame in bytes is:

❍ 4

❍ 12

❍ 44

❍ 56

❍ 64

(e) (1 point) Which of the following instructions is an R-type instruction?

❍ addiu

❍ sll

❍ jr

❍ lbu

❍ bgt

Solution: Both sll and jr are R-type instructions.


COE301 Midterm Exam - Page 4 of 15 3/9/2024

(f) (1 point) Consider the following definition of data segment.

.DATA
var1: .HALF 0x4567, 0x89AB
var2: .BYTE 10
var3: .WORD 0x89ABCDEF

The value of register $t0 after executing lh $t0, 2($t1), where $t1 contains the address
of label var1 (in hexadecimal) is:

❍ 0x0000 0067

❍ 0x89AB 0000

❍ 0xFFFF BA98

❍ 0xFFFF 89AB

❍ 0x0000 89AB

Solution: 0xFFFF 89AB

(g) (1 point) Variables and arrays allocated dynamically at runtime through system calls will
be in:

❍ The heap area

❍ The static data area

❍ The stack area

❍ The text segment

❍ variables cannot be initialized at runtime


COE301 Midterm Exam - Page 5 of 15 3/9/2024

Question 2: (a) (5 points) Write a MIPS assembly code fragment that calculates the fol-
lowing function:
$s2 = ($s0)2 + ($s1)2 + 5 × $s0 + 25
Note that you do not need to declare the .data segment.

Solution:
mul $t0, $s0, $s0 # $t0 = x^2
mul $t1, $s1, $s1 # $t1 = y^2

li $t2,5 # $a0 = 5
mul $t2, $t2, $s0 # $a0 = 5*x
add $s2, $t2, $t0 # $t0 = x^2 + 5*x

addi $s2, $t2, 25 # $s = x^2 + 5*x + 25


add $s2, $s2, $t1 # $s = x^2 + 5*x + 25 + y^2
COE301 Midterm Exam - Page 6 of 15 3/9/2024

(b) (3 points) Write a minimum sequence of MIPS native instructions to implement


the following pseudo-instructions. You can only modify the $at register beside the
destination register. Branch and pseudo instructions are not allowed.

1. andi $t1,$t2,0xABCD0001 # AND with a 32-bit immediate

Solution:
lui $at, 0xABCD # $at = 0xABCD0000
ori $at, $at, 0x0001 # $at = 0xABCD0001
and $t1, $t2, $at

2. odd $t0, $t1 # set $t0 to 1 if $t1 is an odd number, 0 otherwise

Solution: andi $t0, $t1, 1


COE301 Midterm Exam - Page 7 of 15 3/9/2024

Question 3: Consider the following declarations:


. DATA
arraySize : . byte 6

arrayIn0 : . half 0 x6E00 , 0 x0B1F , 0 xF6E0 , 0 x3955 , 0 x592F , 0 x0778


arrayIn1 : . half 0 xF6AB , 0 xD2C7 , 0 x211F , 0 x4D07 , 0 xD306 , 0 xB97F
arrayCond : . half 0 x20F0 , 0 xDEAD , 0 xC95C , 0 xBEEF , 0 xB9F2 , 0 xBEEF
arrayOut : . half 0 x0000 :6

(a) (5 points) Fill-in the following symbol table and show the memory content for the
above static allocation and initialization; assume little-endian ordering and the base
of the .DATA section is 0x0400 1000 (Please, leave uninitialized bytes empty; do not
cross them out):

Symbol Address

arraySize

arrayIn0

arrayIn1

arrayCond

arrayOut

addr 0 1 2 3 4 5 6 7 8 9 a b c d e f

0x04001000

0x04001010

0x04001020

0x04001030

0x04001040

0x04001050

0x04001060

0x04001070
COE301 Midterm Exam - Page 8 of 15 3/9/2024

(b) (5 points) Using ALU, shifts, conditional branches, unconditional jumps, and com-
parison instructions only, write a sequence of MIPS32 instructions to perform the
semantics represented by the following pseudo-code:
for ( int i = 0; i < arraySize ; i ++) {
short int cond = arrayCond [ i ];
if ( cond == 0 xdead || cond == 0 xbeef ) {
arrayOut [ i ] = ( arrayIn0 [ i ] + arrayIn1 [ i ]) / 2;
} else {
arrayOut [ i ] = ( arrayIn0 [ i ] - arrayIn1 [ i ] + 0 x766 ) / 2;
}
}

Note: short int in C is a signed 16-bit type.

# Getting bases for all arrays


la $s0, arrayIn0
la $s1, arrayIn1
la $s2, arrayCond
la $s3, arrayOut

# Getting size of the arrays


la $t0, arraySize
lhu $s4, 0($t0)

# Implement the for-loop here ...

Solution:
ori $t3, $zero, 0xdead
ori $t4, $zero, 0xbeef
for_loop:
blez $s4, for_break
lh $t0, 0($s0)
lh $t1, 0($s1)
lh $t2, 0($s2)
beq $t2, $t3, if_true
bne $t2, $t3, if_false
if_true:
add $t5, $t0, $t1
sra $t5, $t5, 1
j if_done
if_else:
sub $t5, $t0, $t1
addi $t5, $t5, 0x766
if_done:
sw $t5, 0($s3)

# Bump pointers
addi $s0, $s0, 2
addi $s1, $s1, 2
addi $s2, $s2, 2
addi $s3, $s3, 2
COE301 Midterm Exam - Page 9 of 15 3/9/2024

# Decrement counter and loop-back ...


addi $s4, $s4, -1
j for_loop
COE301 Midterm Exam - Page 10 of 15 3/9/2024

(c) (2 points) Re-implement the code for the if-statement from Question 3 Part (b)
to utilize conditional moves. Your implementation of the if-statement for this part
should use no conditional branches nor unconditional jumps whatsoever (i.e., the
flow of instruction from the beginning of the if-statement to end should be sequen-
tial). You do not have to re-write all the code, you can re-write what you need to
demonstrate the conditional moves implementation of the if-statement.

Answer:
# Your implementation goes here ...

Solution:
lh $t0, 0($s0)
lh $t1, 0($s1)
lh $t2, 0($s2)
# Assume if-else always happen
sub $t5, $t0, $t1
addi $t5, $t5, 0x766
# Check if-true happened should happen
sub $t6, $t2, $t3
sub $t7, $t2, $t4
# overwrite
add $t2, $t0, $t1
movz $t5, $t2, $t6
movz $t5, $t2, $t7
# Write
sw $t5, 0($s3)
COE301 Midterm Exam - Page 11 of 15 3/9/2024

Question 4: (a) (5 points) Translate the following C function into assembly while adher-
ing to MIPS programmers’ conventions:
int arraySum ( int v [] , int n ) {
int i , rv ;
rv = 0;

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


rv = rv + v [ i ];
}

return rv ;
}

Solution:
arraySum:
addiu $v0, $zero, 0 # return value
addiu $t0, $zero, 0 # i
for:
lw $t0, 0($a0)
addu $v0, $v0, $t0
addiu $a0, $a0, 4
addiu $t0, $t0, 1
bne $t0, $a1, for
jr $ra
COE301 Midterm Exam - Page 12 of 15 3/9/2024

(b) (5 points) Write a function gcd(a, b) to compute the greatest common divisor of
two unsigned integers. The function is defined recursively as shown below:

gcd(a, 0) = a // if (b == 0)
gcd(a, b) = gcd(b, a % b) // a % b is the remainder of a / b

For example: gcd(8,12) = gcd(12,8) = gcd(8,4) = gcd(4,0) = 4.

Solution:
gcd:
bgtz $a1, recurse
addiu $v0, $a0, 0
jr $ra
recurse:
addiu $sp, $sp, -4
sw $ra, 0($sp)
div $a0, $a1
addiu $a0, $a1, 0
mfhi $a1
jal gcd
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
COE301 Midterm Exam - Page 13 of 15 3/9/2024

This page is left blank intentionally.


Page 9 of 10
List of Instructions and their Format
Page 10 of 10

You might also like