Difference between revisions of "CSC231 Homework 4 Solutions 2014"

From dftwiki3
Jump to: navigation, search
Line 142: Line 142:
  
 
<br />
 
<br />
 +
=Problem 3=
 +
<br />
 +
<source lang="asm">
 +
 +
;;; ; 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
 +
 +
 +
 +
</source>
 
<br />
 
<br />

Revision as of 17:47, 21 October 2014

--D. Thiebaut (talk) 16:16, 21 October 2014 (EDT)




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