Docsity

Midterm II Questions with Solution - Machine Structures | COMPSCI 61C, Exams of Computer Science

Toggle sidebar
bg1
University of California at Berkeley
College of Engineering
Department of Electrical Engineering and Computer Sciences
Computer Science Division
CS 61c J. Wawrzynek
Spring 2006
Machine Structures
Midterm III
ID Number: Your Name:
Your TA: Your login:
Left ID: Seat No. Right ID:
This is an open-book exam. You are allowed to use any books and notes that you wish.
No calculators or electronic devices of any kind, please. You have 3 hours. Each question
is marked with its number of points.
This exam booklet should have 23 printed pages. Check to make sure that you have all
the pages. Fill out the information above completely and put your student ID neatly
on each page.
Show your answers in the space provided for them. Write neatly and be well organized.
If you need extra space to work out your answers, you may use the back of previous
questions. However, only the answers appearing in the proper answer space will be
graded.
Good luck!
problem maximum score
1 6pts
2 8pts
3 6pts
4 10pts
5 5pts
6 14pts
7 6pts
problem maximum score
8 7pts
9 3pts
10 4pts
11 11pts
12 7pts
13 3pts
total 90pts
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Midterm II Questions with Solution - Machine Structures | COMPSCI 61C and more Exams Computer Science in PDF only on Docsity!

University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Sciences Computer Science Division

CS 61c J. Wawrzynek Spring 2006

Machine Structures

Midterm III

ID Number: Your Name:

Your TA: Your login:

Left ID: Seat No. Right ID:

This is an open-book exam. You are allowed to use any books and notes that you wish. No calculators or electronic devices of any kind, please. You have 3 hours. Each question is marked with its number of points.

This exam booklet should have 23 printed pages. Check to make sure that you have all the pages. Fill out the information above completely and put your student ID neatly on each page.

Show your answers in the space provided for them. Write neatly and be well organized. If you need extra space to work out your answers, you may use the back of previous questions. However, only the answers appearing in the proper answer space will be graded.

Good luck!

problem maximum score

1 6pts

2 8pts

3 6pts

4 10pts

5 5pts

6 14pts

7 6pts

problem maximum score

8 7pts

9 3pts

10 4pts

11 11pts

12 7pts

13 3pts

total 90pts

1

  1. [6 points] CPU Pipelining.

Consider a MIPS processor with a pipeline structure different than the one discussed in class and the book. In this processor, the caches (instruction and data) and the ALU are pipelined to two stages each. Therefore the overall processor pipeline has the following eight stages: IF1, IF2, ID, EX1, EX2, DM1, DM2, WB While, the same mechanisms employed with the standard 5-stage MIPS pipeline are also employed on this new 8-stage pipeline (branch delay, ALU forwarding, load interlock), they may have limited effect, and therefore this pipeline may perform differently. Without reordering the instructions, write down the number of cycles needed to execute the following instruction sequences, including the time to drain the pipeline. The pipeline must be fully drained after each sequence.

(a) addi $1, $0, 1 addi $2, $0, 1 addi $3, $0, 3 addi $4, $0, 4 beq $1, $2, exit addi $5, $0, 5 addi $6, $0, 6 addi $7, $0, 7 exit: addi $8, $0, 8

15 cycles 2 points for correct answer -1 point for incorrect branch delay amount (b) addi $1, $0, 1 addi $2, $0, 2 addi $3, $1, $

11 cycles 2 points for correct answer

(c) addi $1, $0, 1 addi $2, $0, 2 lw $3, 100($0) addi $4, $3, $

14 cycles 2 points for correct answer -1 point for incorrect stall amount

(c) [1] Assume technolgy has advanced to a point that we can build a cache the same size as the physical memory. Assume also that we want to guarantee the highest possible cache hit rate in the system. Which of the following cache organization will result in the lowest circuit complexity, yet guaranteeing the highest possible hit rate? A. Direct-Mapped B. 2-Way Set Associative C. 4-Way Set Associative D. Fully Associative A, 1 point

(d) [2] For the purpose of data cache addressing, two students proposed the following two ways of assigning the address fields: Student A tag index offset 18 10 4

