Docsity

Midterm Exam 1 Problems - Computer Systems Engineering | ECE 391, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Midterm Exam 1 Problems - Computer Systems Engineering | ECE 391 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

ECE 391 Exam 1, Spring 2008

Thursday, February 14

Name:

• Be sure that your exam booklet has 11 pages.

• Write your name at the top of each page.

• This is a closed book exam.

• You are allowed one 8. 5 × 11 " sheet of notes.

• Absolutely no interaction between students is allowed.

• Show all of your work.

• Don’t panic, and good luck!

Problem 1 40 points

Problem 2 30 points

Problem 3 30 points

Total 100 points

Problem 1 (40 points): Short Answers

Please answer concisely. If you find yourself writing more than a sentence or two, your answer is probably wrong.

Part A (6 points): Recall the user-level test harness provided for your use with MP1. Describe one advantage and

one disadvantage of developing and using such a testing strategy when writing new kernel code, relative to doing all testing of the new code directly in the kernel.

Part B (6 points): Explain the role of the CAS (cascade) bus in Intel’s 8259A PIC. Specifically, why it is necessary,

and how is it used?

Problem 1, continued:

Part E (8 points): The following is a short description of the string copy() function:

void string copy(char *dest, const char *src);

The string copy() function shall copy the string pointed to by src into an array pointed to by dest.

Explain what is wrong with the following piece of code which is using the string copy() function and how it may affect a program’s run:

void foo() { char to_string[5]; string_copy(to_string, "this is a test."); }

Problem 1, continued:

Part F (8 points):

The following x86 assembly code compares the some value field in a structure against the immediate value 16. The offset constants of the structure fields are defined in the data section for your reference. Assume that my list head is the head of the list and is defined.

.data

Useful offset constants for accessing members of our structure

LOCATION = 0 SOME_VALUE = 2 ON_CHAR = 4 COUNTDOWN = 5 NEXT = 7

.text

some code here...

movl my_list_head, %edx test_loop: cmpl $0, %edx je test_loop_end cmpl 16, SOME_VALUE(%edx) je found_value movl NEXT(%edx), %edx jmp test_loop

found_value:

some more code ...

test_loop_end:

even more code...

Please indicate two bugs in this code and a required fix for each:

.long mp1 list head

sort by location: pushl %ebp movl %esp,$ebp pushl %ebx pushl %esi pushl %edi movl 8(%ebp),%eax movl mp1 list head,%edx imull $STRUCT SIZE,%eax pushl %eax call mp1 malloc addl $4,%esp cmpl $0,%eax je MALLOC FAIL movl $0,%ecx CHECK NEXT: cmpl $0,%edx je DONE INSERTING

Insert call to add to sorted array here.

incl %ecx movl NEXT(%edx),%edx jmp CHECK NEXT DONE INSERTING: popl %edi popl %esi popl %ebx leave ret

MALLOC FAIL: pushl %eax call mp1 free addl $4,%esp movl $0,%eax jmp DONE INSERTING

Problem 2, continued:

Part B (10 points):

Nick has implemented a new function called mp1 replace onoff that takes in the location, new character, new length, and onoff. This new function must follow the standard C calling conventions.

void mp1 replace onoff(int location, int onoff, long new character, long new length)

mp1 replace onoff uses only the registers %eax, %edi, %edx, and %esi throughout its execution.

Identify the mistakes involving calling conventions and the stack from these code snippets. The issues might be in either the calling code or the function. In the caller code, %eax contains the int for location, %ecx contains a boolean status zero extended word to long, %edi containes the new character zero extended byte to long, and%ebx contains new on length or off length zero extended word to long.

pushl %eax #push location pushl %ecx #push onoff pushl %edi #push new character pushl %ebx #push new length call mp1 replace onof addl $8,%esp popl %ecx popl %eax imull $2, %eax #prep location for mp1 poke ...

mp1 replace onoff: pushl %ebp movl %esp,%ebp pushl %edi movl 12(%ebp),%esi movl 16(%ebp),%eax movl 20(%ebp),%edi movl 24(%ebp),%edx ... #Find and replace the mp1 blink struct node #referenced by location with the corresponding #on char or off char and on length or off length #based on onoff ... movl %edx,%ebx #move in the return value popl %edi leave ret

Problem 3 (30 points): x86 Assembly

In some instances, you might want to implement a key based container using a simple linked list. It will be used as a central registry that will allow users to store and retrieve values quickly. Write the retrieval function that looks up a key and returns the corresponding registry value. Follow proper x86 calling conventions.

typedef struct registry node { int key; int value; struct registry node* next; } registry node;

registry node* registry head; #the head of a NULL terminated list

Returns a value given a key, or -1 if not found

C function definition:

int lookup key(int key);

lookup key:

(scratch paper)