Difference between revisions of "CSC231 Exercises On Functions"
(→Exercise 5) |
|||
Line 3: | Line 3: | ||
Note: Highlight the white section on the right hand-side of each code section to see the solution. | Note: Highlight the white section on the right hand-side of each code section to see the solution. | ||
+ | =Exercise 1= | ||
+ | |||
+ | * Write a program that prints all the letters of the alphabet in a loop. Do not use a function. Use '''ecx''' and '''loop''' to control the looping. Make your program print 1 letter in each loop. | ||
+ | |||
+ | * use int 0x80 to print the character (which you'll store in a string). | ||
+ | |||
+ | =Exercise 2= | ||
+ | |||
+ | * Same exercise, but this time use a function that receives the character to be printed in al. | ||
+ | * Explore ways to save ecx in a temporary variable | ||
+ | * Explore alternative ways using the '''push''' and '''pop''' operations | ||
+ | |||
+ | |||
+ | =Exercise 3= | ||
+ | |||
+ | * What is the behavior of the stack, and the resulting values in the registers as this program is executing: | ||
+ | <br /> | ||
+ | <code><pre> | ||
+ | mov eax, 0x01234567 | ||
+ | mov ebx, 0x89ABCDEF | ||
+ | xor ecx, ecx | ||
+ | |||
+ | push ax | ||
+ | pop cx | ||
+ | |||
+ | push ax | ||
+ | push bx | ||
+ | pop ecx | ||
+ | |||
+ | call next | ||
+ | next: pop ecx | ||
+ | |||
+ | </pre></code> | ||
+ | <br /> | ||
− | =Exercise | + | =Exercise 3= |
What is the behavior of this loop? | What is the behavior of this loop? | ||
Line 31: | Line 65: | ||
|} | |} | ||
− | =Exercise | + | =Exercise 4= |
What is the behavior of the loop below? Could it ever be endless? | What is the behavior of the loop below? Could it ever be endless? | ||
{| | {| | ||
Line 59: | Line 93: | ||
|} | |} | ||
− | =Exercise | + | =Exercise 5= |
What is the behavior of this program? Draw the stack as the processor executes this program: | What is the behavior of this program? Draw the stack as the processor executes this program: | ||
{| | {| | ||
Line 89: | Line 123: | ||
|} | |} | ||
− | =Exercise | + | =Exercise 6= |
Same question, but now observe that the programmer forgot the '''ret''' instruction at the end of the first function. | Same question, but now observe that the programmer forgot the '''ret''' instruction at the end of the first function. | ||
{| | {| | ||
Line 118: | Line 152: | ||
|} | |} | ||
− | =Exercise | + | =Exercise 7 (Push and Pop)= |
What are the numbers stored in eax and ebx when the loop terminates? If we assume that the default stack is 2 KB long, what is the largest number of times the loop can iterate before the stack overflows (trick question :-)? | What are the numbers stored in eax and ebx when the loop terminates? If we assume that the default stack is 2 KB long, what is the largest number of times the loop can iterate before the stack overflows (trick question :-)? | ||
{| | {| | ||
Line 149: | Line 183: | ||
|} | |} | ||
− | =Exercise | + | =Exercise 8= |
Same question, but now observe that we pop eax, not ebx... | Same question, but now observe that we pop eax, not ebx... | ||
{| | {| | ||
Line 180: | Line 214: | ||
|} | |} | ||
− | =Exercise | + | =Exercise 9= |
What do these 2 code sections do? | What do these 2 code sections do? |
Revision as of 14:53, 26 October 2012
Back to Weekly Schedule.
Note: Highlight the white section on the right hand-side of each code section to see the solution.
Contents
Exercise 1
- Write a program that prints all the letters of the alphabet in a loop. Do not use a function. Use ecx and loop to control the looping. Make your program print 1 letter in each loop.
- use int 0x80 to print the character (which you'll store in a string).
Exercise 2
- Same exercise, but this time use a function that receives the character to be printed in al.
- Explore ways to save ecx in a temporary variable
- Explore alternative ways using the push and pop operations
Exercise 3
- What is the behavior of the stack, and the resulting values in the registers as this program is executing:
mov eax, 0x01234567
mov ebx, 0x89ABCDEF
xor ecx, ecx
push ax
pop cx
push ax
push bx
pop ecx
call next
next: pop ecx
Exercise 3
What is the behavior of this loop?
|
|
Exercise 4
What is the behavior of the loop below? Could it ever be endless?
|
|
Exercise 5
What is the behavior of this program? Draw the stack as the processor executes this program:
|
|
Exercise 6
Same question, but now observe that the programmer forgot the ret instruction at the end of the first function.
|
|
Exercise 7 (Push and Pop)
What are the numbers stored in eax and ebx when the loop terminates? If we assume that the default stack is 2 KB long, what is the largest number of times the loop can iterate before the stack overflows (trick question :-)?
|
|
Exercise 8
Same question, but now observe that we pop eax, not ebx...
|
|
Exercise 9
What do these 2 code sections do?
and
|
|