Student B index tag offset 10 18 4 With normal day-to-day mix of programs, which student’s design is more likely to have a higher cache hit rate? A. A has higher hit rate B. B has higher hit rate C. Design of A will cause cache malfunction D. Design of B will cause cache malfunction E. A and B are equally good. A, 1 point With an artificially generated random access pattern, which student’s design is more likely to have a higher cache hit rate? A. A has higher hit rate B. B has higher hit rate C. Design of A will cause cache malfunction D. Design of B will cause cache malfunction E. A and B are equally good. E, 1 point

  1. [6 points] Caches II.

Consider, the following is a program: #define SIZE 64 int* a; int main() { a = malloc(sizeof(int) * SIZE * SIZE); FillMatrix(a); FlushCache(); return CountZero(a); }

where FillMatrix(a) fills the array with values, and FlushCache() invalidates all entries in the cache. Now, consider the following 2 implementations of CountZero():

<<< Version A >>> | <<< Version B >>> int CountZero(int* a) { | int CountZero(int* a) { int row, col, z=0; | int row, col, z=0; for (row=0; row<SIZE; row++) | for (col=0; col<SIZE; col++) for (col=0; col<SIZE; col++) | for (row=0; row<SIZE; row++) if (a[row * SIZE + col]==0)| if (a[row * SIZE + col]==0) z += 1; | z += 1; return z; | return z; } | }

(a) [2] If the code is run in a system with 1MB of direct-mapped cache with block size of 32 words which version of CountZero() will run faster? A. Version A will be faster B. Version B will be faster C. They will take the same time to finish C, 2 points (b) [2] Given the same cache as above, if we change SIZE to 1024, which version of CountZero() will run faster? A. Version A will be faster B. Version B will be faster C. They will take the same time to finish A, 2 points (c) [2] If the cache is now changed to 4-way set associative, with the remaining parameter same as above, and SIZE = 1024, which version of CountZero() will run faster? A. Version A will be faster B. Version B will be faster

  1. [10 points] Virtual Memory.

Suppose we have a virtual memory system with the following parameters:

  • 16-bit word length
  • 16-bit virtual address
  • 4M bytes physical memory (M ≡ 1024*1024)
  • 512 byte page size
  • 8 entry TLB
  • A 8-bit wide process id (PID) register that stores the PID of the current running process (a) [2 points] The following resource limit is set for all programs running on this computer:
  • Fixed stack size of 8k bytes (k ≡ 1024) 2^13 / 29 = 2^4 = 16
  • Fixed heap size of 8k bytes 2^13 / 29 = 2^4 = 16
  • Maximum code size of 16k bytes 2^14 / 29 = 2^5 = 32
  • Maximum static data size of 4k bytes 2^12 / 29 = 2^3 = 8 Assuming the page table size is fixed for all processes, how many entries are needed in the page table for each process? 2 points 72 entries (b) [2 points] Assume each entry of the page table has the following fields: Name purpose OnDisk 1-bit field: 1 if this page is on harddisk, 0 otherwise VPN Virtual page number to be translated PPN Physical page number translated to What is the number of bits needed for the VPN field? 1 point

16 bit virtual addr − 9 bits of f set = 7 bits

What is the number of bits needed for the PPN field? 1 point

4 M = 2^22 → 22 bit phys addr − 9 bit of f set = 13 bits

(c) [5 points] The computer described on the previous page is currently running 2 programs, P 1 and P 2. The TLB is fully associative with a true LRU replace- ment policy. The table below shows the sequence of memory accesses generated by both P 1 and P 2. For each memory access indicate whether it generates a TLB hit (Hit), or miss (Miss). The TLB starts out empty. 0.5 points hit/miss, on 9 and 10, must be both misses or both hits

Time Current Process Memory Location (hex) Hit/Miss

1 P1 F000 miss

2 P1 0200 miss

3 P1 010F miss

4 P1 000C hit

OS Swap in P

5 P2 F000 miss

6 P2 0404 miss

7 P2 030C miss

8 P2 F10F hit

OS Swap in P

9 P1 020F miss or hit

10 P1 F004 miss or hit

(d) [1 points] When TLBs were first invented, they were often implemented with a fully-associative organization. In recent years, TLBs have been implemented with other cache organizations, such as set-associative or direct-mapped. Briefly, explain why this is so. 0.5 points for mentioning that TLBs are bigger nowadays 0.5 points for mentioning the benefits in terms of complexity, cost, or speed

  1. [14 points] Disk Performance.

