CSC231 Homework 3 Solutions 2014
--D. Thiebaut (talk) 21:13, 29 September 2014 (EDT)
Hw3_1.asm
;;; ; Hw3_1.asm
;;; ; D. Thiebaut
;;; ;
;;; ; displays all the number from 1 to 20 in increasing order.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf Hw3_1.asm
;;; ; nasm -f elf 231Lib.asm
;;; ; ld -melf_i3896 -o Hw3_1 Hw3_1.o 231Lib.o
;;; ; ./Hw3_1
;;; ; -------------------------------------------------------------------
extern _printDec
extern _println
extern _printString
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start:
mov eax, 1 ; where we start
mov ecx, 20 ; # of times we loop
for:
;;; display the integer at [ebx]
call _printDec
call _println
inc eax
loop for
;;; exit
mov eax,1
mov ebx,0
int 0x80 ; final system call
Hw3_2.asm
;;; ; Hw3_2.asm
;;; ; D. Thiebaut
;;; ;
;;; ; displays all the number from 1 to 20 in increasing order.
;;; ; along with ecx.
;;; ;
;;; ; 1. ecx = 20
;;; ; 2. ecx = 19
;;; ; 3. ecx = 18
;;; ; ...
;;; ; 19. ecx = 2
;;; ; 20. ecx = 1
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf Hw3_2.asm
;;; ; nasm -f elf 231Lib.asm
;;; ; ld -melf_i3896 -o Hw3_2 Hw3_2.o 231Lib.o
;;; ; ./Hw3_2
;;; ; -------------------------------------------------------------------
extern _printDec
extern _println
extern _printString
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
msg db ". ecx = "
MSGLEN equ $-msg
temp dd 0
count dd 1
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start:
mov eax, 1 ; where we start
mov ecx, 20 ; # of times we loop
for: mov dword[temp], ecx
;;; display the counter first
mov eax, dword[count]
call _printDec
inc eax
mov dword[count], eax
;;; display the string ". ecx = "
mov ecx, msg
mov edx, MSGLEN
call _printString
;;; print ecx
mov ecx, dword[temp]
mov eax, ecx
call _printDec
call _println
loop for
;;; exit
mov eax,1
mov ebx,0
int 0x80 ; final system call
Hw3_3.asm
;;; ; Hw3_3.asm
;;; ; D. Thiebaut
;;; ;
;;; ; displays all the number from 1 to 20 in increasing order.
;;; ; along with ecx.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf Hw3_3.asm
;;; ; nasm -f elf 231Lib.asm
;;; ; ld -melf_i3896 -o Hw3_3 Hw3_3.o 231Lib.o
;;; ; ./Hw3_3
;;; ; -------------------------------------------------------------------
extern _printDec
extern _println
extern _printString
extern _getInput
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
prompt db "> "
msg db ". ecx = "
MSGLEN equ $-msg
temp dd 0
count dd 1
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start:
;;; get number of time we need to loop
mov ecx, prompt
mov edx, 2
call _printString
call _getInput
mov ecx, eax
mov eax, 1 ; where we start
for: mov dword[temp], ecx
;;; display the counter first
mov eax, dword[count]
call _printDec
inc eax
mov dword[count], eax
;;; display the string ". ecx = "
mov ecx, msg
mov edx, MSGLEN
call _printString
;;; print ecx
mov ecx, dword[temp]
mov eax, ecx
call _printDec
call _println
loop for
;;; exit
mov eax,1
mov ebx,0
int 0x80 ; final system call
Hw3_4.asm
;;; ; Hw3_4.asm
;;; ; D. Thiebaut
;;; ;
;;; ; displays all the number from 1 to 20 in increasing order.
;;; ; along with ecx.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf Hw3_4.asm
;;; ; nasm -f elf 231Lib.asm
;;; ; ld -melf_i3896 -o Hw3_4 Hw3_4.o 231Lib.o
;;; ; ./Hw3_4
;;; ; -------------------------------------------------------------------
extern _printDec
extern _println
extern _printString
extern _getInput
;;; ------------------------------------------------------------
;;; data areas
;;; ------------------------------------------------------------
section .data
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start:
mov ecx, 40-2
mov ebx, 1 ; Fib[i-1]
mov edx, 1 ; Fib[i-2]
mov eax, 1 ; Fib[i] is eax
call _printDec ; print 1
call _println
call _printDec ; print 1
call _println
for:
mov eax, ebx ; eax <- Fib[i-1]
add eax, edx ; eax <- FIb[i-1]+Fib[i-2]
call _printDec ; print Fib[i]
call _println
mov edx, ebx
mov ebx, eax
loop for
;;; exit
mov eax,1
mov ebx,0
int 0x80 ; final system call
Hw3_5.asm
;;; ; Hw3_4.asm
;;; ; D. Thiebaut
;;; ;
;;; ; displays all the number from 1 to 20 in increasing order.
;;; ; along with ecx.
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ; nasm -f elf Hw3_4.asm
;;; ; nasm -f elf 231Lib.asm
;;; ; ld -melf_i3896 -o Hw3_4 Hw3_4.o 231Lib.o
;;; ; ./Hw3_4
;;; ; -------------------------------------------------------------------
extern _printDec
extern _println
extern _printString
extern _getInput
;;; ------------------------------------------------------------
;;; 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
_start:
;;; display Fibonacci(0) =1
mov ecx, Fibo
mov edx, FIBOLEN
call _printString
mov eax, 0
call _printDec
mov ecx, equal
mov edx, EQUALLEN
call _printString
mov eax, 1
call _printDec
call _println
;;; display Fibonacci(1) =1
mov ecx, Fibo
mov edx, FIBOLEN
call _printString
mov eax, 1 ; Fib[i] is eax
call _printDec ; print 1
mov ecx, equal
mov edx, EQUALLEN
call _printString
mov eax, 1
call _printDec
call _println
mov esi, 1 ; esi is Fib[i-1]
mov edi, 1 ; edi is Fib[i-2]
mov ecx, 40-2
for:
mov dword[temp], ecx
;;; print Fibonacci(
mov ecx, Fibo
mov edx, FIBOLEN
call _printString
;;; print i
mov eax, dword[counter]
inc dword[counter]
call _printDec
;;; print )=
mov ecx, equal
mov edx, EQUALLEN
call _printString
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
mov ecx, dword[temp]
loop for
;;; exit
mov eax,1
mov ebx,0
int 0x80 ; final system call
Hw3_6
Here's a very quick way of generating some Fibonacci terms:
;;; mostFibs.asm
;;; D. Thiebaut
;;; A very quick program (except for the IO operations that computes and displays 22 Fibonacci numbers)
;;;
extern _println
extern _printDec
;;; ------------------------------------------------------------
;;; code area
;;; ------------------------------------------------------------
section .text
global _start
_start:
mov ebx, 1 ; ebx will contain Fib(n-1)
mov edx, 1 ; edx will contain Fib(n-2)
mov eax, 1 ; print first 2 fibs
call _printDec
call _println
call _printDec
call _println
;;; compute and print 20 more fibs
mov ecx, 20
for:
;mov eax, ebx ; compute Fib(n): get Fib(n-1)
add eax, edx ; Fib(n-1) + Fib(n-2)
call _printDec
call _println
mov edx, ebx ; Fib(n-2) gets Fib(n-1)
mov ebx, eax ; Fib(n-1) gets Fib(n)
loop for
;;; exit()
theEnd:
mov eax,1
mov ebx,0
int 0x80 ; final system call
We see that we need 4 instructions to compute a new term of the fibonacci sequence (we remove the printing of the terms, since we are interested in only computing the Fibonacci terms, not printing them).
The processor runs at 2.5 GHz. So a cycle takes 0.4 nanoseconds. 4 instructions take 4 x 0.4 ns = 1.6 ns. So, in 1 second, we could compute 1 sec / 1.6 ns = 1E9 / 1.6 = 625 million terms, or, roughly, 1/2 a billion terms.