Docsity

Computing Mechanisms Study Guide Solutions - Mechanisms Computing | ECE 3035, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035 Computing Mechanisms Study Guide Solutions Version 1.0
1
10: Solutions
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 value 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: 6000
$1 when IC is last executed: 7020
$1 when IE is last executed: 7024
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
sll $2, $2, 8 #move B 8 bits left
or $1, $1, $2 # pack B A
sll $3, $3, 16 #move C 8 bits left
or $1, $1, $3 # pack C B A
sll $4, $4, 24 #move D 8 bits left
or $1, $1, $4 # pack D C B A
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
addi $29, $29, -12 # adjust SP
sw $31, 8($29) # push RA
sw $1, 4($29) # push $1
sw $2, 0($29) # push $2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Computing Mechanisms Study Guide Solutions - Mechanisms Computing | ECE 3035 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

10: Solutions

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 value 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: 6000 $1 when IC is last executed: 7020 $1 when IE is last executed: (^7024)

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 sll $2, $2, 8 #move B 8 bits left or $1, $1, $2 # pack B A sll $3, $3, 16 #move C 8 bits left or $1, $1, $3 # pack C B A sll $4, $4, 24 #move D 8 bits left or $1, $1, $4 # pack D C B A

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 addi $29, $29, -12 # adjust SP sw $31, 8($29) # push RA sw $1, 4($29) # push $ sw $2, 0($29) # push $

Problem IN-2 (5 parts) MIPS Assembly Language

Part A: Write a MIPS program fragment that computes “ − 17 ⋅( B − C )”and puts the result in

register $6. Assume B and C are in registers $1 and $2, respectively. Use a minimum number of instructions and registers. You may reuse registers $1 and $2.

sub $1, $1, $2 # B = B – C addi $6, $0, -17 # $6 = - mult $1, $6 # Lo = -17 * (B – C) mflo $6 # $6 = result

Part B: Suppose A is stored in memory location 1020 and B is stored in memory location 1024. Write a MIPS program fragment that computes “ 256 ⋅ ( A + B / 16 )” and stores the result at

memory location 1028. Use a minimum number of instructions and registers.

lw $1, 1020($0) # $1 = mem[1020] lw $2, 1024($0) # $2 = mem[1024] sra $2, $2, 4 # $2 = $2 / 16 add $2, $2, $1 # $2 = A+B/ sll $2, $2, 8 # $2 = 256*(A+B/16) sw $2, 1028($0) # mem[2028] = $

Part C: Write a MIPS program fragment to jump to address 0xABCD1234.

lui $1, 0xABCD # load upper word ori $1, $1, 0x1234 # combine with lower word jr $1 # jump to 0xABCD

note: since MISASIM does not support hexadecimal value specification, a decimal

equivalent would have to be specified.

Part D: Suppose an image processing system stores a 512x256 pixel image in memory. Each pixel is represented by 8 bits and they are store contiguously in memory. How much memory (in kilobytes) does this require? How many bits are needed to address 1 pixel?

512 x 256 pixels x 1 byte/pixel = 2 9 x 2^8 = 2^17 = 128 Kbytes To address 128 Kbytes, an address would require at least 17 bits.

Part E: Write a MIPS fragment that exchanges two registers ($1 and $2) without using any other registers or memory. (hint: think xor).

xor $1, $1, $2 # $1 = $1 xor $ xor $2, $1, $2 # $2 = $1 xor $2 xor $2 = $ xor $1, $1, $2 # $1 = $1 xor $2 xor $1 = $

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. When your program completes, the result should be returned in register $3.

This program computes the dot product of two ten element integer arrays.

.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 IN4A: addi $1, $0, A # set memory base of array A addi $2, $0, B # set memory base of array B addi $3, $0, 0 # initialize running sum Loop: lw $4, 0($1) # load current element of A lw $5, 40($1) # load current element of B mult $4, $5 # multiply corresponding elts mflo $4 # of A and B add $3, $3, $4 # add product to running sum addi $1, $1, 4 # update array address bne $1, $2, Loop # exit when array address = B jr $31 # return to operating system

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:. Assume at least one even and one odd value occurs in the list. When your program completes, the average of all even numbers is returned in $4 and the average of all odd numbers is returned in $5.

This program finds the largest, smallest, and average values for

an integer array.

.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

$1 = array pointer, $2 = array size, $3 = input, $4, = min,

$5 = max, $6 = avg, $7 = pred

.text IN4B: addi $1, $0, Array # set memory base addi $2, $0, Next # load end condition sub $2, $2, $1 # compute size in bytes add $6, $0, $0 # clear average lw $4, Array($0) # initialize min lw $5, Array($0) # initialize max addi $1, $0, 4 # first element offset Loop: lw $3, Array($1) # load next value add $6, $6, $3 # add to running sum slt $7, $3, $4 # compare input to min beq $7, $0, Skip1 # skip is >= add $4, $3, $0 # otherwise update min Skip1: slt $7, $5, $3 # compare max to input beq $7, $0, Skip2 # skip if >= add $5, $3, $0 # otherwise update max Skip2: addi $1, $1, 4 # adjust array pointer bne $1, $2, Loop # loop until end is reached sra $2, $2, 2 # compute size in words div $6, $2 # compute average mflo $6 # move result jr $31 # return to operating system

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];

