What's new

Fibonacci Sequence in x86 (Inline Assembly)

  • Thread starter Cakes
  • Start date
  • Views 9,148
Status
Not open for further replies.
Cakes

Cakes

お前はもう死んでいる
VIP
Retired
Mythical Veteran Platinum Record End of the Year 2017
Messages
20,705
Reaction score
20,272
Points
3,870
Sin$
-7
So, for my midterm project I have to complete two exercises from this list we're given. I've already finished the first one, but I need a little help finishing the second one. It's done in C++ with inline x86.

Assignment: Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence described by the following formula : Fib(1)=1, Fib(2)=1, Fib(n)=Fib(n-1) + Fib(n-2). Place each value in the EAX register and display it inside the loop.

This is the code I have so far:
C:
#include <iostream>
#include <stdio.h>

int eaxOut;

void main()
{
    _asm {
        MOV EAX, 1;            //First number in sequence.
        MOV EBX, 0;            //Initialize EBX.
        MOV EDX, 1;            //Second number in sequence.
        MOV ECX, 5;            //Set the counter register to 5.

    L1:
        MOV EAX, EBX; 
        ADD EAX, EDX;        //EAX = EBX + EDX; 2 = 1 + 1

        MOV eaxOut, EAX;   //Place EAX's value in ouput variable.

        MOV EBX, EDX;        //Increase EBX to the previous EDX in order to continue the sequence.
        MOV EDX, EAX;        //Increase EDX to the result of EAX in order to continue the sequence.

        Loop L1;            //Continue the Loop until the Counter hits 0.
    }
    printf("EAX: %d\n", eaxOut);
    system("pause");
}

My code currently will perform the Fib sequence and generate the correct numbers. However, what I can't figure out is how to display each value of the sequence through each iteration of the loop. Everything I've looked up has told me to use 'call DumpRegs', but I can't use that command with my inline code. I don't know if there's something else I'm supposed to include, or what. Any help would be appreciated.

S7 Pro, Dwack. Anyone else is welcome to help, I don't really know any specific users who would have knowledge on this.
 
Last edited:
Cakes

Cakes

お前はもう死んでいる
VIP
Retired
Mythical Veteran Platinum Record End of the Year 2017
Messages
20,705
Reaction score
20,272
Points
3,870
Sin$
-7
I wasn't able to get anyone to help me get this working with inline x86, so I decided to just write it in full assembly. The code can be seen below. If anyone ends up using the full assembly code, remember that you'll need to configure your project settings correctly in order to compile the program.
C:
INCLUDE Irvine32.inc

.code
main proc
    mov eax, 1
    call DumpRegs
    mov ebx, 0
    mov edx, 1
    mov ecx, 5

L1:
    mov eax, ebx
    add eax, edx
    call DumpRegs
    mov ebx, edx
    mov edx, eax
    Loop L1
    exit
main endp
end main
 
Status
Not open for further replies.
Top Bottom
Login
Register