Difference between revisions of "CSC231 Exercises On Functions"

From dftwiki3
Jump to: navigation, search
(Exercise 2)
(Exercise 5)
Line 118: Line 118:
 
|}
 
|}
  
=Exercise 5=
+
=Exercise 5 (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 129: Line 129:
 
mov ebx, 0
 
mov ebx, 0
 
mov ecx, 10
 
mov ecx, 10
for: push ebx
+
for:   push ebx
 
     call func1
 
     call func1
 
pop ebx
 
pop ebx

Revision as of 09:30, 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.

Exercise 1

What is the behavior of this loop?

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

func1:	add	eax, 1
	ret
theEnd: ...


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

What is the behavior of the loop below? Could it ever be endless?

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


func1:	sub	ecx, 1
	ret

theEnd: ...


The function decrements ecx by 1 every time through the loop. So ecx is decremented once by the function, once by the loop instruction. The loop is going to go twice as fast.
The loop instruction stops if ecx is 1 when the instruction is executed. We risk endless looping if ecx is either 2 or 0 when the loop instruction executes. This will happen if we start with an odd number in ecx outside the loop.

Exercise 3

What is the behavior of this program? 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


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.

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


Just go through the motion and draw the stack. Now the problem is that when the main program calls func1 the first time, the return address for the ellipses is pushed in the stack, then the processor starts executing the add eax,1 function. Then it executes the call func2 instruction and pushes the address of the add ebx,1 instruction in the stack. Then, when it executes the ret instruction, the address of add ebx,1 is popped out, and ebx is incremented by 1. Then the ret is executed and we return to the main function. The end result is that func1 is executed once, and func2 twice!

Exercise 5 (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 :-)?

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

func1:	add	eax, 1
	call	func2
	ret

func2:	add	ebx, 1
	ret


The loop does not modify ebx!

Exercise 6

Same question, but now observe that we pop eax, not ebx...

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

func1:	add	eax, 1
	call	func2
	ret

func2:	add	ebx, 1
	ret


This one requires the use of the debugger for fully understanding it! :-)

Exercise 7

What do these 2 code sections do?

   
	push	eax
	mov	eax, ebx
	pop	ebx

and

	xor	eax, ebx
	xor	ebx, eax
	xor	eax, ebx


They both swap the contents of eax and ebx.