Difference between revisions of "CSC231 Homework 4 Solutions 2014"
(→Problem 3) |
(→Problem #4) |
||
Line 299: | Line 299: | ||
;;; ; D. Thiebaut | ;;; ; D. Thiebaut | ||
;;; ; | ;;; ; | ||
− | ;;; ; a | + | ;;; ; This program takes a number in eax and prints it in hex |
+ | ;;; ; on the screen. The program provides a global function | ||
+ | ;;; ; called _printHex. | ||
;;; ; | ;;; ; | ||
;;; ; to assemble and run: | ;;; ; to assemble and run: | ||
Line 305: | Line 307: | ||
;;; ; nasm -f elf -F stabs Hw4_4.asm | ;;; ; nasm -f elf -F stabs Hw4_4.asm | ||
;;; ; ld -melf_i3896 -o Hw4_4 Hw4_4.o | ;;; ; ld -melf_i3896 -o Hw4_4 Hw4_4.o | ||
− | ;;; ; ./ | + | ;;; ; ./Hw4_4 |
;;; ; ------------------------------------------------------------------- | ;;; ; ------------------------------------------------------------------- | ||
Revision as of 17:49, 21 October 2014
--D. Thiebaut (talk) 16:16, 21 October 2014 (EDT)
Contents
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: push 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
pop ecx
loop for
;;; exit()
mov eax,EXIT
mov ebx,0
int 0x80 ; final system call
;;; ----------------------------------------------------------------
;;; printLower: Print letters in lower case
;;; ----------------------------------------------------------------
printLower:
forLower:
push 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]
pop ecx
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
push ecx
lea ecx, [newLine]
int 0x80
pop ecx
ret
Problem 3
;;; ; Hw4_3.asm
;;; ; D. Thiebaut
;;; ;
;;; ; displays the first Fibonacci terms along with a string
;;; ; identifying the terms.
;;; ;
;;; ; Example:
;;; ; Fibonacci(0) =1
;;; ; Fibonacci(1) =1
;;; ; Fibonacci(2) =2
;;; ; Fibonacci(3) =3
;;; ; Fibonacci(4) =5
;;; ; Fibonacci(5) =8
;;; ; ...
;;; ; To assemble and run:
;;; ;
;;; ; nasm -f elf Hw3_5.asm
;;; ; nasm -f elf 231Lib.asm
;;; ; ld -melf_i3896 -o Hw3_5 Hw3_5.o 231Lib.o
;;; ; ./Hw3_5
;;; ; -------------------------------------------------------------------
extern _printDec
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
Fibo db "Fibonacci("
FIBOLEN equ $-Fibo
Equal db ") ="
EQUALLEN equ $-Equal
counter dd 2
temp dd 0 ; holds ecx as loop counter
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
;;; --------------------------------------------------
;;; printLn: prints a new line
;;; --------------------------------------------------
printLn:
section .data
msg1 db 0x0a
section .text
push eax
push ebx
push ecx
push edx
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, 1
int 0x80
pop edx
pop ecx
pop ebx
pop eax
ret
;;; --------------------------------------------------
;;; printMsg: prints the message passed in ecx (address)
;;; and edx (# of chars). Does not modify eax or ebx.
;;; --------------------------------------------------
printMsg: push eax
push ebx
mov eax, 4
mov ebx, 1
int 0x80
pop ebx
pop eax
ret
;;; --------------------------------------------------
;;; printFibMsg: gets a number in eax, and prints it
;;; between parentheses, as in "Fibonacci(###) = "
;;; --------------------------------------------------
printFibMsg: push eax
push ebx
push ecx
push edx
;; print "Fibonacci("
mov ecx, Fibo
mov edx, FIBOLEN
call printMsg
;; print eax
call _printDec
;; print ")="
mov ecx, Equal
mov edx, EQUALLEN
call printMsg
pop edx
pop ecx
pop ebx
pop eax
ret
_start:
;;; display Fibonacci(0) =1
mov eax, 0
call printFibMsg
mov eax, 1
call _printDec
call printLn
;;; display Fibonacci(1) =1
mov eax, 1
call printFibMsg
mov eax, 1
call _printDec
call printLn
;;; start for loop
mov esi, 1 ; esi is Fib[i-1]
mov edi, 1 ; edi is Fib[i-2]
mov ecx, 40-2
for:
push ecx
mov eax, dword[counter]
call printFibMsg
mov eax, esi ; eax <- Fib[i-1]
add eax, edi ; eax <- FIb[i-1]+Fib[i-2]
call _printDec ; print Fib[i]
call printLn
mov edi, esi
mov esi, eax
pop ecx
loop for
;;; exit
mov eax,1
mov ebx,0
int 0x80 ; final system call
Problem #4
;;; ; Hw4_4.asm
;;; ; D. Thiebaut
;;; ;
;;; ; This program takes a number in eax and prints it in hex
;;; ; on the screen. The program provides a global function
;;; ; called _printHex.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf -F stabs Hw4_4.asm
;;; ; ld -melf_i3896 -o Hw4_4 Hw4_4.o
;;; ; ./Hw4_4
;;; ; -------------------------------------------------------------------
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
bin2hex db "0123456789ABCDEF"
;; table of all the hex characters from 0 to F
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
;;; ------------------------------------------------------------
;;; getHexDigit: given 4-bit number in dl, returns the ASCII
;;; corresponding to its hex digit representation in dl.
;;; ------------------------------------------------------------
getHexDigit:
push ebx
mov ebx, bin2hex
and edx, 0xf ;clear all but lower 4 bits
mov dl, byte[ebx+edx]
pop ebx
ret
;;; ------------------------------------------------------------
;;; printString
;;; ------------------------------------------------------------
printString: push eax
push ebx
mov eax, 4
mov ebx, 1
int 0x80
pop ebx
pop eax
ret
printLn:
section .data
linefeed db 10
section .text
push ecx
push edx
mov ecx, linefeed
mov edx, 1
call printString
pop edx
pop ecx
ret
;;; ------------------------------------------------------------
;;; printHex:
;;; ------------------------------------------------------------
section .data
hexString db "00000000"
index dd 7
_printHex: push eax
push ebx
push ecx
push edx
mov dword[index],7
mov ecx, 8 ; 8 digits to create
.for: mov edx, eax ; get copy of dword in edx
and edx, 0xf ; isolate lower 4 bits
call getHexDigit
mov ebx, dword[index] ; ebx points to hexString
mov byte[ebx+hexString], dl
dec dword[index] ; get ready for next digit
shr eax, 4 ; put next digit in least
; significant position
loop .for
mov ecx, hexString
mov edx, 8
call printString
pop edx
pop ecx
pop ebx
pop eax
ret
;;; ------------------------------------------------------------
;;; ------------------------------------------------------------
_start:
mov eax, 0x12345678
call _printHex
call printLn
mov eax, 0x89ABCDEF
call _printHex
call printLn
;;; exit()
mov eax,1
mov ebx,0
int 0x80 ; final system call