Professional Documents
Culture Documents
Name:
ID:
Section:
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.
❍ It is interpreted at runtime.
(c) (1 point) Which of the following statements is NOT true regarding assembly language di-
rectives?
❍ 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.
When translating this foo function to assembly, the minimum size of the stack
frame in bytes is:
❍ 4
❍ 12
❍ 44
❍ 56
❍ 64
❍ addiu
❍ sll
❍ jr
❍ lbu
❍ bgt
.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
(g) (1 point) Variables and arrays allocated dynamically at runtime through system calls will
be in:
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
Solution:
lui $at, 0xABCD # $at = 0xABCD0000
ori $at, $at, 0x0001 # $at = 0xABCD0001
and $t1, $t2, $at
(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;
}
}
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
(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;
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
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