Difference between revisions of "CSC231 Exercises On Functions"

From dftwiki3
Jump to: navigation, search
(New page: Back to Weekly Schedule. ---- =Exercise 1= What is the behavior of this loop? <code><pre> mov eax, 0 mov ecx, 10 for: call func1 ... loop for func1: add eax...)
 
Line 5: Line 5:
  
 
What is the behavior of this loop?
 
What is the behavior of this loop?
 +
{|
 +
|-
 +
|
 
<code><pre>
 
<code><pre>
 
mov eax, 0
 
mov eax, 0
Line 16: Line 19:
 
ret
 
ret
 
</pre></code>
 
</pre></code>
 +
|
 +
<font color="white">
 +
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>
 +
|}
  
 
=Exercise 2=
 
=Exercise 2=

Revision as of 07:26, 8 October 2008

Back to Weekly Schedule.


Exercise 1

What is the behavior of this loop?

	mov	eax, 0
	mov	ecx, 10
for:	call	func1
	...
	loop	for


func1:	add	eax, 1
	ret

the loop goes 10 times and the function is called 10 times, adding 1 to eax every time. Eax ends with 10 in it.

Exercise 2

Same question:

	mov	eax, 0
	mov	ecx, 10
for:	call	func1
	...
	loop	for


func1:	sub	ecx, 1
	ret

Exercise 3

Draw the stack as the processor executes this program:

	mov	eax, 0
	mov	ebx, 0
	mov	ecx, 10
for:	call	func1
	...
	loop	for



func1:	add	eax, 1
	call	func2
	ret

func2:	add	ebx, 1
	ret

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