



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




4 problems, 7 pages Exam One Solutions 26 September 2007
Problem 1 (3 parts, 22 points) MIPS Assembly Language
p1p5: addi $1, $0, Array (^) # IA addi $6, $0, Bright (^) # IB … Loop: (^) loop body (^) # IC … Next: addi $1, $1, 4 (^) # ID bne $1, $6, Loop (^) # IE
Part A (6 points) What are the value of $1 at the following times in the execution of the loop above? Assume Array: begins at 6000 and Bright: begins at 7024.
Part B (8 points) Write a code fragment that packs four unsigned eight bit values A, B, C, and D (stored in $1, $2, $3, and $4) in order into a single 32 bit word stored in $1. When complete,
value A should be stored in the least significant byte, while value D is stored in the most significant byte. Use only $1, $2, $3, $4, all of which can be modified.
Label Instruction Comment
Part C (8 points) In a recursive subroutine, two registers $1 and $2 and the unmoved return address of the subroutine's caller must be preserved on the stack prior to the recursive call. Write a MIPS code fragment that preserves these three words on the stack.
Label Instruction Comment
4 problems, 7 pages Exam One Solutions 26 September 2007
Problem 2 (3 parts, 30 points) Pointers and Arrays
The following static variables are allocated in memory beginning at address 5000. int A[] = {5, 98, 97, 36, 10}; int C = 25, D = 17, *P = A; double H = 3.14; double *J = &H; int K = 9; int *Q = &(A[1]); Part A (15 points) Determine the numerical values for the following expressions.
Part B (5 points) Can the following statement be implemented, given the declaration of A above? Explain why or why not? A = A + 1;
Part C (10 points) Write the MIPS code implementation of the dynamically allocated array access below in the smallest number of instructions. A pointer to the array (declared below) is stored in $3. Variables A, B, and C reside in $4, $5, and $6 respectively. Modify only $1 and $ and the indexed memory location.
int Array[8][6][32]; / array declaration /
Array[A][B][C]= 81; / implement this / Label Instruction Comment addi $1, $0, 192 # LxLy mult $4, $1 # ALxLy mflo $ sll $2, $5, 5 # BLx add $2, $2, $1 # ALxLy + BLx add $2, $2, $6 # ALxLy + BLx + C sll $2, $2, 2 # byte offset add $2, $2, $3 # + base of array addi $1, $0, 81 # value to store sw $1, 0($2) # store 81 at Array[A][B][C]
4 problems, 7 pages Exam One Solutions 26 September 2007
Problem 4 (2 parts, 25 points) C Programming and Make files
Part A (15 points) The C program below should count the frequency of letters in a text file and print a letter usage histogram. Fill in the body of the do-while loop to complete the program. Keep track of the number of letters encountered in NumLetters and keep a frequency count of each of the 26 letters in the LetterCount array. Only count characters that are one of the 26 letters (in either lower-case or upper-case) of the alphabet.
void LetterHistogram(FILE *FP) { int Char, I, NumLetters = 0; int LetterCount[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int getc(FILE *); do { Char = getc(FP);
NumLetters += 1; if (Char >= 'a' && Char <= 'z') LetterCount[Char - 'a'] += 1; else if (Char >= 'A' && Char <= 'Z') LetterCount[Char - 'A'] += 1; else NumLetters -= 1;
} while (Char != EOF); printf("Letter Occurrence Histogram\n"); for (I = 0; I < 26; I++) printf("[%c] %5d, %4.1f%\n", I + 'A', LetterCount[I], 100.0 * LetterCount[I] / NumLetters);
4 problems, 7 pages Exam One Solutions 26 September 2007
CC= gcc CFLAGS= -O
LN= $(CC) LNFLAGS=
LDLIBS= SOURCES= P1-4.c jpeg_decoder.c jpeg_decoder.h jpeg_encoder.c jpeg_encoder.h
INCLUDES= jpeg_decoder.h jpeg_encoder.h OBJECTS= P1-4.o jpeg_decoder.o jpeg_encoder.o
all: bright .c.o: $(CC) $(CFLAGS) -c $*.c bright: $(OBJECTS) $(LN) $(LNFLAGS) -o $@ $(OBJECTS) $(LDLIBS) clean: rm -f *.o
test: bright ./bright test1.jpg test1out.jpg
jpegdecoder.o: jpeg_decoder.c jpeg_decoder.h jpegencoder.o: jpeg_encoder.c jpeg_encoder.h P1-4.o: P1-4.c jpeg_decoder.h jpeg_encoder.h
Part B (10 points) Answer the following questions about the makefile fragments above.
What is the name of the linker used in this makefile?
gcc
What type of files are produced by the line that begins “.c.o:”?
Object files
What does “bright” depend on?
P1-4.o jpeg_decoder.o jpeg_encoder.o
What does $@ refer to in the line that links the program?
“bright”