CSC231 Lab 1 Solutions 2017

From dftwiki3
Revision as of 20:18, 1 February 2017 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Program 1= <br /> <source lang="asm"> ;;; lab1 problem 1 ;;; D. Thiebaut ;;; ;;; Prints boxed string ;;; ;;; To assemble, link, and run: ;;; ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 20:18, 1 February 2017 (EST)


Program 1


;;; lab1 problem 1
;;; D. Thiebaut  
;;;
;;; Prints boxed string
;;;                
;;; To assemble, link, and run:  
;;;     nasm -f elf  helloWorld.asm  
;;;     ld -melf_i386 -o helloWorld helloWorld.o 
;;;     ./helloWorld        
;;;      

                section .data
msg	        db      "*********************************", 10
		db	"* Welcome to CSC231             *", 10
		db	"* Home of the Assembly Language *", 10
		db	"*********************************", 10
msgLen          equ     $-msg
	
                section .text
                global  _start
_start:

;;; print message                                                                                                                           
                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, msg        ; address of message to print    
                mov     edx, msgLen     ; # of chars to print  
                int     0x80

;;; exit                                                                                                                                    
                mov     ebx, 0
                mov     eax, 1
                int     0x80


Program 2


;;; lab1 problem 2
;;; D. Thiebaut  
;;;
;;; Prints boxed string, but declares only 1 line of stars
;;;                
;;; To assemble, link, and run:  
;;;     nasm -f elf  lab1_2.asm  
;;;     ld -melf_i386 -o lab1_2 lab1_2.o 
;;;     ./lab1_2        
;;;      

                section .data
msg	        db      "*********************************", 10
msgLen1		equ	$-msg
		db	"* Welcome to CSC231             *", 10
		db	"* Home of the Assembly Language *", 10
msgLen          equ     $-msg
	
                section .text
                global  _start
_start:

;;; print first 3 lines of message
	
                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, msg        ; address of message to print    
                mov     edx, msgLen     ; # of chars to print  
                int     0x80

;;; print first line one more time, to close the box

		mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, msg        ; address of first line
                mov     edx, msgLen1    ; # of chars in first line
                int     0x80
	
;;; exit                                                                                                                                    
                mov     ebx, 0
                mov     eax, 1
                int     0x80


Program 3


;;; helloWorld.asm 
;;; D. Thiebaut  
;;;           
;;; Prints a triangle of stars
;;;     
;;; To assemble, link, and run:  
;;;     nasm -f elf  helloWorld.asm  
;;;     ld -melf_i386 -o helloWorld helloWorld.o 
;;;     ./helloWorld        
;;;      

                section .data
Mesg            db      "*", 10
		db	"**", 10
		db	"***", 10
		db	"****", 10
		db	"*****", 10
MesgLen         equ     $-Mesg
	
                section .text
                global  _start
_start:

;;; print message                                                                                                                           
                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Mesg       ; address of message to print    
                mov     edx, MesgLen    ; # of chars to print  
                int     0x80

;;; exit                                                                                                                                    
                mov     ebx, 0
                mov     eax, 1
                int     0x80


Program 4


;;; helloWorld.asm 
;;; D. Thiebaut  
;;;                
;;; To assemble, link, and run:  
;;;     nasm -f elf  helloWorld.asm  
;;;     ld -melf_i386 -o helloWorld helloWorld.o 
;;;     ./helloWorld        
;;;      

                section .data
Mesg            db      "*****", 10
MesgLen         equ     $-Mesg
	
                section .text
                global  _start
_start:

;;; print message                                                                                                                           
                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Mesg+4     ; address of message to print    
                mov     edx, 2          ; 1 * and 1 line-feed
                int     0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Mesg+3     ; address of message to print    
                mov     edx, 3    	; 2 * and 1 line-feed
                int     0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Mesg+2     ; address of message to print    
                mov     edx, 4    	; 3 * and 1 line-feed
                int     0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Mesg+1     ; address of message to print    
                mov     edx, 5    	; 4 * and 1 line-feed
                int     0x80

                mov     eax, 4          ; write    
                mov     ebx, 1          ; stdout  
                mov     ecx, Mesg       ; address of message to print    
                mov     edx, 6          ; 5 * and 1 line-feed
                int     0x80
	
;;; exit                                                                                                                                    
                mov     ebx, 0
                mov     eax, 1
                int     0x80