Difference between revisions of "CSC231 Loop Exercises"

From dftwiki3
Jump to: navigation, search
(Exercise 3)
Line 42: Line 42:
 
<onlydft>
 
<onlydft>
 
=Possible Solutions=
 
=Possible Solutions=
 +
==Version 1==
 +
<br />
 
<source lang="asm">
 
<source lang="asm">
 
;;; printStars.asm
 
;;; printStars.asm
Line 83: Line 85:
 
</source>
 
</source>
 
<br />
 
<br />
 +
==Version 2==
 +
<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>
 
</onlydft>
 
<br />
 
<br />
 
[[Category:CSC231]][[Category:Exercises]]
 
[[Category:CSC231]][[Category:Exercises]]

Revision as of 08:21, 10 October 2012

--D. Thiebaut 11:21, 20 October 2010 (UTC)


Exercise 1

Review. Write a simple program that prints a line of 30 stars

******************************

Exercise 2

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 3

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.









...