Docsity

Midterm Exam 2 Sample Solutions - Computer Architecture - Spring 2005 | EEL 4713, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
Please write your name at the top of every page: _______________________________
EEL 4713/5764, Computer Architecture, Spring 2005
Midterm Exam #2 – Make-Up Version
SAMPLE SOLUTIONS
On this exam, you may ONLY complete those questions such that you got less than a B
(80%) on the corresponding question in the original exam #2. Out of these, please
complete ONLY those questions that you wish to have graded and to take the place of the
corresponding question from the original exam. Please mark X’s in the first column
below next to the questions on this make-up exam that you would like to have graded:
Grade? Score
1. CAssembly: ____ ____ / 20
2. Floating point: ____ ____ / 20
3. ALU & contol: ____ ____ / 20
4. Multip. / divis.: ____ ____ / 40
5. Single-cycle DP: ____ ____ / 10
TOTAL: ____ / 100
WARNING: Since this exam is a second chance, it will be graded even more strictly
than the original exam. Take your time, and check your answers to make they are right!
Remember to always show your work!
BIG WARNING: This is an exam, not a homework assignment! You MUST work by
yourself, and you may not give or receive answers to/from anyone! Any answers that
look like they were copied from (or to) someone else’s paper will automatically earn a 0!
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Exam 2 Sample Solutions - Computer Architecture - Spring 2005 | EEL 4713 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

EEL 4713/5764, Computer Architecture, Spring 2005

Midterm Exam #2 – Make-Up Version

SAMPLE SOLUTIONS

On this exam, you may ONLY complete those questions such that you got less than a B

(80%) on the corresponding question in the original exam #2. Out of these, please

complete ONLY those questions that you wish to have graded and to take the place of the

corresponding question from the original exam. Please mark X’s in the first column

below next to the questions on this make-up exam that you would like to have graded:

Grade? Score

  1. CAssembly: ____ ____ / 20
  2. Floating point: ____ ____ / 20
  3. ALU & contol: ____ ____ / 20
  4. Multip. / divis.: ____ ____ / 40
  5. Single-cycle DP: ____ ____ / 10

TOTAL: ____ / 100

WARNING: Since this exam is a second chance, it will be graded even more strictly

than the original exam. Take your time, and check your answers to make they are right!

Remember to always show your work!

BIG WARNING: This is an exam, not a homework assignment! You MUST work by

yourself, and you may not give or receive answers to/from anyone! Any answers that

look like they were copied from (or to) someone else’s paper will automatically earn a 0!

  1. [20 points] (CIO #4, CMIPS Assembly) Consider the following C language

code fragment.

p = 1;

for ( i =2; i * i <= n ; i ++) {

if ( n % i == 0) { p = 0; break; }

a) What does this algorithm do? That is, given some initial value of n that is

greater than 1, under what conditions will the final value of p be 1, as opposed

to 0? Give the simplest description of these conditions, using ordinary,

common mathematical terminology. (Hint: It should only take a few words.)

This algorithm determines whether n is a prime number. The

final value of p will be 1 if and only if n is prime.

To see this, note that if n is composite, then it must have a

factor i that is greater than or equal to 2 and where i

2 ≤ n.

We try all i in this range, and set p = 0 if for some i , n mod i =

0, or in other words, if i divides n evenly, which means i is a

factor of n and n is composite. If we find no factors then n

must be prime, and p remains at its initial value of 1.

b) Convert the above algorithm into an equivalent MIPS assembly language code

fragment. Assume that variables n , p , and i are all 32-bit signed integer

variables that are initially contained in registers $s0, $s1, and $s

respectively. You may use any of the temporary registers $t n. For this

problem, you ONLY need to write a code fragment, that is, do not worry

about subroutine entry and exit code. For full credit, please comment your

code.

li $s1, 1 # p := 1;

li $s2, 2 # i := 2;

while: mul $t0, $s2, $s2 # $t0 := ii;*

bgt $t0, $s0, end # until $t0>n do body

divu $s0, $s2 # (lo,hi) := (n/i, n%i)

mfhi $t0 # $t0 := n%i

bnez $t0, endif # if $t0!=0 skip body

move $s1, $zero # p := 0;

b end # break;

endif: addi $s2, $s2, 1 # i := i+1;

b while # continue while loop

end:

  1. [20 points] (CIO#6, ALU & control) Below are two copies of the 1-bit ALU cell

from fig. B.5.9 in the textbook Assume the upper cell handles bit #0 of the

operands, and the lower copy handles bit #1. (For a 32-bit ALU, thirty additional

cells below these are implied but not shown.)

a) How would you modify these cells to also support the srl (shift right logical)

instruction, without impairing the ALU’s existing functionality? Sketch any

needed modifications directly on top of the below diagram. Your

modifications can extend outside the box if you need the space. Then, write a

short textual explanation of your modifications in the space below the

diagram.

Here is one way. Each cell’s mux gets a new input (labeled

3=11 2 ) which is the shift result. This can be provided by another

32-input mux whose inputs come from the B inputs of all the

higher-numbered cells (or 0 if there are no more), and whose

control comes from the low 5 bits of operand A, to which we can

route the shamt field (instruction bits 6-10).

b[1]

a[31:0] a[0]

a[1]

a[4:0]

b[0]

b[31]

b[2]

b[1]

GND

a[4:0]

b) In order to tell your new ALU that the srl function should be performed, you

will either need to define either a new control signal (which you should name), or

define a new possible value for an existing control signal. Explain how the

control is handled in your design. What should the values of ALL of the control

signals (including the CarryIn to bit 0) be set to in order to select your new srl

