Link to home
Start Free TrialLog in
Avatar of HasanChowdhury
HasanChowdhury

asked on

Assembly programming Fibonacci Number

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
Avatar of ozo
ozo
Flag of United States of America image

                 DEC Edx          ; N-2
shouldn't this be EBX?
              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:
Avatar of hclgroup
hclgroup

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
Avatar of HasanChowdhury

ASKER

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            

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I reversed, but no output

Please review my full code
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial