fa06-sl-s1 - ECE 3035 12:00pm Computing Mechanisms 4 problems 4 pages Exam One Solutions Problem 1(2 parts 25 points p1p5 Next 27 September 2006

fa06-sl-s1 - ECE 3035 12:00pm Computing Mechanisms 4...

This preview shows page 1 out of 4 pages.

You've reached the end of your free preview.

Want to read all 4 pages?

Unformatted text preview: ECE 3035 12:00pm Computing Mechanisms 4 problems, 4 pages Exam One Solutions Problem 1 (2 parts, 25 points) p1p5: Next: 27 September 2006 Background Identification addi swi addi $1, $0, Array 526 $4, $1, 4096 addi bne addi swi jr loop body … $1, $1, 4 $1, $4, Loop $1, $0, Array 527 $31 Loop: Fall 2006 # # # # I1 I2 I3 I4 # # # # # IA IB IC ID IE Part A (7 points) What are the value of $1 at the following times in the execution of the loop above? Assume Array: begins at 5000. $1 when I4 is first executed: $1 when I4 is last executed: $1 when IB is last executed: 5000 9092 9096 Part B (18 points) Write a code fragment that tests whether the difference between two bytes in memory are within a threshold of 50 (i.e., B1 − B2 ≤ 50 ). The first byte is pointed at by $1; the second byte is 1024 words above the first byte’s address. If the difference is not within the threshold, the final instruction should branch to the label Next:. Use any registers needed to perform this operation. Label Loop: Skip: Instruction Comment lbu $2, 0($1) # load B1 lbu $3, 4096($1) # load B2 sub $3, $3, $2 # B1 – B2 slt $5, $3, $0 # negative? beq $5, $0, Skip # skip is positive sub $3, $0, $3 # otherwise negate slti $5, $3, 51 # within threshold? beq $5, $0, Next # skip to next pixel 1 ECE 3035 12:00pm Computing Mechanisms 4 problems, 4 pages Exam One Solutions Problem 2 (2 parts, 30 points) Fall 2006 27 September 2006 Pointers and Arrays Part A (21 points) The following static variables are allocated in memory beginning at address 5000. Determine the numerical or character values for the following expressions. float float char double double int F = 2.37; *G = &F; S[9]= “Guess Who”; H = 6.081; *J = &H; K[3][2] = {{10, 22}, {34, 11}, {45, 63}}; G 5000 &S[3] 5011 S[6] ‘W’ J 5020 &J 5028 K[2][1] 63 &(K[2][1]) 5052 J++ 5028 Part B (9 points) The following MIPS code accesses a three-dimensional array Array[Lz][Ly][Lx]. In particular, it implements the C instruction: Element = Array[Z][Y][X]; using registers $3, $4, $5, and $6 to hold the variables Element, X, Y, and Z, respectively. Determine the size of the elements of Array (in bytes) and the size of two of its dimensions: Lx and Ly. Assume the array starts at the memory address labeled “Array”. Instruction addi $1, $0, 48 mult $1, $6 mflo $3 sll $1, $5, 4 add $3, $3, $1 add $3, $3, $4 sll $3, $3, 3 lw $3, Array($3) Element Size (in bytes): 8 bytes Lx: 16 entries 2 Ly: 3 entries ECE 3035 12:00pm Computing Mechanisms 4 problems, 4 pages Exam One Solutions Fall 2006 27 September 2006 Problem 3 (2 parts, 20 points) Flow Control Part A (12 points) Draw the control flow graph corresponding to the following C code. for (X = 0; X < MaxNum; X++) { Data[X][X] = Value; if (Data[Y][X] == 55) continue; Data[X][Y] = 0; Value++; } Y = Value * Y; X = 0; X < MaxNum T F Data[X][X] = Value; Data[Y][X] == 55 T F Data[X][Y] = 0; Value++; Y = Value * Y; X++; Part B (8 points) Turn this compound predicate if-then-else statement into the equivalent nested if-thenelse statement which does not use compound predicates (i.e., do not use the && and || operators). if ((!C || D) && A) x = x + y; else y = x – y; Answer: if (!C) if (A) x = x + y; else y = x – y; else if (D) if (A) x = x + y; else y = x – y; else y = x – y; 3 ECE 3035 12:00pm Computing Mechanisms 4 problems, 4 pages Exam One Solutions Fall 2006 27 September 2006 Problem 4 (5 parts, 25 points) Stack In this problem, you will write a recursive procedure that computes factorial. Use only the registers described in the table below. Your answer should fit in the boxes provided. For maximum credit, comment your instructions. register $0 $1 $2 description zero input value (n) output value register $3 $29 $31 description branch predicate stack pointer return address Part A (4 points) Write a code fragment that initializes the result to 1 and then skips to label Done if the input n is less than 2. The code fragment implementing Done will be defined later. Label Fact: addi Instruction $2, $0, 1 Comment # default result slti $3, $1, 2 # if N < 2 bne $3, $0, Done # then done Part B (8 points) Write a code fragment that preserves values on the stack that will be needed after the recursive call to Fact. Label addi Instruction $29, $29, -8 Comment # allocate room on stack sw $31, 4($29) # preserve RA sw $1, 0($29) # preserve N Part C (2 points) Write a code fragment that recursively calls Fact(n-1). Label addi jal Instruction $1, $1, -1 Comment # compute N - 1 Fact # call Fact(N-1) Part D (8 points) Write a code fragment that restores preserved values from the stack. Label lw Instruction $31, 4($29) lw $1, 0($29) addi Comment # restore RA # restore N $29, $29, 8 # deallocate stack space Part E (3 points) Write a code fragment that computes the result and returns from this subroutine. The label Done should be defined here. Label Done: mult Instruction $2, $1 Comment # N * Fact(N-1) mflo $2 # position Fact(N) jr $31 # return top caller 4 ...
View Full Document

  • Fall '08
  • Staff
  • Power of two, The Stack, Skip, code fragment

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes