Solved

Assembly programming Fibonacci Number

Posted on 2006-11-30
9
3,281 Views
Last Modified: 2016-02-10
I want to write an assembly program to generate Fibonnaci numbers. Following is the code, but it does not work.
Can anybody give me a hand

Masud

.386
.model flat
include cs266.inc
.code
      main:
            PUSH 8
            call fib
            jmp exit1
      exit1:
            putint eax
            ret
            
      fib:
            PUSH EBP
            MOV EBP, ESP
            MOV EBX, [EBP+8]      ; N
            CMP EBX, 0
            JNE notzero
            MOV EAX,1
            JMP exit
            notzero:
                  CMP EBX,1
                  Jne else
                  MOV EAX,1
                  JMP exit
                  else:
                        DEC EBX            ; N-1
                        PUSH EBX
                        CALL fib      ; fib(N-1)
                        MOV EDX, EAX       ; EDX contains fib(N-1)
                        ;PUSH EDX
                        DEC Edx            ; N-2
                        push ebx
                        CALL fib      ; fib(N-2)
                        ;POP EDX
                        ADD EAX, EDX      ; fib(n-1) + fib (n-2)
                  exit:
                        pop ebp
                        ret 4
      ret
end
0
Comment
Question by:HasanChowdhury
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18051744
                 DEC Edx          ; N-2
shouldn't this be EBX?
0
 
LVL 8

Expert Comment

by:doraiswamy
ID: 18052089
              else:                    ; here BX has N
                    DEC EBX          ; N-1
                    PUSH EBX        ; store it
                    CALL fib     ; fib(N-1) is returned in EAX
                    POP EBX
                    DEC EBX     ; n-2
                    PUSH EAX   ; store fib(n-1)
                    CALL fib     ; fib(N-2)
                    POP EDX    ; get back fib(n-1)                    
                    ADD EAX, EDX     ; fib(n-1) + fib (n-2)
               exit:
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 18054122
2 examples I found on the web

http://www.beroset.com/asm/fibo-masm.asm
http://www.beroset.com/asm/fibo.asm

maybe you could get some ideas from there
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:HasanChowdhury
ID: 18078666
Following the update version, but it still does not work


.386
.model flat
include cs266.inc
.code
main:
      PUSH 8
      call fib
      putint eax
      jmp exit1
      exit1:
            putint eax
            ret
      fib proc
            PUSH EBP
            MOV EBP, ESP
            MOV EBX, [EBP+8]      ; N
            CMP EBX, 0
            JNE notzero
            MOV EAX,1
            JMP exit
            notzero:
                  CMP EBX,1
                  Jne else
                  MOV EAX,1
                  JMP exit
                  else:
                        DEC EBX         ; N-1
                            PUSH EBX        ; store it
                            CALL fib        ; fib(N-1) is returned in EAX
                            MOV EDX, EAX      ; store fib(N-1) to edx
                            PUSH EDX      ; save edx
                            POP EBX
                            DEC EBX           ; n-2
                            PUSH EBX           ; store fib(n-1)
                            CALL fib           ; fib(N-2), fib(n-2) in EAX
                            POP EDX            ; get back fib(n-1)                    
                            ADD EAX, EDX    ; fib(n-1) + fib (n-2)
                            putint eax
                        exit:
                              pop ebp
                              ret 4
      endp
end            

0
 
LVL 84

Accepted Solution

by:
ozo earned 63 total points
ID: 18079642
                       PUSH EDX     ; save edx
                        POP EBX
Shouldn't this be done in the opposite order?
0
 

Author Comment

by:HasanChowdhury
ID: 18079949
I reversed, but no output

Please review my full code
0
 
LVL 8

Assisted Solution

by:doraiswamy
doraiswamy earned 62 total points
ID: 18087142
ozo => Shouldn't this be done in the opposite order? => Correct.

Hasan -> No output => what is the output function?

I found some other errors. Here is the corrected code:

.386
.model flat
include cs266.inc
.code
main:
     PUSH 8
     call fib
     putint eax
     jmp exit1
     exit1:
          putint eax
          ret
     fib proc
          PUSH EBP
          MOV EBP, ESP
          MOV EBX, [EBP+8]     ; N
          CMP EBX, 0
          JNE notzero
          MOV EAX,1
          JMP exit
          notzero:
               CMP EBX,1
               Jne else
               MOV EAX,1
               JMP exit
               else:
                    DEC EBX         ; N-1
                        PUSH EBX        ; store it
                        CALL fib        ; fib(N-1) is returned in EAX
                        MOV EDX, EAX     ; store fib(N-1) to edx
                        POP EBX    ; get (n-1)
                        PUSH EDX     ; save edx => fib(n-1)
                        DEC EBX          ; n-2
                        PUSH EBX          ;
                        CALL fib          ; fib(n-2) in EAX
                        POP EBX        ; balance stack
                        POP EDX           ; get back fib(n-1)                    
                        ADD EAX, EDX    ; fib(n-1) + fib (n-2)
                        putint eax
                    exit:
                         pop ebp
                         ret 4
     endp
end          
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
git hub on ubuntu clone a git hub repo how do i find the "packed' file that it downloaded 5 39
changePi Challenge 15 103
creating threads in delphi 1 80
Apps blocked by Java 9 55
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now