1

I have two files: f1.S where is my Fibonacci function (counts n-th member of Fibonacci sequence) written in assembler language and f2.c where the Fibonacci function is called.

Here are these files: f1.S

.global fibonacci
fibonacci:

push %rbp
movq %rsp, %rbp
push %rax

movq 16(%rbp), %rax 

cmp $0, %rax
je zeroValue
cmp $1, %rax
je oneValue
jmp more

zeroValue:
addq $0, %r8
jmp end

oneValue:
addq $1, %r8
jmp end

more:
movq 16(%rbp), %rax
dec %rax
pushq %rax
call fibonacci
movq 16(%rbp), %rax
dec %rax
dec %rax
pushq %rax
call fibonacci

end:
mov %rbp, %rsp
pop %rbp
ret

f2.c

#include <stdio.h>
extern int fibonacci (int);
int main ()
{
    int n = 6;
    int res;
    res = fibonacci(n);
    printf ("N-th member of Fibonacci sequence is: %d", res);
    return 0;
}

To compiling and linking i am making these commands:

as f1.S -o f1.o

gcc f2.c -c -o f2.o

gcc f2.o f1.o -o program

Everything is OK till i am trying to run my exe file (program). I can not run it cause I've got message: Segmentation Fault. What am I doing wrong? Fibonacci function is for sure OK cause I was using it in clean Assembler and then it worked.

  • 2
    Where are the files? – Some programmer dude May 10 '15 at 11:52
  • 1
    And if you have crashes, then run in a debugger to try and locate *where* the crash is. – Some programmer dude May 10 '15 at 11:52
  • Sry that is my first message here and i accidentally sent it... Now is edited and i added codes. When im trying to run it in gdb i've got: `Program received signal SIGSEGV, Segmentation fault. 0x000000000040056f in more ()` – Krzysiek Zienkiewicz May 10 '15 at 12:07
  • Seems like you should align the stack. Read online for more information. – emjay May 10 '15 at 12:24
  • Learn to use the debugger. Also, comment your code, especially if you want others to help. That said, I can see you discard the result from the first recursive call, and the fault is probably due to stack overflow caused by wrong termination condition. Single step in the debugger. – Jester May 10 '15 at 12:38

2 Answers2

0

first, make sure how the C function will call your assembly function. so you can prepare your assembly function to deal with the call.

for example if the c call will not clean the stack after the assembly function return stdcal then you must clean the stack in the assembly code

EDIT: removed the notice on the registers syntax. thanks Vyktor

the accountant
  • 506
  • 6
  • 14
0

In gdb you get SIGSEGV like this:

Program received signal SIGSEGV, Segmentation fault.
fibonacci () at f1.S:6
6   push %rax

What's more interesting is your backtrace (end of it):

(gdb) backtrace -10
#1048280 0x00007fffffffe6e0 in ?? ()
#1048281 0x000000000040056d in more () at f1.S:28
#1048282 0x00007fffffffe7fe in ?? ()
#1048283 0x00007fffffffe7ff in ?? ()
#1048284 0x00007fffffffe700 in ?? ()
#1048285 0x000000000040056d in more () at f1.S:28
#1048286 0x00007fffffffe7ff in ?? ()
#1048287 0x0000000000000006 in ?? ()
#1048288 0x00007fffffffe720 in ?? ()
#1048289 0x000000000040051f in main () at f2.c:7
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

So you managed to call your recursion 1 048 289 times...

Further more, after line 8 (movq 16(%rbp), %rax), you'll get:

(gdb) i r rax
rax            0x7fffffffe800   140737488349184

And from your function it looks like rax should be a counter.

Since you're using 64bit registers I assume you're running x86_64 architecture which doesn't store arguments on stack but uses registries:

If the class is INTEGER, the next available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9 is used

(gdb) i r edi
edi            0x6  6

Your solution would probably run fine on cdecl calling convention, but not on Microsoft x64 calling convention or System V AMD64 ABI.

So for example a way to get this done is (works for System V AMD64 ABI):

.global fibonacci
fibonacci:

push %rbp
push %rdi

cmp $0, %rdi
je zeroValue
cmp $1, %rdi
je oneValue
jmp more

zeroValue:
    movq $0, %rax
    jmp end

oneValue:
    movq $1, %rax
    jmp end

more:
    dec %rdi
    call fibonacci
    push %rax
    dec %rdi
    call fibonacci
    pop %r8
    add %r8, %rax

end:
    pop %rdi
    pop %rbp
    ret

Which goes like:

  • store rbp
  • store rdi
  • if rdi == 0: return 0
  • if rdi == 1: return 1
  • decrease rdi
  • call fibonacci
  • store result on stack
  • decrease rdi
  • call fibonacci
  • restore result to r8
  • add r8 to result
  • restore rdi
  • restore rbp

And just out of curiosity, here's a version without recursion, just one loop (and few cases handling low values):

.global fibonacci
fibonacci:

cmp $2, %rdi # 2 and higher should be computer normally
jg compute

cmp $0, %rdi
jz zero 
mov $1, %rax # 1st and 2nd elements = 1
ret

zero:
    mov $0, %rax
    ret

compute:
    mov %rdi, %rcx  # Use cpu counter register
    sub $2, %rcx    # The first number that will come out of this loop
                    # is 2, so decrease number of iterations
    mov $1, %r8     # a = 1
    mov $1, %r9     # b = 1

begin_iter:
    mov %r9, %rax   # sum = b
    add %r8, %rax   # sum += a
    mov %r9, %r8    # a = b
    mov %rax, %r9   # b = a

    loop begin_iter

    ret
Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96