Difference between revisions of "CSC231 Homework 4 Solution"
(New page: =Homework #4 Solution= ==Problem #1== <code><pre> ;;; ; ------------------------------------------------------------------- ;;; ; hw4a.asm ;;; ; Lei Lei ;;; ; ;;; ; Given the following de...) |
(→Problem #2) |
||
Line 137: | Line 137: | ||
==Problem #2== | ==Problem #2== | ||
<code><pre> | <code><pre> | ||
+ | ;;; ; ------------------------------------------------------------------- | ||
;;; ; hw4b.asm | ;;; ; hw4b.asm | ||
;;; ; Julia Burch | ;;; ; Julia Burch | ||
;;; ; 231a-aa | ;;; ; 231a-aa | ||
;;; ; | ;;; ; | ||
+ | ;;; ; Computes and prints the first 10 rows of Pascal's triangle. | ||
;;; ; OUTPUT: | ;;; ; OUTPUT: | ||
;;; ; 1 0 0 0 0 0 0 0 0 0 | ;;; ; 1 0 0 0 0 0 0 0 0 0 |
Revision as of 11:33, 14 October 2008
Homework #4 Solution
Problem #1
;;; ; -------------------------------------------------------------------
;;; ; hw4a.asm
;;; ; Lei Lei
;;; ;
;;; ; Given the following definition:
;;; ; msg db 3,"ALL",11,"PROGRAMMERS",3,"ARE",11,"PLAYWRIGHTS"
;;; ; db 3,"AND",3,"ALL",9,"COMPUTERS",3,"ARE",5,"LOUSY"
;;; ; db 6,"ACTORS"
;;; ; This program outputs each letter of msg capitalized in a line:
;;; ;
;;; ; All
;;; ; Programmers
;;; ; Are
;;; ; Playwrights
;;; ; And
;;; ; All
;;; ; Computers
;;; ; Are
;;; ; Lousy
;;; ; Actors
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf -F stabs hw4a.asm
;;; ; ld -o hw4a hw4a.o
;;; ; ./hw4a
;;; ; -------------------------------------------------------------------
;;; ; -----------------------------------------------------------
;;; ; Constants
;;; ; -----------------------------------------------------------
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
newLine db 0x0A
line_len equ 1
counter dd 0
temp1 dd 0
temp2 dd 0
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start: mov ecx, N
for: mov dword[temp1], ecx ;Main loop, loop for N=10 times
mov esi, msg
add esi, dword[counter] ;esi points to integer
inc dword[counter]
mov eax, WRITE
mov ebx, STDOUT
mov ecx, msg
add ecx, dword[counter] ;ecx points to first letter
mov edx, 1
int 0x80 ;print first letter in cap
movzx ecx, byte[byte esi] ;move the integer to ecx
dec ecx ;minus the already printed first letter
inc dword[counter]
call printLower
call printLine
mov ecx, dword[temp1]
loop for
;;; exit()
mov eax,EXIT
mov ebx,0
int 0x80 ; final system call
;;; ----------------------------------------------------------------
;;; printLower: Print letters in lower case
;;; ----------------------------------------------------------------
printLower:
forLower:
mov dword[temp2], ecx
mov eax, WRITE
mov ebx, STDOUT
mov ecx, msg
add ecx, dword[counter]
add dword[ecx], 'a'-'A' ;change into lower case
mov edx, 1
int 0x80
inc dword[counter]
mov ecx, dword[temp2]
loop forLower
ret
;;; ---------------------------------------------------------------
;;; printLine: Hard break and print new line
;;; ---------------------------------------------------------------
printLine:
section .bss
.temp resd 1
section .text
mov eax, WRITE
mov ebx, STDOUT
mov edx, line_len
mov dword[.temp], ecx
lea ecx, [newLine]
int 0x80
mov ecx, dword[.temp]
ret
Problem #2
;;; ; -------------------------------------------------------------------
;;; ; hw4b.asm
;;; ; Julia Burch
;;; ; 231a-aa
;;; ;
;;; ; Computes and prints the first 10 rows of Pascal's triangle.
;;; ; OUTPUT:
;;; ; 1 0 0 0 0 0 0 0 0 0
;;; ; 1 1 0 0 0 0 0 0 0 0
;;; ; 1 2 1 0 0 0 0 0 0 0
;;; ; 1 3 3 1 0 0 0 0 0 0
;;; ; 1 4 6 4 1 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
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf -F stabs hw4b.asm
;;; ; gcc -o hw4b driver.c asm_io.o hw4b.o
;;; ; ./hw4b
;;; ; -------------------------------------------------------------------
;; %include files here...
%include "asm_io.inc"
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
shift db 0
pascal db 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
temp dd 0
space db 9
rows equ 10
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global asm_main
asm_main:
;; move first line of Pascal into edi, then rows into ecx
;; to initialize and run for loop 10 times.
mov edi, pascal
mov ecx, rows
main:
;; store ecx in a temporary variable, move rows into ecx to initialize
;; and run print_triangle loop. Move the first line into edi
;; and 0 into esi.
mov dword[temp], ecx
mov ecx, rows
mov edi, pascal
mov esi, 0
print_triangle:
;; print the int in al, print tab and then clear
;; tab from register.
mov al, byte[edi+esi]
call print_int
mov eax, space
call print_string
mov eax, 0
inc esi
loop print_triangle
;; print a new line
call print_nl
;; load newly calculated pascal line into edi, then shift the row
mov edi, pascal
mov esi, shift
mov ecx, rows-1
calculate:
;; move pascal digit into al, add it to previous row's, store the value
mov al, byte[edi+ecx]
add al, byte[esi+ecx]
mov byte[edi+ecx], al
loop calculate
;; restore value of ecx for loop
mov ecx, dword[temp]
loop main
;;; exit()
ret