Difference between revisions of "CSC231 Exercises On Functions"
Line 6: | Line 6: | ||
=Exercise 1= | =Exercise 1= | ||
− | * Write a program that prints all the letters of the alphabet in a loop. Do not use a function | + | * 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 16: | Line 15: | ||
* Explore alternative ways using the '''push''' and '''pop''' operations | * Explore alternative ways using the '''push''' and '''pop''' operations | ||
+ | =Exercise 3= | ||
− | =Exercise | + | * 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: | ||
Line 39: | Line 43: | ||
<br /> | <br /> | ||
− | =Exercise | + | =Exercise 5= |
− | What is the behavior of this | + | What is the behavior of this program? |
{| | {| | ||
! width="300" | | ! width="300" | | ||
Line 47: | Line 51: | ||
|- | |- | ||
| | | | ||
− | < | + | <source lang="asm"> |
− | + | mov eax, 0 | |
− | + | mov ecx, 10 | |
− | for: call func1 | + | for: call func1 |
− | + | ||
− | + | ||
− | + | func1: add eax, 1 | |
+ | ret | ||
− | + | ;;; exit code | |
− | + | mov ebx, 0 | |
− | + | mov eax, 1 | |
− | </ | + | int 0x80 |
+ | </source> | ||
| | | | ||
<font color="white"> | <font color="white"> |
Revision as of 07:27, 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.
Contents
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
|
|
Exercise 5
What is the behavior of the loop below? Could it ever be endless?
|
|
Exercise 6
What is the behavior of this program? Draw the stack as the processor executes this program:
|
|
Exercise 7
Same question, but now observe that the programmer forgot the ret instruction at the end of the first function.
|
|
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 :-)?
|
|
Exercise 9
Same question, but now observe that we pop eax, not ebx...
|
|
Exercise 10
What do these 2 code sections do?
and
|
|