Difference between revisions of "CSC231 Homework 3 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <onlydft> <br /> = Hw3_1.asm = <br /> <source lang="asm"> ;;; ; Hw3_1.asm ;;; ; D. Thiebaut ;;; ; ;;; ; displays all the number from 1 to 20 in increasing order. ...")
 
Line 383: Line 383:
 
<br />
 
<br />
 
<br />
 
<br />
 +
=Hw3_6=
 
<br />
 
<br />
 +
Here's a very quick way to generate some Fibonaccis:
 
<br />
 
<br />
 +
<source lang="asm">
 +
;;; 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
 +
 +
</source>
 
<br />
 
<br />
 +
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.
 +
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 17:59, 12 October 2014

--D. Thiebaut (talk) 21:13, 29 September 2014 (EDT)



...



Hw3_6


Here's a very quick way to generate some Fibonaccis:

;;; 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.