CSC231 Homework 5 Solutions 2010

From dftwiki3
Revision as of 10:54, 19 November 2010 by Thiebaut (talk | contribs) (Created page with '--~~~~ ---- =Program 1= <code><pre> ;;; ; File: hw5a.asm ;;; ; Author: Tiffany Q. Liu ;;; ; Acct: 231a-ac ;;; ; Date: October 28, 2010 ;;; ; ;;; ; Desc: Print the N words in ms…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 14:54, 19 November 2010 (UTC)


Program 1

 ;;; ; File: hw5a.asm
;;; ; Author: Tiffany Q. Liu
;;; ; Acct: 231a-ac
;;; ; Date: October 28, 2010
;;; ;
;;; ; Desc: Print the N words in msg such that each word is on its own line and 
;;; ;       every word is capitalized (first letter in upper case, other letters
;;; ;       in lower case). Task is done using loops.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf -F  stabs hw5a.asm
;;; ;     ld -melf_i386 -o hw5a hw5a.o
;;; ;     ./hw5a
;;; ;
;;; ; Output:
;;; ; All
;;; ; Programmers
;;; ; Are
;;; ; Playwrights
;;; ; And
;;; ; All
;;; ; Computers
;;; ; Are
;;; ; Lousy
;;; ; Actors
;;; ; -------------------------------------------------------------------

	EXIT    equ             1
	WRITE   equ             4
	STDOUT  equ             1

;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

	section	.data
msg	db	3, "ALL", 11, "PROGRAMMERS", 3, "ARE", 11, "PLAYWRIGHTS"
	db	3, "AND", 3, "ALL", 9, "COMPUTERS", 3, "ARE", 5, "LOUSY"
	db	6, "ACTORS"
N	equ	10
lf	db	0x0a

;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

	section	.text
	global	_start

_start:	mov	ecx, N		; init loop counter, ecx <- 10
	mov	ebx, msg	; ebx points to first index of msg (# letters in
				; in the proceding string)

forOut:	push	ecx		; save counter in stack
	mov	eax, WRITE	; eax <- WRITE, prepare to print to screen
	push	ebx		; save pointer ebx in stack
	inc	ebx		; increment ebx to point to next byte (first 
				; char of current string to print)
	mov	ecx, ebx	; ecx <- string to print to screen
	mov	ebx, STDOUT	; eax <- STDOUT, prepare to print to screen
	mov	edx, 1		; edx <- 1, print only 1 character in the string
	int	0x80		; print character to screen

	pop	ebx		; retain ebx (points to string length) from 
				; stack
	mov	ecx, 0		; ecx <- 0, clear out ecx
	mov	cl, byte[ebx] 	; cl <- number of letters in curr string to 
				; print
	dec	ecx		; ecx <- ecx - 1, since first character already
				; printed
	add	ebx, 2		; ebx <- ebx + 2, increment ebx to point to next
				; character to print

forIn:	push	ecx		; save counter in stack 
	mov	eax, WRITE	; eax <- WRITE, prepare to print to screen
	mov	ecx, ebx	; ecx <- string to print to screen
	add	dword[ecx], 0x20; convert from upper case to lower case
	inc	ebx		; ebx <- ebx + 1, increment ebx to point to next
				; character in string
	push	ebx		; save pointer in stack
	mov	ebx, STDOUT	; ebx <- STDOUT, prepare to print to screen
	mov	edx, 1		; edx <- 1, print only 1 character in the string
	int	0x80		; print character to screen
	pop	ebx		; retain pointer from stack
	pop	ecx		; retain inner loop counter from stack

endIn:	loop	forIn		; loop until all characters in current string
				; has been printed

	push	ebx		; save pointer in stack
	mov	eax, WRITE	; eax <- WRITE, prepare to print to screen
	mov	ebx, STDOUT	; ebx <- STDOUT, prepare to print to screen
	mov	ecx, lf		; ecx <- lf, move linefeed into print reg
	mov	edx, 1		; edx <- 1 , length of lf is 1
	int	0x80		; print linefeed

	pop	ebx		; retain pointer from stack
	pop	ecx		; retain outer loop counter from stack

endOut:	loop	forOut		; loop until all strings in msg are printed
	
	int	0x80
	
;;;  exit()

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