CSC231 First Program

From dftwiki3
Revision as of 13:34, 4 September 2008 by Thiebaut (talk | contribs) (New page: <code><pre> ;;; ======================================================================= ;;; firstProg.asm ;;; Tsui Mei Chang ;;; This program prints descending rows of stars using loops ;;...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
;;; =======================================================================
;;; firstProg.asm
;;; Tsui Mei Chang
;;; This program prints descending rows of stars using loops
;;; example of output:
;;; 
;;; *********************
;;; ********************
;;; *******************
;;; ******************
;;; *****************
;;; ****************
;;; ***************
;;; **************
;;; *************
;;; ************
;;; ***********
;;; **********
;;; *********
;;; ********
;;; *******
;;; ******
;;; *****
;;; ****
;;; ***
;;; **
;;; *
;;; 
;;;  to	assemble, link and run:
;;; 
;;; 	nasm -f	elf -F	stabs firstProg.asm
;;; 	ld -o firstProg firstProg.o
;;; 	./firstProg
;;; 
;;; =======================================================================

	
;;; ==================================
;;; CONSTANTS
;;; ==================================
%assign ENDL       0x0A ; end of line
%assign SYS_EXIT   1
%assign SYS_READ   3
%assign SYS_WRITE  4

%assign STDIN      0
%assign STDOUT     1
        
;;; ==================================
;;; DATA SEGMENT
;;; ==================================
        section .data
stars      db	"*********************" ; the row of stars of be decremented
stars_len  equ	$ - stars               ; length of the message
len        dd	stars_len               ; stores the length of the star row
newline    db	ENDL                    ; the new line character
line_len   equ	1		        ; length of the newline


;;; ==================================
;;; CODE SEGMENT
;;; ==================================
        section	.text
        global	_start

_start:

        mov     ecx,21          ; get ready to print 21 lines
printloop:              
        call    printstars      ; calls the star printing function
        call    printline       ; calls the new line printing function

        dec     dword [len]     ; decrements the length of the line of stars
	
        loop    printloop       ; decrements ecx and starts loop again

        
        ;; exit program, return to OS
        mov     eax, SYS_EXIT   ; select exit sys call
        xor     ebx, ebx        ; exit code
        int     0x80


;;; ========================================================
;;; printline:   prints the row of stars with a given length
;;; REGISTERS MOFIFIED:  EAX, EBX, ECX, EDX
;;; ========================================================
printstars:
        section .bss
.temp   resd	1		   ; temporary var to store ecx

        section .text
        mov     eax, SYS_WRITE     
        mov     ebx, STDOUT        
        mov     edx, dword [len]   ; get VARIABLE message length
        mov     dword [.temp], ecx ; save ecx
        lea     ecx, [stars]       ; message to be output
        int     0x80
        mov     ecx, dword [.temp] ; restores original ecx
        ret

;;; ========================================================
;;; printline:  prints the new line
;;; registers mofified:  eax, ebx, ecx, edx
;;; ========================================================
        
printline:
        section .bss
.temp    resd 1			   ; temporary var to hold ecx

        section .text
        mov     eax, SYS_WRITE     
        mov     ebx, STDOUT        
        mov     edx, line_len      ; CONSTANT message length
        mov     dword [.temp], ecx ; stores ecx in temp before it is changed
        lea     ecx, [newline]     ; message to be output
        int     0x80
        mov     ecx, dword [.temp] ; restores original ecx
        ret