Difference between revisions of "CSC231 Exercises On Functions"
(→Exercise 1) |
|||
Line 23: | Line 23: | ||
| | | | ||
<font color="white"> | <font color="white"> | ||
+ | <br> | ||
The loop goes 10 times and the function is called 10 times, adding 1 to eax every time. Eax ends with 10 in it. | The loop goes 10 times and the function is called 10 times, adding 1 to eax every time. Eax ends with 10 in it. | ||
</font> | </font> | ||
Line 29: | Line 30: | ||
=Exercise 2= | =Exercise 2= | ||
Same question: | Same question: | ||
+ | {| | ||
+ | ! width="300" | | ||
+ | ! width="300" | | ||
+ | |- | ||
+ | | | ||
<code><pre> | <code><pre> | ||
mov eax, 0 | mov eax, 0 | ||
Line 40: | Line 46: | ||
ret | ret | ||
</pre></code> | </pre></code> | ||
+ | | | ||
+ | <font color="white"> | ||
+ | <br> | ||
+ | The function decrements ecx by 1 every time through the loop. The loop is going to go twice as fast. | ||
+ | </font> | ||
+ | |} | ||
=Exercise 3= | =Exercise 3= | ||
Draw the stack as the processor executes this program: | Draw the stack as the processor executes this program: | ||
+ | {| | ||
+ | ! width="300" | | ||
+ | ! width="300" | | ||
+ | |- | ||
+ | | | ||
<code><pre> | <code><pre> | ||
mov eax, 0 | mov eax, 0 | ||
Line 60: | Line 77: | ||
ret | ret | ||
</pre></code> | </pre></code> | ||
+ | | | ||
+ | <font color="white"> | ||
+ | <br> | ||
+ | Just go through the motion and draw the stack. The return address of the instruction after the call gets pushed in the stack every time the processor executes a '''call'''. It is popped out of the stack every time the processor executes a '''ret'''.</font> | ||
+ | |} | ||
=Exercise 4= | =Exercise 4= |
Revision as of 07:33, 8 October 2008
Back to Weekly Schedule.
Contents
Exercise 1
What is the behavior of this loop?
|
|
Exercise 2
Same question:
|
|
Exercise 3
Draw the stack as the processor executes this program:
|
|
Exercise 4
Same question, but now observe that the programmer forgot the ret instruction at the end of the first function.
mov eax, 0
mov ebx, 0
mov ecx, 10
for: call func1
...
loop for
func1: add eax, 1
call func2
func2: add ebx, 1
ret