CS 232 Fall 2009 Practice Final Solutions

CS 232 Fall 2009 Practice Final Solutions - CS 232 Practice...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 232 Practice Final Exam: Solutions Fall 2009 1. Dynamic Arrays in MIPS (20 points) Let t0 be the base address of an array of 100 int s. Write MIPS code to double the size of the array so that it can hold 200 int s, while preserving the original array contents. # Start: $t0 = base address of int array, size = 100 li $a0, 800 # 200 ints = 800 bytes li $v0, 9 syscall move $t1, $v0 # copy base address of new chunk into $t1 addi $t2, $t0, 400 # pointer to end of original array loop: beq $t0, $t2, done lw $t3, 0($t0) sw $t3, 0($t1) addi $t0, $t0, 4 addi $t1, $t1, 4 j loop done: move $t0, $v0 # End: $t0 = base address of int array, size = 200, first 100 values as before 2. Caches (20 points) Consider the following C++ program: char a[LENGTH]; // array of bytes STEP int sum;------+------------------------------------- for(int i = 0; i < 10000; ++i) LENGTH| 1 2 4 8 16 32 64 128 for(int j = 0; j < LENGTH; j += STEP)------+------------------------------------- sum += a[j]; // statement X 8 | 7 7 7 7 6 7 7 7 16 | 8 7 7 6 6 7 7 7 32 | 7 8 7 7 7 7 6 7 64 | 16 21 45 45 7 8 8 9 128 | 14 25 47 45 45 7 7 7 256 | 17 25 43 45 45 46 8 7 (a) First consider the case when STEP = 1 . If the cache size is n bytes (i.e., the cache can hold n bytes of data) and LENGTH n , what proportion of the array accesses in statement X will result in a cache hit? Solution : Since we access the array left-to-right in steps of size 1, after the first iteration of the outer loop, the entire array will fit within the cache. Hence, for every subsequent iteration of the outer loop, the array access in statement X will result in a cache hit. (b) Answer part (a) for the case when LENGTH > n . Solution : Here, the array length is larger than the cache. In each iteration of the outer loop, as the later bytes of the array are accessed, the new blocks will replace the earliest (and hence, least recently used) parts of the array. Thus, there will always be cache misses in each iteration of the outer loop. The proportion of cache hits will be about 1 1 block size . (c) Based on your answers to parts (a) and (b) and the given data, what is the cache size? Solution : We see that we go from nearly 100% hits to significantly less than 100% hits as soon as LENGTH > n . The decreased percentage of hits is reflected in a higher average time for statement X . According to the data, this happens when LENGTH jumps from 32 to 64. Hence, the cache size is 32 bytes....
View Full Document

Page1 / 5

CS 232 Fall 2009 Practice Final Solutions - CS 232 Practice...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online