function? (Even if some of the control signals don’t matter, you should indicate

the don’t-cares.)

We’ll just use the same Operation control signal and assign a

new value 3=11 2 to select SRL. Anegate and Bnegate should be

0 and Carry is a don’t care. Outside the ALU, a new control input

Asrc is needed to select whether the ALU’s input A comes from rs

(for this, set Asrc=0) or from the shamt field (instruction bits 6-

  1. (for this, set Asrc=1). This solution allows the same

hardware to also execute srlv (shift right logical variable).

Option #1. If you choose to write your program in C or C++, assume that

you are given a function with the following declaration, which you must use to

multiply two 8-bit unsigned numbers to get an unsigned 16-bit result.

unsigned short mult8(unsigned char multiplicand,

unsigned char multiplier);

Meanwhile, the new 16-bit multiplication function that you write should

be a complete, working function with the following declaration:

unsigned int mult16(unsigned short multiplicand,

unsigned short multiplier);

Assume that an int is 32 bits and a short is 16 bits.

Option #2. If you write your program in MIPS assembly, assume you are

given a subroutine at label mult8 that takes an unsigned 8-bit multiplicand

located in the LSB of register $a0 , and an unsigned 8-bit multiplier located in the

LSB of register $a1 , and returns an unsigned 16-bit product located in the lower

half of register $v0. You may assume this subroutine preserves the $s registers.

Meanwhile, your subroutine should begin at the label mult16 , and

should take an unsigned 16-bit multiplicand in the lower half of register $a0 , and

an unsigned 16-bit multiplier in the lower half of register $a1 , and should return

the unsigned 32-bit product in register $v0. Your subroutine must observe all of

the standard MIPS subroutine calling conventions.

Please note: You may NOT use any built-in multiplication instruc-

*tions (whether C’s , or MIPS’s mul, mult, etc. ) anywhere in your program!

You must, however, use the mult8 routine described above.

Write out your program (in either C or assembly, or both) neatly on the

next page. (You should probably write out a draft on scratch paper first.) You

must COMMENT YOUR CODE to get full credit.

Answer to question #4:

Option #1:

unsigned int mult16( unsigned short multiplicand ,

unsigned short multiplier ){

/* Upper and lower halves of operands. */

unsigned char

a1 = multiplicand >> 8,

a0 = multiplicand & 0xff,

b1 = multiplier >> 8,

b0 = multiplier & 0xff;

/* Coefficients of terms in the sum. */

unsigned short

c2 = mult8( a1 , b1 ),

c0 = mult8( a0 , b0 ),

c1 = mult8( a1 + a0 , b1 + b0 ) – c2 – c0 ;

/* Put together the result. */

unsigned int product =

( c2 << 16) + ( c1 << 8) + c0 ;

return product ;

Option #2:

The following assembly implements the above C code. $s

registers must be used for our local variables, since we

can’t depend on mult8 preserving the $t registers. Thus

we must preserve the caller’s values for the $s registers

we use. Also, $ra gets trashed when we jal to mult8, so we

have to preserve it also. Our local variables (from the C

program above) are allocated to registers as follows:

Local variables a1,a0: $s1,$s

b1,b0: $s3,$s

c2,c1,c0: $s6,$s5,$s

The assembly code follows.

sll $s6, $s6, 16 # c2 <<= 16

sll $s5, $s5, 8 # c1 <<= 8

add $v0, $s6, $s5 # $v0 = c2 + c

add $v0, $v0, $s4 # $v0 += c

# Restore registers.

lw $ra, 0($sp) # Our return addr.

lw $s0, 4($sp) # $s regs we used.

lw $s1, 8($sp)

lw $s2, 12($sp)

lw $s3, 16($sp)

lw $s4, 20($sp)

lw $s5, 24($sp)

lw $s6, 28($sp)

addi $sp, $sp, 32 # Restore stk.ptr.

jr $ra # Return to caller

Actually, there was a bug in the original problem description,

which is that when computing the product of (a 1 +a 0 )(b 1 +b 0 ), the

operands are ideally supposed to be N-bit numbers (8 bits in our

case) but they may actually be (N+1) bits long (9 in our case)

since adding two N-bit numbers in general can produce an

(N+1)-bit number. So, the mult8 routine actually needs to check

to see if this extra bit is present, and adjust its results

accordingly. Similarly, if our mult16 routine is being used in the

context of a similar mult32 algorithm, then it too needs to check

to see if there is an extra bit at position 16 in the input operands.

Basically, to correct the final result we just need to add ij 2

M

  • ( ib

  • ja )

N

, where i and j are the extra bits of A and B , and a and b

are A and B with the extra bits stripped off. Since i and j are just

0 and 1, this expression can be computed using just shifts and

adds.

  1. [10 points] (Extra credit.) Single-cycle datapath. Below is the MIPS single-cycle

datapath from figure 5.24, with control lines shown.

a.) Assuming the ALU already supports it, how would this datapath need to be

modified to support the srl instruction, without disabling any existing

instructions? Sketch your modifications clearly on top of the above diagram.

Basically just need to route instruction bits 10-6 (shamt

field) to the ALU, either as an extra control input, or in place

of operand B, which would require a third input to the mux

feeding the lower input to the ALU.

b) What data lines in your modified datapath are required in order to execute the

srl instruction? Use a highlighter pen to emphasize all of the lines that are

required (except for control lines), including in the PC update path.

Like any other R-type instruction, except that the shamt

field bits are used (instead of rs) to provide the A input to the

ALU.

(shamt)

Instruction[10-6]

1

0

ALUSrcA