






















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!























This study guide provides example problems on material from the class outline. It
is valuable in helping you test your understanding of concepts from lecture. Many
of these problems are from old exams; so they also provide good exam preparation.
Solutions are provided to verify your answers.
If any errors are detected, please send mail to [email protected] or
[email protected] so future versions can be corrected.
Good Luck!
Scott and Linda Wills
Version 1. 18 August 2008 Copyright © 2008
1: Introduction to Computing Mechanisms
Problem IN-1 (3 parts) 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: What are the values of $1 at the following times in the execution of the loop above?
Assume Array: begins at 6000 and Bright: begins at 7024.
$1 when IC is first executed:
$1 when IC is last executed:
$1 when IE is last executed:
Part B: 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: 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
Problem IN-3 (1 part) MIPS and C
Part A: Consider the following MIPS code, which is the packed-data.asm example from the
first module http://www.ece.gatech.edu/academic/courses/ece3035/examples/intro/asm/packed- data.asm. Write a C program corresponding to this MIPS code by filling in body of the C program fragment provided below. Assume that the registers $2, $3, and $6 hold variables “Pixels,” “P,” and “Result,” respectively.
.data Pixels: .word -12869361 # decimal equivalent of 0xFF3BA10F
.text Dilate: lw $2, Pixels($0) andi $6, $2, 255 # mask: extract P0 (0xFF) srl $2, $2, 8 # logical shift right by 8 bits andi $3, $2, 255 # mask; extract P srl $2, $2, 8 # shift off P or $6, $3, $6 # $6: P1 or P andi $3, $2, 255 # mask; extract P srl $2, $2, 8 # shift off P2, $2 is right with just P or $6, $3, $6 # $6: P2 or P1 or P or $6, $2, $6 # $6: P3 or P2 or P1 or P jr $
int Pixels = -12869361; int main() { int P; int Result;
return Result; }
Problem IN-4 (2 parts) MIPS Assembly Language
Part A: Create an assembly language implementation of dot product on two 10 element integer
arrays. Here the values are placed in an array in static memory using the .word assembler directive. An assembly language shell program, shown below, is provided.
.data A: .word 243, 459, 896, 535, 264, 698, 268, 281, 921, 886 B: .word 864, 215, 781, 151, 435, 128, 276, 336, 790, 825 .text P1p1: addi $1, $0, A # set memory base of array A
jr $31 # return to operating system When your program completes, the result should be returned in register $3. The solution is best written and tested using MISASIM.
Part B: Create an assembly language function that computes the averages of even and odd numbers in a variable sized array. Here the values are placed in an array in static memory using the .word assembler directive. The number of values is implied by the address of the next value in static memory at label Next:. An assembly language shell program, shown below, is provided. Assume at least one even and one odd value occurs in the list.
.data Array: .word 243, 459, 896, 535, 264, 698, 268, 281, 921, 886 .word 864, 215, 781, 151, 435, 128, 276, 336, 790, 825 .word 501, 725, 835, 160, 300, 095, 481, 282, 515, 282 .word 662, 770, 776, 998, 758, 447, 758, 272, 015, 398 .word 042, 645, 565, 265, 105, 778, 739, 148, 309, 960 .word 903, 067, 469, 126, 673, 864, 658, 333, 170, 987 .word 565, 228, 235, 477, 568, 254, 628, 421, 788, 012 .word 246, 170, 746, 892, 586, 875, 055, 850, 885, 828 .word 717, 797, 971, 862, 269, 082, 824, 728, 650, 470 .word 740, 522, 232, 648, 323 Next: .word 00 .text P1p2: addi $1, $0, Array # set memory base
jr $31 # return to operating system
When your program completes, the average of all even numbers is returned in
Problem FC-2 (2 parts) Flow Control
Part A: Draw the control flow graph corresponding to the following C fragment, including the
flow within the compound predicate. for (I = 100; I > 0; I--) { if ((X[I] < NumX) && ((Y[I] < NumY) || (Z[I] != 0))) Z[I] = 5I; else Y[I] = 3I; } I = X[0] + Y[0];
Part B: Express the switch statement below as a flat compound predicate if-then-else statement that uses only logical operators (==, !=, ||). Do not use logical AND (&&).
switch (N) { case 0: x = x + y; break; case 10: y = x - y; break; case 20: x = x + y; break; default: y = x - y; }
Problem FC-3 (3 parts) Flow Control
Part A: Write the C code fragment that corresponds to this control flow graph. Use the appropriate looping construct. Where possible, compress nested if-then-else constructs into a flat if-then-else using compound logical predicates.
J > 0 T F V[J] > Min
J = X[0] * Y[0];
F T V[J] < Max F T Flag == OK F T X[J] = 10*J; Y[J] = J/10;
J = J - 2;
int J = 100;
Part B: Write a single C statement that corresponds to the following MIPS code. Assume $ holds A, $2 holds B, $3 holds C, and $4 holds D. Do not use an if-then-else.
addi $4, $0, 0 bne $3, $0, Set beq $1, $0, Continue beq $2, $0, Continue Set: addi $4, $0, 1 Continue: …
Part C: Turn this nested if-then-else statement into a flat compound predicate if-then-else
statement which uses only basic operators (such as == and !=) and logical && and || operators.
if P if Q A; else if R if S A; else B; else B; else B;
3: Storage
Problem ST-1 (3 parts) 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: Determine the numerical values for the following expressions.
A[5] P[1] Q[1]
Part B: Can the following statement be implemented, given the declaration of A above? Explain why or why not? A = A + 1;
Part C: 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 $2 and the indexed memory location.
int Array[8][6][32]; / array declaration / Array[A][B][C]= 81; / implement this / label instruction comment
Problem ST-2 (3 parts) Pointers and Structures
Part A: The following variables are allocated in memory beginning at address 5000. Complete the memory map below with the variable names at each word or byte in memory. Do not include the variable’s value.
int A; int B[] = {1, 2}; char C = ‘z’; char D[] = “Hello”; int *E; char *F;
Part B: Suppose the following code follows the definitions in Part A. List the values of the listed variables following execution of this code. E = B; A = *E + E[1]; F = &(D[1]); C = *F;
Part C: Consider the following structure declaration.
struct State { int Num; char Name[NAMELENGTH]; int YearPop[NUMYEARS]; float Growth; };
Suppose S is a pointer to a previously allocated and initialized State object. Write proper C statements to perform the following structure accesses.
Store initial year population in integer P Store first character of Name in character C Set Growth to percentage growth between years 0 and 1. (Don’t worry if it’s the max.)
Part E: Suppose $8 holds variable x and $11 holds variable y. What is the mathematical
expression computed by the following code fragment (e.g., “y = 2*y”)?
slt $5, $8, $ beq $5, $0, Next add $8, $11, $ Next: …
Answer :
Part F: Registers $13, $14, and $15 each contain a color pixel value (R, G, B, respectively) in
their lowest 8 bits (with 0’s in their upper 24 bits). Write code to pack them into an RGB color pixel representation (in the lower 24 bits of a word) and store it in an output image array whose base address is labeled Edges at the pixel offset stored in $2. Modify only $2, $13, $14, $15.
label instruction comment
Problem FP-2 (1 part) Parameter Passing & Activation Frames
Part A: The following C function includes a call by value parameter and two call by reference
parameters. Implement both the caller and called function in MIPS assembly. Use proper procedure linkage conventions.
/* This program tests different parameter passing conventions. */ #include int main() { int X = 33; int Y = 55; int Z = 44; int P; int Conditional_Exchange(int *, int *, int); P = 0; Conditional_Exchange(&X, &Y, P); P = 1; Conditional_Exchange(&Y, &Z, P); printf("X = %d, Y = %d, Z = %d\n", X, Y, Z); } int Conditional_Exchange(int *A, int *B, int C) { int Temp; if (C) { Temp = *A; *A = *B; *B = Temp; } return(C); } Start with the shell MIPS code provided below.
.text FP2: add $30, $29, $00 # set caller's FP addi $29, $29, -16 # create storage for locals X, Y, Z, P addi $01, $00, 33 # load value 33 sw $01, -4($30) # initialize x = 33 addi $01, $00, 55 # load value 55 sw $01, -8($30) # initialize Y = 55 addi $01, $00, 44 # load value 44 sw $01, -12($30) # initialize Z = 55
jal CXchange # call CXchange(&X, &Y, P);
jal CXchange # call CXchange(&Y, &Z, P);
add $29, $30, $00 # pop locals from stack jr $31 # return to operating system
Note: the solution is several lines long and is best written and tested using
MISASIM.
Part B: Assume the code above calls a function int Foo(int A, *B, C[]) that
properly completes and references its activation frame. Write short code fragments for Foo that
perform the access of the specified function parameters below. Assume Foo’s frame pointer is properly set and no parameters are present in registers (i.e., access all parameters from Foo’s
activation frame).
*$5 = B (dereference B and store the value in $5)
C[2] = $6 (stores the contents of register 6 into C[2])
A-- (decrements A)
return A (returns A as the return value)
Problem PC-2 (2 parts) Activation Frames
Part A: The function Bar (below left) calls function Foo after completing code block 1. Write MIPS assembly code that properly calls Foo. You should include all instructions between
code block 1 and code block 2. List the symbolic names and values for each stack
entry (below right). Bar is called by a jal instruction at address 2000. Foo returns 55.
int Bar() {
int A = 21, B = 33; char C[] = “Hi!”; int Foo(int *, char *);
(code block 1)
B = Foo(&A, C)
(code block 2) }
Bar’s FP 9900 9896 A = 21 9892 B = 33 9888 C = “Hi!” 9884
9880
9876
9872
9868
label instruction comment
Part B : Explain why an activation frame cannot be fully allocated by a called function.
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: Answer the following questions about the makefile fragments above.
What is the name of the linker used in this makefile?
What type of files are produced by the line that begins “.c.o:”?
What does “bright” depend on?
What does $@ refer to in the line that links the program?
7: Dynamic Typing and Interpretation
Problem DT-1 (3 parts) Dynamic Typing and Interpretation
Part A: Consider the following program fragment. Line number Instruction 10: int x; 11: float y = 3.14; 12: x = 5; 13: y = y + x; 14: x = “abc”; 15: x = x+y; If the programming language is statically typed (such as C), at which line of code would a type error first occur? Assume the language has C-like coercion rules.
Line number:.
If the programming language is dynamically typed , at which line of code would a type error first occur? Assume the language has C-like coercion rules.
Line number:.
Part B: Compare static versus dynamic typing in terms of:
Part C: Suppose the following object is allocated on the heap using malloc(). Assume the
heap pointer is 5000 and there is no free list. Show where A, B, C, and D are placed in memory. Record the object size (i.e., write the actual size value where it belongs) and show the heap pointer following the allocation. struct Thing { int A[2]; char B[7]; char *C; double D; } HP 5000
5004
5008
5012
5016
5020
5024
5028
5032