You are responsible for the storage subsystem design of a new computer. Your system runs only one application. This application has the following characteristics:

  • Code size (including .text and .data section) is P bytes long.
  • It allocates dynamic memory in the heap during run time. The heap grows as needed but never exceed 1M bytes.
  • It is allocated a fixed stack size of 1 physical memory page. Your computer system has the following characteristics:
  • It has 1M bytes of physical memory (M ≡ 1024*1024)
  • It has a page size of P bytes; No virtual memory system; No cache.
  • Single cycle CPU, 100 MHz clock rate
  • It takes 4 CPU cycles to load/store one byte to/from memory You need to decide on which kind of storage should be used in this system. You are given 2 disk choices, D1, D2. The following table compares the 2 choices: D1 D Technology Magnetic Hard Disk USB Flash Drive RPM 10000 N/A Average Seek Time 8ms N/A Controller Overhead 4ms 1ms Transfer Speed (bytes per seconds) Read (from disk to CPU) 50000 P 125 P Write (from CPU to disk) 50000 P 25 P Both devices are block based (the block size is P ). A coprocessor handles loading data from, and writing data to disk. Therefore, no CPU cycle is needed. (a) [4] What is the time, TA 1 and TA 2 , to load the application from disk into the memory system using D1 and D2 respectively? Show your work. Loading takes 1 page = 1 block For D1:

TA 1 = Controller Overhaed + Avg Seek Time + Avg Rotation Time + Xfer Time

= 4 ms + 8ms + 0. 5 ∗ 60 /10000 +

P

50000 P

= 4 ms + 8ms + 3ms + 20μs = 15. 02 ms

For D2:

TA 2 = Controller Overhead + Xfer Time

= 1 ms +

P

125 P

= 9 ms

4 points: correct answer 3 points: formula mostly correct except for 1 error, or if more than P bytes are loaded 2 points: incorrect formalt, but showed effort

(b) [6] Assume your application has a page fault rate of f during run-time. That is, out of all memory operations (lw,sw), f of them generates page fault. This page fault rate does not include the page fault generated during application loading. Your application has the following instruction mix: Type CPI Usage arithmetic/branch instructions 1 80% memory read (lw) instructions 4 15% memory read (sw) instructions 4 5% Total number of instructions executed to finish the application is 10^8. Let,

TR 1 = time needed to read 1 page of memory from D TR 2 = time needed to read 1 page of memory from D TW 1 = time needed to write 1 page of memory to D TW 2 = time needed to write 1 page of memory to D TE 1 = expected execution time of the application using a system with D TE 2 = expected execution time of the application using a system with D

Expected execution time is the time the application is expected to spend fin- ishing all 10^8 instructions. Do not include the time to load your applications in your execution time calculation. Express TE 1 and TE 2 in terms of f , TR 1 , TW 1 , and TR 2 , TW 2. Clearly show your work. Assume all pages are dirty, each page fault takes 1 page write and 1 page read.

TE 1 = num of insts ∗ cycle time ∗ avg CP I = 108 ∗ 10 −^8 ∗ 1 ∗ 0 .8 + (0.15 + 0.05)(4 + f ∗ P ageF aultP enalty) = 0 .8 + 0.8 + 0. 2 f (TW 1 + TR 1 )/Cycle time = 1 .6 + 2 ∗ 107 f (TW 1 + TR 1 )

Similarly

C. Double the amount of memory to 2M bytes

D. none of the above

E. all of the above C

  1. [6 points] C Programming and Networks. In the following problem, a network packet has the following structure: <-------------------------- 32 bits --------------------------> +------------------------------+------------------------------+ | Source Address | +------------------------------+------------------------------+ | Destination Address | +------------------------------+------------------------------+ | reserved | Payload Length | +------------------------------+------------------------------+ | Payload | |. | |. | In the rest of this question we will use the term body of the packet to describe the payload and the 16 bits preceding it (its length). The program below should repeatedly read a packet from the network using readPacket(), check if the packet is meant for this computer, and invoke processPacket() on the body of the packet if it is.

