ITCS 321 Test TWO DEC 2018 KEY

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Name: Student id: #:

QUESTION # 1 2 3 4 5 TOTAL
MAX POINTS 14 15 16 16 12 73
POINTS EARNED

***************************************************************************************
UNIVERSITY OF BAHRAIN COLLEGE OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
ITCS 321: COMPUTER ORGAIZATION AND ASSEMBLY LANGUAGE
SECOND TEST FIRST SEMESTER 2018/2019 DATE: DEC 25, 2018
***************************************************************************************
QUESTION ONE {14 pts}
1) In returning from a function, the callee performs the two following actions:
 Puts the results in a place that can be accessed by the caller
 Transfers control to the caller, to the instruction immediately
following the call.
2) The MIPS instruction jalr $t2, $t4 performs the two following actions:
 Saves the address of the next instruction in $t2 = PC+4
 Calls the function whose address is in $t4
 Carefully study the following MIPS code and answer all questions below.
Address MIPS Instructions Assembly Language
1) 00400040 lui $1, 0x1001 la $a0, arr
2) 00400044 ori $4, $1, 0
3) 00400048 ori $5, $0, 20 ori $a1,$0,10
4) 0040004C jal 0x10001a jal wyst
5) 00400040 . . .
6) wyst:
7) 00400068 sll $8, $5, 2 sll $t0, $a1, 2
8) 0040006C add $8, $8, $4 add $t0, $t0, $a0
9) 00400070 lw $9, 0($8) lw $t1, 0($t0)
10) 00400074 lw $10,4($8) lw $t2, 4($t0)
11) 00400078 sw $10,0($8) sw $t2, 0($t0)
12) 0040007C sw $9, 4($8) sw $t1, 4($t0)
13) 00400080 jr $31 jr $ra

a) Briefly explain (write one sentence only) what the function wyst do.
 It swaps element #20 with element #21 in the array named arr
b) What will be in PC register after the instruction at line #4 is fetched (not executed yet)?
 PC = 0x00400040
c) Executing the instruction at line #4 changes the value of $ra to 0x00400040
d) What will be in PC register after executing the instruction at line #4? PC = 0x00400068
e) What will be in PC register after executing the instruction at line #13? PC = 0x00400040

ITCS 321 Test TWO DEC 2018 KEY Page# 1 AAA


QUESTION TWO {15 pts}
1) In calling a function, the caller performs the three following actions:
 Puts the parameters in a place that can be accessed by the callee
 Saves the return address PC+4 in $31 ($ra)
 Transfers control to the callee function

2) Briefly explain (write one sentence only) when the stack frame is created and when is deleted.
A stack frame is created whenever a function is called and deleted
whenever a function is exited.
3) What is the RTL description of the BEQ instruction?
if (Reg(rs) == Reg(rt)) PC ← PC + 4 + 4 × sign_ext(offset16)
else PC ← PC + 4

4) Suppose that floating-point square root FSQRT is responsible for 30% of the execution time of some
application and all floating-point FP instructions (ALLFP) are responsible for 72%. To improve the
performance of this application, the developers asked you to help them select the best of the following
two choices:
a) First choice is to speedup FSQRT by a factor of 12
b) Second choice: execute ALLFP instructions 2x faster.

 First choice: Improve FSQRT by a factor of 12


Speedup (FSQRT) = 1/(0.7 + 0.3/12) = 1.379

 Second choice: Improve ALLFP instructions by a factor of 2


Speedup (ALLFP) = 1/(0.28 + 0.72/2) = 1.563  Better

5) A new compiler is being tested a 3 GHz machine with three different classes of instructions: class A
requires 2 cycles, class B requires 3 cycles, and class C requires 1 cycle. For a benchmark program the
compiler produces 4 billion class A instructions, 2 billion class B instructions, and 3 billion class C
instructions. Calculate the MIPS of the new compiler.

 CPU cycles = (4×2 + 2×3 + 3×1)×109 = 17×109


 Execution time = 17×109 cycles / 3×109 Hz = 5.667 sec
 MIPS = Instruction Count / (Execution Time × 106)
 MIPS = (4+2+3) × 109 / (5.667 × 106) = 1588

ITCS 321 Test TWO DEC 2018 KEY Page# 2 AAA


QUESTION THREE {16 pts}

