Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fibonacci Series in Assembly x86

Finally after a long session of countless errors , hope this is the final one.

No Compile or runtime errors, Just a logical error.

EDIT: (Fixed Pseudocode)

My Pseudocode:

first  = 1;
second = 1;
third  = 0;

 for i from 1 to n{

    third=first+second
    first=second
    second=third

}
return third

This would print the final result of the series.

My Assembly Code:

I have added Comments where ever possible

.386
.model flat,stdcall
option casemap:none

.data
timestell     db "Loop Ran : %d Times -----",0     ;format string
fmtd   db "%d",0
finalprint  db "Final Number is : %d ------",0     ;format string
times  dd 0Ah                                      ;times to loop
first dd 1h
second dd 1h
third dd 0h


.data?

retvalue1 dd ?             ;we will initialize it later

.code
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
includelib MSVCRT
extrn printf:near
extrn exit:near

public main
main proc


         mov ecx, times      ;loop "times" times
         mov eax,0           ;just to store number of times loop ran
      top:                   ;body of loop
         cmp ecx, 0          ;test at top of loop
         je bottom           ;loop exit when while condition false
         add eax,1           ;Just to test number of times loop ran
         mov ebx,first       ;move first into ebx
         add ebx,second      ;add ebx, [ first+second ]
         mov third,ebx       ;Copy result i.e ebx [first+second] to third
         xor ebx,ebx         ;clear for further use
         mov ebx,first       ;move first into ebx
         mov second,ebx      ;copy ebx to second [NOW second=first]
         xor ebx,ebx         ;clear for later use
         mov ebx,third       ;move thirs into ebx
         mov second,ebx      ;copy ebx to third [NOW second=third]
         xor ebx,ebx         ;clear it
         dec ecx             ;decrement loop
         jmp top             ;Loop again

      bottom:
           mov retvalue1,eax       ;store eax into a variable
           push retvalue1          ;pass this variable to printf
           push offset timestell   ;pass Format string to printf    
           call printf             ;Print no.  of times loop ran
           push third              ;push value of third to printf
           push offset finalprint  ;push the format string
           call printf             ;Print the final number


      push 0        ;exit gracefully
      call exit     ;exit system

main endp

end main

The code runs well but the output doesn't satisfies me:

Output: Loop Ran : 10 Times -----Final Number is : 11 ------

First of all i am not really sure that Final number is in decimal or hex form.

  • Assuming it as decimal : Fibonacci Series doesn't have 11
  • Assuming it as hex : Fibonacci Series doesn't have 17 (11 hex = 17 dec)

What am i doing wrong?

like image 874
Idkwhoami Avatar asked Feb 23 '16 14:02

Idkwhoami


People also ask

What is Fibonacci series in C code?

Fibonacci Series in C: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1. There are two ways to write the fibonacci series program: Fibonacci Series without recursion.

What is Fibonacci series in DSA?

Fibonacci series generates the subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively. Fibonacci series satisfies the following conditions − Fn = Fn-1 + Fn-2. Hence, a Fibonacci series can look like this ...

What is Fibonacci series in Ruby?

A Fibonacci series is a series of numbers in which the next number is the sum of the previous two numbers. The first two numbers are 0 and 1. The third number is the sum of the previous two, and so on. For example, the following is a Fibonacci series: 0, 1, 1, 2, 3, 5 ... .

How many elements in array are Fibonacci numbers in C program?

int fib[24]; This defines an array called fib of 24 integers. In C, the array index is bounded by square brackets (this avoids confusion with a function call). Also, the first element of the array has index zero, and for this 24 element array, the last element is index 23.


1 Answers

The problem was that my Actual code was not matching with my Pseudocode which resulted in the Logical error.

This Part

     mov ebx,first       ;move first into ebx
     mov second,ebx      ;copy ebx to second [NOW second=first]

This gives first value of second, but my PseudoCode says "first=second", which means give value of second to first.

     mov ebx,second      ;move second into ebx
     mov first,ebx       ;copy ebx to second [NOW first=second]

Final Working Code for x86 Intel Processor:

For any further referrers , i am posting a working code for x86 intel

.386
.model flat,stdcall
option casemap:none

.data
timestell   db   "Loop Ran : %d Times -----",0          ;format string
finalprint  db   "%d th Fibonacci number is %d",0       ;format string
times       dd   14h                                    ;times to loop
first dd 1h
second dd 1h
third dd 0h



.code
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
includelib MSVCRT
extrn printf:near
extrn exit:near

public main
main proc


         mov ecx, times       ;set loop counter to "times" time
         sub ecx,2            ;loop times-2 times

      top:
         cmp ecx, 0          ; test at top of loop
         je bottom           ; loop exit when while condition false
         xor ebx,ebx         ;Clear ebx
         mov ebx,first       ;move first into ebx
         add ebx,second      ;add ebx, [ first+second ]
         mov third,ebx       ;Copy result i.e ebx [first+second] to third
         xor ebx,ebx         ;clear for further use
         mov ebx,second      ;move second into ebx
         mov first,ebx       ;copy ebx to second [NOW first=second]
         xor ebx,ebx         ;clear for later use
         mov ebx,third       ;move thirs into ebx
         mov second,ebx      ;copy ebx to third [NOW second=third]
         xor ebx,ebx         ;clear it
         dec ecx             ;decrement loop
         jmp top             ;Loop again

      bottom:
        push third
              push times                ;push value of third to printf
              push offset finalprint    ;push the format string
              call printf               ;Print the final number
      push 0        ;exit gracefully
         call exit      ;exit system

    main endp

end main
like image 63
Idkwhoami Avatar answered Sep 29 '22 17:09

Idkwhoami