CSC231 Homework 5 Solutions 2010

From dftwiki3
Revision as of 11:33, 8 December 2010 by Thiebaut (talk | contribs) (Program 1)
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

Program 2

;;; ; File: hw5b.asm
;;; ; Author: Tiffany Q. Liu
;;; ; Acct: 231a-ac
;;; ; Date: October 28, 2010
;;; ;
;;; ; Desc: Prints the first 10 rows of Pascal's triangle using one array of
;;; ;       10 bytes.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf -F  stabs hw5b.asm
;;; ;     ld -melf_i386 -o hw5b hw5b.o
;;; ;     ./hw5b
;;; ;
;;; ; Output:
;;; ; 1 0 0 0 0 0 0 0 0 0 0
;;; ; 1 1 0 0 0 0 0 0 0 0 0
;;; ; 1 2 1 0 0 0 0 0 0 0 0
;;; ; 1 3 3 1 0 0 0 0 0 0 0
;;; ; 1 4 6 4 1 0 0 0 0 0 0
;;; ; 1 5 10 10 5 1 0 0 0 0
;;; ; 1 6 15 20 15 6 1 0 0 0
;;; ; 1 7 21 35 35 21 7 1 0 0
;;; ; 1 8 28 56 70 56 28 8 1 0
;;; ; 1 9 36 84 126 126 84 36 9 1
;;; ; -------------------------------------------------------------------

%include "asm_io.inc"

        ;; -------------------------
        ;; data segment
        ;; -------------------------
        section 	.data
pas     dw      	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00
space	db		" ", 0x00
N	equ		10
        ;; -------------------------
        ;; code area
        ;; -------------------------
        section 	.text
        global  	asm_main

asm_main:
	mov	ecx, N			; ecx <- 10, init outer loop counter
forOut:
	push	ecx			; save outer loop counter to stack 
	mov	ecx, N			; ecx <- 10, init 1st inner loop counter
	mov	ebx, pas		; ebx <- first index of pas, 
					; set up pointer
forIn1:	mov	eax, 0			; eax <- 0, clear out eax
        mov     ax, word[ebx]   	; pass address of the int pas to ax
        call    print_int		; call a function in asm_io library
                       	        	; that will print the int

	mov	eax, space		; eax <- " ", prepare to print space
	call	print_string		; call function in asm_io library that
					; will print the string
	add	ebx, 1*2		; ebx <- ebx + 2, increment ebx to point
					; to the next index in array of words

endIn1:	loop	forIn1			; loop until all the ints in the array
					; are printed with a space in between
					; each int

        call    print_nl        	; call the function that prints
                       	        	; a new blank line

	mov	ebx, pas+(N-1)*2	; ebx <- addr of last pascal int in 
					; the array
	mov	ecx, N-1		; ecx <- 1, init 2nd inner loop counter
					; one less than the number of int since
					; the first int never gets changed
forIn2:	mov	eax, 0			; eax <- 0, clear out eax
	mov	edx, 0			; edx <- 0, clear out edx
	mov	ax, word[ebx]		; ax <- int ebx is pointing to
	mov	dx, word[ebx-1*2]	; dx <- int to the left of the one
					; stored in ax
	add	ax, dx			; ax <- ax+dx, obtain next pascal int in
					; ax by adding the current int to its 
					; left neighbor
	mov	word[ebx], ax		; move sum back to index ebx points to
	sub	ebx, 1*2		; ebx <- ebx - 2, move pointer to left
					; to point to the prev word
endIn2:	loop	forIn2			; loop until all ints in array are 
					; updated
	
	pop	ecx			; retain outer loop counter from stack
	loop	forOut			; loop until all first 10 rows of 
					; Pascal's Triangle are printed

        ;; return to C program

        ret