I > 0

T F

X[I] < NumX

I = X[0] + Y[0];

F T

Y[I] < NumY F T

Z[I] != 0 F T

Y[I] = 3 * I; Z[I] = 5 * I;

I--

I = 100;

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; }

if ((N == 0) || (N == 20)) x = x + y; else 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;

for (J=100; J > 0; J -= 2) if (V[J] > Min && (V[J] < Max || Flag == OK)) Y[J] = J / 10; else X[J] = 10 * J; J = X[0] * Y[0];

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: … D = C || (A && B);

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; if (P && (Q ||(R && S))) A; else B;

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] 25 P[1] 98 Q[1] 97

*(A+3) 36 J 5032 &K 5044

Part B: Can the following statement be implemented, given the declaration of A above? Explain why or why not? A = A + 1; No because A is a constant base address

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 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]

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;

5000 A → → →

5004 B[0] → → →

5008 B[1] → → →

5012 C D[0] D[1] D[2]

(^5016) D[3] D[4] D[5] slack

5020 E → → →

5024 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;

A 3 C (^) ‘e’ D 5013 E 5004 F (^5014)

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 P = S->YearPop[0];

Store first character of Name in character C

C = S->Name[0];

Set Growth to percentage growth between years 0 and 1

S->Growth = 100.0 * (S->YearPop[1] – S->YearPop[0]) / S->YearPop[0];

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 : (^) y = min(x, y)

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 sll $13, $13, 8 # shift red left one byte ori $13, $13, $14 # combine red and green sll $13, $13, 8 # shift red & green left one byte ori $13, $13, $15 # add blue to red & green sw $13, Edges($2) # store packed word in to memory

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); }

This program tests different parameter passing conventions.

.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

set P = 0 and prepare to call CXchange here.

sw $00, -16($30) # P = 0 addi $29, $29, -24 # allocate beginning of CXchange's frame sw $31, 20($29) # push return address sw $30, 16($29) # push current FP (main's FP) addi $01, $30, -4 # form &X sw $01, 12($29) # push input &X addi $1, $30, -8 # form &Y sw $01, 8($29) # push input &Y lw $01, -16($30) # get P sw $01, 4($29) # push input P

(note space for uninit'd return val is at 0($29))

jal CXchange # call CXchange(&X, &Y, P);

lw $30, 16($29) # restore main's FP lw $31, 20($29) # restore main's return addr

Problem PC-1 (1 part) 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. Include all instructions between code block 1 and code block 2. Symbolically label all required stack entries and give their values if they are known (below right).

int Bar() {

int V[] = {9, 4, 1}; int R = V[2];

(code block 1)

R = Foo(R, &R, V)

(code block 2) }

Bar’s FP 9900 XXX XXX 9896 V[2] 1 9892 V[1] 4 9888 V[0] 9 SP 9884 R 1 9880 RA N/A

9876 FP 9900

9872 R 1

9868 &R 9884

9864 V 9888

9860 RV N/A

9856

label instruction comment addi $29, $29, -24 # allocate activation frame sw $31, 20($29) # preserve RA sw $30, 16($29) # preserve FP lw $1, -16($30) # copy R's value sw $1, 12($29) # store in activation frame addi $1, $30, -16 # compute R's effective address sw $1, 8($29) # store in activation frame addi $1, $30, -12 # computer V's base address sw $1, 4($29) # store in activation frame jal Foo # call Foo lw $31, 20($29) # restore RA lw $30, 16($29) # restore FP lw $1, 0($29) # load return value sw $1, -16($30) # store in R addi $29, $29, 24 # deallocate activation frame

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)

lw $5, 8($30) # get pointer B lw $5, 0($5) # dereference B

C[2] = $6 (stores the contents of register 6 into C[2])

lw $1, 4($30) # load base of array C sw $6, 8($1) # store $6 at C[2]

A-- (decrements A) lw $1, 12($30) # load A addi $1, $1, -1 # decrement A sw $1, 12($30) # store A

return A (returns A as the return value)

lw $1, 12($30) # load A sw $1, 0($30) # store in RV slot add $29, $30, $0 # deallocate activation frame jr $31 # return to caller

Problem SS-1 (2 parts) C Programming and Make files

Part A : 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);

Part B: 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”

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: 14.

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: 15.

Part B: Compare static versus dynamic typing in terms of:

  1. runtime performance: static typing is faster at runtime (less runtime overhead)
  2. compilation time: dynamic typing has shorter compile time (no type checking)

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 size size size size: 28

5004 A[0] A[0] A[0] A[0]

5008 A[1] A[1] A[1] A[1]

5012 B[0] B[1] B[2] B[3]

5016 B[4] B[5] B[6] slack

5020 *C^ *C^ *C^ *C

5024 D^ D^ D^ D

5028 D^ D^ D^ D

HP 5032