Given a static array arr .word 10, 20, ... consisting of 80 values. Write a
complete MIPS program that defines and calls a recursive function rec_sum to
calculate the sum of arr elements, and then prints the calculated sum with proper
message: The sum of array elements = xxxx.
int rec_sum (int A[], int n)
.data {
arr: .word 10,20,30,40,50,60,-90,-60, … if (n == 0) return 0;
mes: .asciiz "The sum of array = " if (n == 1) return A[0];
.text int sum = rec_sum (&A[0], n);
.globl main return sum;
main: li $v0, 4 # print string }
la $a0, mes
syscall

la $a0, arr
li $a1, 80 # array size n = 80
jal rec_sum

move $a0, $v0


li $v0, 1 # print sum
syscall

li $v0, 10 # exit
syscall

rec_sum: # $a0 = &A, $a1 = n


bnez $a1, L1 # branch if (n != 0)
li $v0, 0
jr $ra # return 0
L1: bne $a1, 1, L2 # branch if (n != 1)
lw $v0, 0($a0) # $v0 = A[0]
jr $ra # return A[0]

L2: addiu $sp, $sp, -12 # allocate frame = 12 bytes


sw $ra, 0($sp) # save $ra
sw $s0, 4($sp) # save $s0
sw $s1, 8($sp) # save $s1
move $s0, $a0 # $s0 = &A (preserved)
move $s1, $a1 # $s1 = n (preserved)
addiu $a0, $a0, 4 # next array element
addiu $a1, $a1, -1 # n = n-1
jal rec_sum # recursive call

lw $t1, 0($s0)
add $v0, $v0, $t1 # $v0 = $v0 + A[i]
lw $ra, 0($sp) # restore $ra
lw $s0, 4($sp) # restore $s0
lw $s1, 8($sp) # restore $s1
addiu $sp, $sp, 12 # free stack frame
jr $ra # return to caller

ITCS 321 Test TWO DEC 2018 KEY Page# 3 AAA


QUESTION FOUR {16 pts}

Given a datapath with the following delay times: instruction memory access time = 500 ps, data
memory access time = 500 ps, instruction decode and register read = 300 ps, register write = 200 ps,
ALU delay = 300 ps. Ignore the other delays in the multiplexers, wires, etc. Assume the following
instruction mix: 35% ALU, 20% load, 10% store, 25% branch, and 10% jump.
a) [5 pts] Use the following table to compute the length (delay) for each instruction class for the
single-cycle datapath.
Instruction Instruction Decode & Data Write Total
ALU
Class Memory Reg Read Memory Back Delay
ALU 500 300 300 200 1300
Load 500 300 300 500 200 1800
Store 500 300 300 500 1600

Branch 500 300 300 1100

Jump 500 300 800

b) [2 pts] Compute the clock cycle for the single-cycle processor.

Clock cycle = longest instruction delay = 1800 ps

c) [2 pts] Compute the clock cycle for the multi-cycle processor.

Clock cycle for multi-cycle = max(500, 300, 200) = 500 ps

d) [3 pts] Compute the average CPI for the multi-cycle processor.

Average CPI = 0.35×4 + 0.2×5 + 0.1×4 + 0.25×3 + 0.1×2 = 3.75

e) [2 pts] What is the speedup factor of the multi-cycle over the single-cycle processor?

Speedup = 1800 × 1 / (500 × 3.75) = 1800 / 1875 = 0.96

(Multi-cycle is slower)

f) [2 pts] What is the speedup factor of the pipelined over the single-cycle processor?

Speedup = 1800 / 500 = 3.6 (Pipelined processor is faster)

ITCS 321 Test TWO DEC 2018 KEY Page# 4 AAA


QUESTION FIVE {12 pts}

 Write the if statement that defines the value of the PCSrc controlling the PC MUX and draw the
logic diagram for the PC register control logic.

if (Op == J)
PCSrc = 1;
else
if ( (Op == BEQ && Zero == 1) || (Op == BNE && Zero == 0) )
PCSrc = 2;
else
PCSrc = 0;

Op

Decoder

BEQ BNE
Zero J

Branch Jump

 Draw the logic diagram of the extender used in the datapath.

.. Upper
16 bits
.
ExtOp

.. Lower
Imm16
16 bits
.

ITCS 321 Test TWO DEC 2018 KEY Page# 5 AAA


Table of MIPS System calls
Service $v0 Arguments / Result

Print Integer 1 $a0 = integer value to print

Print Float 2 $f12 = float value to print


Print Double 3 $f12 = double value to print
Print String 4 $a0 = address of null-terminated string
Read Integer 5 Returns integer value in $v0
Read Float 6 Returns float value in $f0
Read Double 7 Returns double value in $f0
$a0 = address of input buffer
Read String 8
$a1 = maximum number of characters to read
Allocate Heap $a0 = number of bytes to allocate
9
memory Returns address of allocated memory in $v0

Exit Program 10
Print Char 11 $a0 = character to print
Read Char 12 Return character read in $v0

$a0 = address of null-terminated filename string $a1 =


flags (0 = read-only, 1 = write-only)
Open File 13
$a2 = mode (ignored)
Returns file descriptor in $v0 (negative if error)

$a0 = File descriptor


Read $a1 = address of input buffer
14
from File $a2 = maximum number of characters to read
Returns number of characters read in $v0

$a0 = File descriptor


$a1 = address of buffer
Write to File 15
$a2 = number of characters to write
Returns number of characters written in $v0
Close File 16 $a0 = File descriptor

ITCS 321 Test TWO DEC 2018 KEY Page# 6 AAA

You might also like