Hey everyone, my first post here, so lets see if I do this right.
So I have this assignment that links a fibonacci.c, fib.h, and fib.s file. The assignment is supposed to be done by compiling all of the files, then running ./fibonacci <n> where <n> is the n'th term that should be printed out of the fibonacci sequence. Here's what I have so far:

my fibonacci.c:

#include <stdio.h>
#include "fib.h"

int main(int argc, char*argv[]){
int x;


return 0;

fib.h, which was provided for the assignment:

#ifndef _FIB_H_
#define _FIB_H_

extern int fib(int n);


and my fib.s, where the work should be done:

.file	"fib.c"
.globl fib
	.type	fib, @function
	pushl	%ebp
	movl	%esp, %ebp
	subl	$32, %esp
	movl	$0, -20(%ebp)
	movl	$0, -4(%ebp)
	movl	$1, -8(%ebp)
	movl	20(%esp), %esi
	jmp	.L2
	movl	-4(%ebp), %eax
	movl	%eax, -12(%ebp)
	movl	-8(%ebp), %eax
	addl	%eax, -4(%ebp)
	movl	-12(%ebp), %eax
	movl	%eax, -8(%ebp)
	addl	$1, -20(%ebp)
	cmpl	%esi, -20(%ebp)  #-20 is my i counter
	jle	.L3
	movl	-12(%ebp), %eax
	.size	fib, .-fib
	.ident	"GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
	.section	.note.GNU-stack,"",@progbits

All of this works fine, (oh, this is using GNU Assembler, GAS), and compiles on Linux..
My question is, since I'm running this through the C file, i know that argv[1] is my N term, and i send that as an int to the fibs.s file.. but where does it get stored in fib.s? Basically if I compile it:
-> gcc fibonacci.c fib.s -o fibonacci
-> ./fibonacci 7
..should print out 13
...where in the Assembler is that 7 stored? I want to put that number in my "%esi" and so my loop will work that many times. I thought it would be stored in %ebp, but it's apparently not?

6 Years
Discussion Span
Last Post by Psyenyde

or here's a shorter version of my question.. when i call fib(x).. where does that X go in the assembler code?


Nevermind, the value passed was stored in 8(%ebp).. now my issue is determining overflow. Basically if that value X is greater than 46, since in signed int's, 46 is the highest number of fibonacci terms before an overflow.

So, what does everything think? JO? JNO? I'm not sure how to use either.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.