Difference between revisions of "CSC231 Loop Exercises"
Line 7: | Line 7: | ||
=Exercise 2= | =Exercise 2= | ||
− | Modify the loop so that it sums up all the numbers from 1 to 100 | + | Modify the loop so that it sums up all the numbers from 1 to 100. |
=Exercise 3= | =Exercise 3= | ||
Line 60: | Line 60: | ||
<br /> | <br /> | ||
<onlydft> | <onlydft> | ||
− | = | + | ==Solution 1= |
+ | <source lang="asm"> | ||
+ | ; print all ints between 1 and 100, one per line. | ||
+ | %include "asm_io.inc" | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; data segment | ||
+ | ;; ------------------------- | ||
+ | section .data | ||
+ | |||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; code area | ||
+ | ;; ------------------------- | ||
+ | section .text | ||
+ | global asm_main | ||
+ | asm_main: | ||
+ | |||
+ | mov ecx, 100 | ||
+ | mov eax, 1 | ||
+ | |||
+ | for: | ||
+ | call print_int | ||
+ | call print_nl | ||
+ | inc eax | ||
+ | loop for | ||
+ | |||
+ | ret | ||
+ | |||
+ | </source> | ||
+ | |||
+ | =Solution 2= | ||
+ | <source lang="asm"> | ||
+ | %include "asm_io.inc" | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; data segment | ||
+ | ;; ------------------------- | ||
+ | section .data | ||
+ | |||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; code area | ||
+ | ;; ------------------------- | ||
+ | section .text | ||
+ | global asm_main | ||
+ | asm_main: | ||
+ | |||
+ | mov ecx, 100 | ||
+ | mov eax, 1 | ||
+ | mov ebx, 0 | ||
+ | |||
+ | for: add ebx, eax | ||
+ | call print_int | ||
+ | call print_nl | ||
+ | inc eax | ||
+ | loop for | ||
+ | |||
+ | call print_nl | ||
+ | mov eax, ebx | ||
+ | call print_int | ||
+ | call print_nl | ||
+ | |||
+ | ret | ||
+ | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
==Version 1== | ==Version 1== | ||
<br /> | <br /> |
Revision as of 08:31, 12 October 2012
--D. Thiebaut 11:21, 20 October 2010 (UTC)
Exercise 1
Write a loop using the LOOP instruction that prints all the numbers from 1 to 100, one per line
Exercise 2
Modify the loop so that it sums up all the numbers from 1 to 100.
Exercise 3
Modify the loop so that it prints all the numbers from n1 to n2, where n1 and n2 are dword variables.
Exercise 4
Modify exercise 1 so that the program prints the numbers 10 per line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14... 20 21 22 23... 30 ... 91 92 93 94... 100
Exercise 5
Review. Write a simple program that prints a line of 30 stars
******************************
Exercise 6
Using a loop, write a simple program that prints 30 lines of stars, such that the first line contains 30 stars, the next one 29 stars, then 28 stars, until the last line printed contains only 1 star.
****************************** ***************************** **************************** *************************** ************************** ************************* ************************ *********************** ********************** ********************* . . . *** ** *
Exercise 7
Same exercise, but this time the first line printed contains 1 star, the next 2 stars, and so on until the last one, which contains 30 stars.