Difference between revisions of "CSC231 Loop Exercises"
(New page: Back to CSC231 Schedule ---- =Exercise 1= Review. Write a simple program that prints a line of 30 stars ****************************** =Exercise 2= Using a ...) |
|||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | [[ | + | --[[User:Thiebaut|D. Thiebaut]] 11:21, 20 October 2010 (UTC) |
+ | ---- | ||
+ | __NOTOC__ | ||
+ | =Exercise 0= | ||
− | + | Given a string that is all lowercase, flip it to uppercase. | |
=Exercise 1= | =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 and prints the sum at the end. | ||
+ | |||
+ | =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 | Review. Write a simple program that prints a line of 30 stars | ||
Line 9: | Line 34: | ||
****************************** | ****************************** | ||
− | =Exercise | + | =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. | 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. | ||
Line 30: | Line 55: | ||
* | * | ||
− | =Exercise | + | =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. | 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. | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <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"> | ||
+ | ;;; loop2.asm | ||
+ | ;;; prints all the numbers between 1 and 100 | ||
+ | ;;; sums them up | ||
+ | ;;; displays the sum at the end. | ||
+ | |||
+ | %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> | ||
+ | ==Solution 3== | ||
+ | <source lang="asm"> | ||
+ | ;;; loop3.asm | ||
+ | ;;; prints all the numbers between n1 and n2, sums them up, and prints | ||
+ | ;;; the sum | ||
+ | |||
+ | |||
+ | %include "asm_io.inc" | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; data segment | ||
+ | ;; ------------------------- | ||
+ | section .data | ||
+ | n1 dd 10 | ||
+ | n2 dd 20 | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; code area | ||
+ | ;; ------------------------- | ||
+ | section .text | ||
+ | global asm_main | ||
+ | asm_main: | ||
+ | |||
+ | mov ecx, dword[n2] | ||
+ | sub ecx, dword[n1] | ||
+ | inc ecx | ||
+ | mov eax, dword[n1] | ||
+ | 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 | ||
+ | |||
+ | ret | ||
+ | |||
+ | </source> | ||
+ | ==Solution 4== | ||
+ | <source lang="asm"> | ||
+ | ;;; loop4.asm | ||
+ | ;;; same as loop1.asm, but prints the | ||
+ | ;;; numbers 10 per line. | ||
+ | |||
+ | |||
+ | %include "asm_io.inc" | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; data segment | ||
+ | ;; ------------------------- | ||
+ | section .data | ||
+ | space db " ", 0 | ||
+ | |||
+ | ;; ------------------------- | ||
+ | ;; code area | ||
+ | ;; ------------------------- | ||
+ | section .text | ||
+ | global asm_main | ||
+ | |||
+ | asm_main: | ||
+ | ;;; n = 1 | ||
+ | ;;; for i in range( 1, 11 ): # loops 10 times | ||
+ | ;;; for j in range( 1, 11 ): # loops 10 times | ||
+ | ;;; print( n, end=" " ) | ||
+ | ;;; n += 1 | ||
+ | ;;; print() | ||
+ | ;;; | ||
+ | |||
+ | |||
+ | ;;; n = 1 | ||
+ | mov eax, 1 | ||
+ | |||
+ | mov ecx, 10 | ||
+ | |||
+ | for1: mov edx, ecx ; save ecx of loop1 | ||
+ | |||
+ | mov ecx, 10 | ||
+ | |||
+ | for2: call print_int ; print n | ||
+ | mov ebx, eax ; save n | ||
+ | |||
+ | mov eax, space ; print space | ||
+ | call print_string | ||
+ | mov eax, ebx ; restore n | ||
+ | inc eax ; n <- n+1 | ||
+ | |||
+ | loop for2 | ||
+ | |||
+ | call print_nl | ||
+ | |||
+ | mov ecx, edx | ||
+ | loop for1 | ||
+ | |||
+ | call print_nl | ||
+ | ret | ||
+ | |||
+ | </source> | ||
+ | |||
+ | ==Solution 6== | ||
+ | <br /> | ||
+ | <source lang="asm"> | ||
+ | ;;; printStars.asm | ||
+ | ;;; D. Thiebaut | ||
+ | ;;; | ||
+ | ;;; | ||
+ | ;;; | ||
+ | ;;; To assemble, link, and run: | ||
+ | ;;; nasm -f elf -F stabs printStars.asm | ||
+ | ;;; ld -melf_i386 -o printStars printStars.o | ||
+ | ;;; ./printStars | ||
+ | ;;; | ||
+ | |||
+ | section .data | ||
+ | |||
+ | stars db 0x0a, "******************************" | ||
+ | starsLen equ $-stars | ||
+ | |||
+ | addr dd stars | ||
+ | count dd starsLen | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | mov ecx, dword[count] | ||
+ | |||
+ | for: mov dword[count], ecx | ||
+ | mov eax, 4 | ||
+ | mov ebx, 1 | ||
+ | mov ecx, stars | ||
+ | mov edx, dword[count] | ||
+ | int 0x80 | ||
+ | |||
+ | mov ecx, dword[count] | ||
+ | loop for | ||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | ==Solution 7== | ||
+ | <br /> | ||
+ | <source lang="asm"> | ||
+ | ;;; printStars2.asm | ||
+ | ;;; D. Thiebaut | ||
+ | ;;; | ||
+ | ;;; | ||
+ | ;;; | ||
+ | ;;; To assemble, link, and run: | ||
+ | ;;; nasm -f elf -F stabs printStars.asm | ||
+ | ;;; ld -melf_i386 -o printStars printStars.o | ||
+ | ;;; ./printStars | ||
+ | ;;; | ||
+ | |||
+ | section .data | ||
+ | |||
+ | stars db 0x0a, "******************************" | ||
+ | starsLen equ $-stars | ||
+ | |||
+ | addr dd stars | ||
+ | count dd starsLen | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | mov ecx, dword[count] | ||
+ | |||
+ | for: mov dword[count], ecx | ||
+ | mov eax, 4 | ||
+ | mov ebx, 1 | ||
+ | mov ecx, stars | ||
+ | mov edx, starsLen | ||
+ | sub edx, dword[count] | ||
+ | int 0x80 | ||
+ | |||
+ | mov ecx, dword[count] | ||
+ | loop for | ||
+ | |||
+ | ;;; exit | ||
+ | mov ebx, 0 | ||
+ | mov eax, 1 | ||
+ | int 0x80 | ||
+ | |||
+ | |||
+ | </source> | ||
+ | </onlydft> | ||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:Exercises]] |
Latest revision as of 06:34, 21 October 2015
--D. Thiebaut 11:21, 20 October 2010 (UTC)
Exercise 0
Given a string that is all lowercase, flip it to uppercase.
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 and prints the sum at the end.
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.