Difference between revisions of "CSC231 Homework 2 Solution"

From dftwiki3
Jump to: navigation, search
Line 124: Line 124:
 
int 0x80
 
int 0x80
  
mov ecx, dword[tempC]             ; restore ecx
+
mov ecx, dword[tempC]         ; restore ecx
mov edx, dword[tempD]             ; and edx
+
mov edx, dword[tempD]         ; and edx
 
dec edx
 
dec edx
  

Revision as of 16:40, 6 October 2008

The solution this week is provided by Rae.

Problem 1

Rae
CSC 231: Homework#2, Problem#2
Filename: hw2.txt

1. "a man a plan a c a nalp a nam a!"
2. eax: 0xe5b109ae
   ecx: 0x0
   edx: 0xc529abc
   ebx: 0x2061206e

Problem 2


;;; hw2a.asm
;;; Ellysha Raelen Recto
;;; 
;;; This program outputs the following using loops:
;;;
;;;  **********#
;;;   *********##
;;;    ********###
;;;     *******####
;;;      ******#####
;;;       *****######
;;;        ****#######
;;;         ***########
;;;          **#########
;;;           *##########
;;;          **#########
;;;         ***########
;;;        ****#######
;;;       *****######
;;;      ******#####
;;;     *******####
;;;    ********###
;;;   *********##
;;;  **********#
;;;
;;; In this specific program, I followed the requirement that not more
;;; than 5 'int 0x80' statements can be used.
;;;
;;; To assemble and run:
;;;
;;;     nasm -f elf -F  stabs hw2a.asm
;;;     ld -o hw2a hw2a.o
;;;     ./hw2a
;;; -------------------------------------------------------------------

EXIT    equ             1
WRITE   equ             4
STDOUT  equ             1
        
      	;; ------------------------------------------------------------
	;; data areas
	;; ------------------------------------------------------------

	section	.data
line1 	db	10,"         "
line2	dd	"**********##########",10
tempC	dd	10
tempD	dd	0
endl	db	10, 10
len	equ 	$-endl
        
	;; ------------------------------------------------------------
	;; code area
	;; ------------------------------------------------------------

	section	.text
	global	_start

_start:
	;;; this section prints out first 10 lines
	mov	ecx, 10		
	mov	edx, 0
for1:
	mov	dword[tempC], ecx
	mov	dword[tempD], edx
	
	mov	eax, WRITE		; prints out the spaces
	mov	ebx, STDOUT
	mov 	ecx, line1
	inc	edx
	int	0x80	
	
	mov	eax, WRITE		; prints out the *s and #s
	mov	ebx, STDOUT
	mov	ecx, dword[tempD]
	add	ecx, line2 	
	mov	edx, 11	
	int 	0x80		
	
	mov	ecx, dword[tempC]
	mov	edx, dword[tempD]
	inc	edx
	
	loop	for1
	
	;;; this section prints out last 9 lines
	mov	ecx, 9
	mov	edx, 9
for2:
	mov 	dword[tempC], ecx
	mov	dword[tempD], edx

	mov	eax, WRITE		; prints out the spaces		
	mov	ebx, STDOUT
	mov	ecx, line1
	int 	0x80

	mov	eax, WRITE		; prints out the *s and #s
	mov	ebx, STDOUT
	mov	ecx, dword[tempD]
	add	ecx, line2
	dec	ecx
	mov	edx, 11
	int 	0x80

	mov	ecx, dword[tempC]         ; restore ecx
	mov	edx, dword[tempD]         ; and edx
	dec 	edx

	loop 	for2

	;;; prints out next line
	mov	eax, WRITE
	mov	ebx, STDOUT
	mov	ecx, endl
	mov	edx, len
	int 	0x80
	
	;; exit()

	mov	eax,EXIT
	mov	ebx,0
	int	0x80		; final system call