Docsity

Exam 2 Solutions | Program and Data Representation 2004 | CS 2150, Exams of Computer Science

Toggle sidebar
bg1
Exam 2 CS216 – Anderson – Fall 2004 Page 1 of 11
CS 216 Exam 2 – Fall 2004 - SOLUTION
Name _______________________ Section _______________________
Email Address _________________ Student ID # ___________________
Pledge:
This exam is closed note, closed book, with the exception of the “IBCM Principles of
Operation” and the “Code Examples”, and the “Tiny Guide to x86 Assembly”. You may view
these on-line - at the computer in front of you off of the cs216 home page – you may NOT use
printed copies you may have brought with you. You may NOT refer to any other
notes/books/slides/old exams etc.
You will have an hour and fifty minutes total to complete the exam. You may use calculators if
needed (including the calculator on Windows).
Good Luck!
MAX SCORE
TOTAL 71
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Exam 2 Solutions | Program and Data Representation 2004 | CS 2150 and more Exams Computer Science in PDF only on Docsity!

CS 216 Exam 2 – Fall 2004 - SOLUTION

Name _______________________ Section _______________________

Email Address _________________ Student ID # ___________________

Pledge:

This exam is closed note, closed book, with the exception of the “IBCM Principles of Operation” and the “Code Examples”, and the “Tiny Guide to x86 Assembly”. You may view these on-line - at the computer in front of you off of the cs216 home page – you may NOT use printed copies you may have brought with you. You may NOT refer to any other notes/books/slides/old exams etc.

You will have an hour and fifty minutes total to complete the exam. You may use calculators if needed (including the calculator on Windows).

Good Luck!

MAX SCORE

TOTAL 71

  1. (4 points):

Modify the picture above to show what happens to the stack and the contents of the registers after the following instructions have been executed. Assume all values are decimal.

pop ecx

mov esi, [ebx + 1000]

mov [2 * eax], ebp

  1. (4 points) Given the following infix expression: ((a + b) / ((c - d) * (k + j ))) + z a) Write it as a prefix expression:

+ / + a b * - c d + k j z

b) Write it as a postfix expression:

a b + c d - k j + * / z +

The Stack

Memory Address

1004

1000

996

992

esi 250

ecx 996

988

ebx 0

eax 500

esp 1000

ebp 1004

4

1004

4

1004

  1. (3 points) Given the following AVL tree:

Insert the value 6 into the AVL tree above, doing any necessary rotations to maintain the AVL property.

7

10

5

4

3

5

7

6

4

3 10

  1. (3 points total) Describe the fetch execute cycle in pseudocode:

while (power is on) {

IR = mem [PC] PC++ Execute instruction in IR }

a) (7 points) Assuming that the compiler generates code using the C calling convention as discussed in class, draw a picture of the stack as it would appear immediately after the callee’s prolog in meal_plan has been executed. (location indicated approximately by the a)---Æ ) Be sure to include any values that would be pushed on to the stack by the caller’s ( main’s ) prolog (after start ---Æ ). For all of this question, assume that only the registers listed in the comments in each function and in main are used, and that only the registers that had to be saved were saved. Be sure to indicate what the contents of esp and ebp are by clearly drawing arrows from the boxes to the right to the stack.

Memory

Lower Addresses

Higher Addresses

b) (7 points) Draw a picture of the stack as it would appear immediately after the callee’s prolog in compare has been executed. (location indicated approximately by the b)---Æ ). (If you want, you can draw only the additional things pushed on the stack above what is already there in question a).) Be sure to indicate what the contents of esp and ebp are. Memory

Lower Addresses

Higher Addresses

Stuff from a)

main’s EBX

7

100

Return address

oranges

apples

Main’s ESI

esp

ebp

main’s EBP

esp

ebp

meal plan’s ECX

meal plan’s EDX

apples

Return address

meal plan’s EBP

answer

temp[2]

temp[1]

52

input b + 13

temp[0]

meal plan’s EDI

  1. (25 points) Please write the x86 code that implements a function remove_max as shown below. remove_max will find and return the maximum value in the array. In addition, remove_max will modify the array to write a value of 0 in place of the removed value. The integer size indicates how many elements are in the input array. You can assume that the array input contains only non-negative integer values. You can also assume that size >= 1. If more than one instance of the maximum value exists you should only remove the first instance.

Detailed comments will help us assign you partial credit in case your solution is not perfect, but are not required for full credit. You should follow the C++ calling convention as described in class. You should allocate space for the local variables shown below regardless of whether you use them in your code or not. Feel free to allocate more local variables if needed. Avoid saving registers whenever possible. Writing pseudo-code at first may be very helpful!! Remember that arrays are passed by reference in C++. Be sure to include the prolog and the epilog. You should refer to parameters and local variables as we have done in class and in the x86 handout.

Here are a few example calls in C++ and what the results should be:

When: my_array = {12, 0, 20, 36, 14}, remove_max (my_array, 5) returns 36 and modifies my_array to contain: {12, 0, 20, 0, 14} When: my_array = {1, 56, 10, 2, 56, 1, 24}, remove_max (my_array, 7) returns 56 and modifies my_array to contain: {1, 0, 10, 2, 56, 1, 24}

int remove_max (int input[], int size) { int location; int i;

(see next page for solution)

In case it is useful to you, the following table provides the operation code definitions for IBCM. It is the same as the table in class handouts.

OP Mnem Note OP Mnem Note 0 halt halt! 8 or logical OR mem into accum 1 io bit 4 I/O, bit 5 hex/ascii 9 xor logical XOR mem into accum 2 shift bit 4 shift/rotate, bit 5 left/right A not logical complement of accum 3 load load accum from mem B nop no operation, do nothing 4 store store accum to mem C jmp unconditional jump 5 add add mem to accum D jmpe jump to addr if accum is 0 6 sub subtract mem from accum E jmpl jump to addr if accum < 0 7 and logical AND mem into accum F brl jump to addr; set accum to value of PC just before jump

  1. (15 points) Write an IBCM program that implements the following pseudocode. Your answer should be machine code (encoded). First write symbolic IBCM instructions for significant partial credit. Then encode the IBCM instructions for full credit. For full credit on this question, encoded instructions and symbolic assembly is what is required. Comments will help us assign you partial credit in case your solution is not perfect, but are not required for full credit. You can assume that x will be an integer >= 0. Please clearly indicate your final answer.

read x; read y; while (x > y) { print x - 3; x--; } halt

Write your IBCM code on the next page….

mem loc label op addr comments C006 00 jmp start go to start 0000 01 x DW 0 variable x 0000 02 y DW variable y 0001 03 one DW constant 1 0003 04 three DW constant 3 0000 05 1000 06 start readH read in x 4001 07 store x store x 1000 08 readH read in y 4002 09 store y store y 3001 0A loop load x 6002 0B sub y x - y E015 0C jmpl xit If y > x goto xit D015 0D jmpe xit 3001 0E load x 6004 0F sub three 1800 10 printH print x - 3 3001 11 load x 6003 12 sub one x-- 4001 13 store x C00A 14 jmp loop 0000 15 xit halt