1 /* gets a packet from the network and writes it into buf / 2 void readPacket(char buf); 3 4 /* processes the body / 5 void processPacket(char body); 6 7 char* readAndCheckPacket() { 8 char mydata[MAX_PACKET_SIZE]; 9 int* destination; 10 readPacket(mydata); 11 destination = (int)(&mydata); 12 destination += 4; 13 if (destination != ADDRESS_OF_THIS_COMPUTER) 14 return NULL; 15 16 return (mydata+10); 17 } 18 19 void main() { 20 char* data; 21 while(1) { 22 data = readAndCheckPacket(); 23 if (data != NULL) 24 processPacket(data); 25 } 26 }

  1. [7 points] I/O and MIPS assembly.

The following MIPS assembly program reads exactly 1024 keystrokes from the key- board and sends them over a modem in any order. It also performs “other tasks” while waiting for data. The Keyboard The code at intHandler is the interrupt handler for the keyboard. When a key is pressed on the keyboard, it triggers the interrupt. The interrupt handler is then involked and should then read the character corresponding to the pressed key from address 0x00FF0000. The Modem The modem’s status can be read from address 0x00FA0000; if this value is zero, the modem is ready to accept data. Data is sent to the modem by writing to address 0x00FFA0004. The Buffer Since the order of the keystrokes does not need to be preserved, the programmer has chosen to store them in a stack at address keyStack. The word at address stackTop contains the address of the top of the stack.

2 stack: .bytes 1024 3 stackTop: .word stack 4 5 intHandler: 6 la $k0, 0x00FF0000 # $k0 = address of keyboard data 7 lb $k0, 0($k0) # $k0 = the keystroke 8 la $k1, stackTop # $k1 = address of stackTop variable 9 lw $k1, 0($k1) # $k1 = address of top of stack 10 sb $k0, 0($k1) # mem[stackTop] = newly-read keystroke 11 addi $k1, $k1, 1 # $k1 = new top of stack 12 la $k0, stackTop # $k0 = address of stackTop variable 13 sw $k1, 0($k0) # stackTop = $k 14 eret 15 16 main: 17 la $t0, 0x00FA0000 # $t0 = address of modem status 18 lw $t1, 0($t0) # $t1 = modem status 19 bne $t1, $0, otherWork # if modem not ready, do other stuff 20 21 la $t0, stackTop # $t0 = address of stackTop 22 lw $t1, 0($t0) # $t1 = address of top of stack 23 la $t2, stack # $t2 = address of bottom of stack 24 beq $t2, $t1, otherWork # if stack top and bottom same, jump 25 26 addi $t1, $t1, -1 # $t1 = new top of stack 27 lb $t3, 0($t1) # $t3 = key at top of stack 28 sw $t1, 0($t0) # mem[stackTop] = new top of stack 29 la $t0, 0x00FA0004 # $t0 = address of modem output 30 sb $t3, 0($t0) # modem output = $t 31 32 otherWork: 33 # .... 34 j main 35

  1. [3 points] String Representation.

In contrast to the ANSI C standard, early Macintosh computers stored strings as a length byte followed by the bytes of the string, with no terminator. This style of string is called Pascal-style strings, since they were first used in the Pascal program- ming language.

[1 point] Are there any advantages to using Pascal strings instead of C strings? If so, name one. Yes, strings can contain the null character or strlen() runs in constant time [1 point] Are there any advantages to using C strings over Pascal strings? If so, name one. Yes, strings can be longer than 28 − 1 chars [1 point] Would either of your answers above change if Pascal strings used an int (assumed to be four bytes long) rather than a byte? If so, how? Yes, strings can now be up to 232 − 1 chars long, but every string requires an extra 3 bytes of space

  1. [4 points] MAL and TAL.

[1 point] The MAL (MIPS Assembly Language) instruction nop can be translated into TAL (True Assembly Language) in many different ways. Give two possible translations which have different opcodes or function fields. slt $0, $0, $ 0 add $0, $0, $ 0 0.5 points each [3 points] Imagine that the assembler writer wants to add a new MAL instruction called lwa, Load Word from Address, which takes two arguments: lwa $rd, addr The first argument is a destination register. The second argument to the instruc- tion is a 32-bit compile-time constant, just like the second argument to la. The instruction should load the 32-bit value at the memory location contained in addr and place this value in $rd. Show how the assembler should translate this MAL instruction into TAL. Your result should be a template containing $rd and addr somewhere, to be used by the assembler to generate one or more TAL instructions. What remains should be entirely TAL instructions. lui $at, upper(addr) ori $at, $at, lower(addr) lw $rd, 0($at) can substitute $rd for $at -1 for usng t/s/etc. registers -1 for using non-TAL instructions -1 for not dividing into upper and lower halves -1 for oring with $0 instead of $at, or not specifying ori operand