Difference between revisions of "CSC231 Exercises On Functions"

From dftwiki3
Jump to: navigation, search
(Exercise 4)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[CSC231_Class_Page | Back]] to Weekly Schedule.
 
[[CSC231_Class_Page | Back]] to Weekly Schedule.
 
----
 
----
 +
Note: For some exercises, highlight the white section on the right hand-side of each code section to see the solution.
 +
[[Image:pushPop.jpg| right]]
  
Note: Highlight the white section on the right hand-side of each code section to see the solution.
 
 
=Exercise 1=
 
=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.
+
* Write a program that prints all the letters of the alphabet in a loop.  All the characters appear on the same line.  Do not use a function yet!   Use '''ecx''' and '''loop''' to control the looping.  Make your program print 1 letter per loop.
 
 
 
* use int 0x80 to print the character (which you'll store in a string).
 
* use int 0x80 to print the character (which you'll store in a string).
  
Line 15: Line 15:
 
* Explore alternative ways using the '''push''' and '''pop''' operations
 
* Explore alternative ways using the '''push''' and '''pop''' operations
  
 +
=Exercise 3=
  
=Exercise 3=
+
* Print all the characters on separate lines.  You cannot use a library function for printing the 0x0a line-feed character.
 +
* Explore a solution where the new-line character is printed at the same time the letter of the alphabet is printed.
 +
* Explore a solution where you have a separate ''myPrintLn'' function that prints a new-line character.
 +
 
 +
=Exercise 4=
  
 
* What is the behavior of the stack, and the resulting values in the registers as this program is executing:
 
* What is the behavior of the stack, and the resulting values in the registers as this program is executing:
 
<br />
 
<br />
<code><pre>
+
<source lang="asm">
 
         mov eax, 0x01234567
 
         mov eax, 0x01234567
 
mov ebx, 0x89ABCDEF
 
mov ebx, 0x89ABCDEF
Line 35: Line 40:
 
next: pop    ecx
 
next: pop    ecx
  
</pre></code>
+
</source>
 
<br />
 
<br />
  
=Exercise 4=
+
[[Media:file.pdf|.]]
  
What is the behavior of this loop?
+
=Exercise 5=
{|
 
! width="300" | &nbsp;
 
! width="300" | &nbsp;
 
|-
 
|
 
<code><pre>
 
mov eax, 0
 
mov ecx, 10
 
for: call func1
 
...
 
loop for
 
jmp    theEnd
 
  
func1: add eax, 1
+
What is the behavior of this program?
ret
 
theEnd: ...
 
</pre></code>
 
|
 
<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.
 
</font>
 
|}
 
 
 
=Exercise 5=
 
What is the behavior of the loop below?  Could it ever be endless?
 
 
{|
 
{|
 
! width="300" | &nbsp;
 
! width="300" | &nbsp;
Line 72: Line 53:
 
|-
 
|-
 
|
 
|
<code><pre>
+
<source lang="asm">
mov eax, 0
+
                mov eax, 0
mov ecx, 10
+
                mov ecx, 10
for: call func1
+
for:           call func1
...
 
loop for
 
jmp    theEnd
 
  
  
func1: sub ecx, 1
+
func1:         add eax, 1
ret
+
                ret
  
theEnd: ...
+
;;; exit code
</pre></code>
+
                mov    ebx, 0
 +
                mov    eax, 1
 +
                int    0x80
 +
</source>
 
|
 
|
 
<font color="white">
 
<font color="white">
 
<br>
 
<br>
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.  <br />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 executesThis will happen if we start with an odd number in ecx outside the loop.
+
The loop goes 10 times and the function is called 10 times, adding 1 to eax every timeEax ends with 10 in it.
 
</font>
 
</font>
 
|}
 
|}
Line 100: Line 81:
 
|-
 
|-
 
|
 
|
<code><pre>
+
<source lang="asm">
 
mov eax, 0
 
mov eax, 0
 
mov ebx, 0
 
mov ebx, 0
Line 116: Line 97:
 
func2: add ebx, 1
 
func2: add ebx, 1
 
ret
 
ret
</pre></code>
+
</source>
 
|
 
|
 
<font color="white">
 
<font color="white">
Line 130: Line 111:
 
|-
 
|-
 
|
 
|
<code><pre>
+
<source lang="asm">
 
mov eax, 0
 
mov eax, 0
 
mov ebx, 0
 
mov ebx, 0
Line 145: Line 126:
 
func2: add ebx, 1
 
func2: add ebx, 1
 
ret
 
ret
</pre></code>
+
</source>
 
|
 
|
 
<font color="white">
 
<font color="white">
Line 159: Line 140:
 
|-
 
|-
 
|
 
|
<code><pre>
+
<source lang="asm">
 
mov eax, 0
 
mov eax, 0
 
mov ebx, 0
 
mov ebx, 0
Line 175: Line 156:
 
func2: add ebx, 1
 
func2: add ebx, 1
 
ret
 
ret
</pre></code>
+
</source>
 
|
 
|
 
<font color="white">
 
<font color="white">
Line 190: Line 171:
 
|-
 
|-
 
|
 
|
<code><pre>
+
<source lang="asm">
 
mov eax, 0
 
mov eax, 0
 
mov ebx, 0
 
mov ebx, 0
Line 206: Line 187:
 
func2: add ebx, 1
 
func2: add ebx, 1
 
ret
 
ret
</pre></code>
+
</source>
 
|
 
|
 
<font color="white">
 
<font color="white">
Line 223: Line 204:
 
|-
 
|-
 
|
 
|
<code><pre>
+
<source lang="asm">
 
push eax
 
push eax
 
mov eax, ebx
 
mov eax, ebx
 
pop ebx
 
pop ebx
</pre></code>
+
</source>
  
 
and
 
and
  
<code><pre>
+
<source lang="asm">
 
xor eax, ebx
 
xor eax, ebx
 
xor ebx, eax
 
xor ebx, eax
 
xor eax, ebx
 
xor eax, ebx
</pre></code>
+
</source>
  
 
|
 
|

Latest revision as of 09:55, 21 October 2014

Back to Weekly Schedule.


Note: For some exercises, highlight the white section on the right hand-side of each code section to see the solution.

PushPop.jpg

Exercise 1

  • Write a program that prints all the letters of the alphabet in a loop. All the characters appear on the same line. Do not use a function yet! Use ecx and loop to control the looping. Make your program print 1 letter per 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

  • Print all the characters on separate lines. You cannot use a library function for printing the 0x0a line-feed character.
  • Explore a solution where the new-line character is printed at the same time the letter of the alphabet is printed.
  • Explore a solution where you have a separate myPrintLn function that prints a new-line character.

Exercise 4

  • 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 5

What is the behavior of this program?

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


func1:          add	eax, 1
                ret

;;; exit code
                mov     ebx, 0
                mov     eax, 1
                int     0x80


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 6

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 7

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 8 (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 9

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 10

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.