使用 C 的汇编程序 - 分段错误

Assembler with C - Segmentation Fault

我有两个文件:f1.S 我的斐波那契函数(计算斐波那契数列的第 n 个成员)是用汇编语言编写的,f2.c 斐波那契函数被调用的地方。

以下是这些文件: f1.S

.global fibonacci
fibonacci:

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

movq 16(%rbp), %rax 

cmp [=10=], %rax
je zeroValue
cmp , %rax
je oneValue
jmp more

zeroValue:
addq [=10=], %r8
jmp end

oneValue:
addq , %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;
}

为了编译和链接,我正在执行这些命令:

as f1.S -o f1.o

gcc f2.c -c -o f2.o

gcc f2.o f1.o -o program

一切正常,直到我尝试 运行 我的 exe 文件(程序)。我不能 运行 因为我收到消息:分段错误。我究竟做错了什么? 斐波那契函数肯定没问题,因为我在干净的汇编程序中使用它,然后它就可以工作了。

首先,确定 C 函数将如何调用您的汇编函数。所以你可以准备你的汇编函数来处理调用。

例如,如果 c 调用不会在汇编函数后清除堆栈 return stdcal 那么您必须在汇编代码中清除堆栈

编辑:删除了关于寄存器语法的通知。感谢 Vyktor

在 gdb 中,您会像这样获得 SIGSEGV:

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

更有趣的是你的回溯(结束):

(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?)

因此您成功调用了递归 1 048 289 次...

此外,在第 8 行 (movq 16(%rbp), %rax) 之后,您将得到:

(gdb) i r rax
rax            0x7fffffffe800   140737488349184

从你的函数看来 rax 应该是一个计数器。

由于您使用的是 64 位寄存器,我假设您是 运行宁 x86_64 架构 which doesn't store arguments on stack 但使用注册表:

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

您的解决方案可能 运行 适合 cdecl calling convention,但不适用于 Microsoft x64 调用约定System V AMD64 ABI.

因此,例如,完成此操作的一种方法是(适用于 System V AMD64 ABI):

.global fibonacci
fibonacci:

push %rbp
push %rdi

cmp [=14=], %rdi
je zeroValue
cmp , %rdi
je oneValue
jmp more

zeroValue:
    movq [=14=], %rax
    jmp end

oneValue:
    movq , %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

是这样的:

  • 存储 rbp
  • 存储 rdi
  • 如果 rdi == 0: return 0
  • 如果 rdi == 1: return 1
  • 减少 rdi
  • 调用斐波那契
  • 将结果存储在堆栈中
  • 减少 rdi
  • 调用斐波那契
  • 将结果恢复到 r8
  • 将 r8 添加到结果中
  • 恢复 rdi
  • 恢复 rbp

出于好奇,这是一个没有递归的版本,只有一个循环(很少有情况处理低值):

.global fibonacci
fibonacci:

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

cmp [=15=], %rdi
jz zero 
mov , %rax # 1st and 2nd elements = 1
ret

zero:
    mov [=15=], %rax
    ret

compute:
    mov %rdi, %rcx  # Use cpu counter register
    sub , %rcx    # The first number that will come out of this loop
                    # is 2, so decrease number of iterations
    mov , %r8     # a = 1
    mov , %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