【问题标题】:Assembly x86/C -Recursive Binomial Coefficient Segfault/printing pascals triangle汇编 x86/C -递归二项式系数段错误/打印帕斯卡三角形
【发布时间】:2016-11-19 08:00:18
【问题描述】:

我编写了一些代码(c 中的 main,x86 中的子程序)以递归方式计算所有二项式系数并打印出所有 n=10 且受 m

所以基本上我正在尝试输出 n=10 的帕斯卡三角形。 (没有三角形的完整格式)

我的问题是我在编译时遇到了段错误,我无法弄清楚如何打印递归函数生成的各个值。

Segmentation fault (core dumped)

这是主程序:

#include <stdio.h>

unsigned int result,m,n,i;
unsigned int binom(int,int);
int main(){

n=10;


for (i=0; i<n+1;i++){
printf("i=%d | %d \n", i, binom(n,i) );
}

return;


}

还有递归子程序:

    .text
    .globl  binom

binom: 
    mov     $0x00, %edx     #for difference calculation
    cmp     %edi, %esi          #m=n?
    je      equalorzero         #jump to equalorzero for returning of value 1
    cmp     $0x00, %esi         #m=0?
    je      equalorzero     
    cmp     $0x01, %esi         #m=1?

    mov     %esi,%edx
    sub     %edi, %edx
    cmp     $0x01, %edx         # n-m = 1 ?
    je      oneoronedifference  

    jmp     otherwise

equalorzero:
    add     $1, %eax            #return 1
    ret 

oneoronedifference:
    add     %edi, %eax          #return n
    ret

otherwise:
    sub     $1, %edi            #binom(n-1,m) 
    call    binom       
    sub     $1, %esi            #binom(n-1,m-1)
    call    binom

这就是 gcc 给我的

./runtimes
i=0 | 12 
Segmentation fault (core dumped)

【问题讨论】:

  • 标签 otherwise: 之后有 4 行,但没有什么可以结束代码。是否缺少ret?在最后一个call binom 之后,CPU 将继续执行内存中的任何半随机数据,并且会出现段错误、挂起或通常行为不正确。您应该在调试器中运行您的代码。
  • 我的理解是,当调用 binom 时,它会递归为 equalorzero 或 oneoronedifference,其中包含 ret。 - 我会在那里添加一个 ret 以阻止它这样做。
  • 这并没有修复 segfault - 也许它修复了另一个,我确定我需要 ret 最后以防止你提到的内容
  • 你应该试试 gdb...
  • @Egyptian_Coder 请花点时间阅读此评论。现在是您学习如何使用调试器的时候了。您目前在 SO 职业生涯中提出了 8 个问题,它们都是关于调试的。如果您在第一次使用 gdb 之后学会了使用 gdb,那么您现在已经精通了。您的代码有很多初学者错误(错误的寄存器使用、虚假指令、缺少条件),可以通过快速调试轻松修复,并且在您的所有问题中都是不变的。我投票结束这个问题,我希望今后能在你身边看到更成熟的行为。

标签: c recursion assembly segmentation-fault x86-64


【解决方案1】:

您的汇编代码的两个主要问题是:1)您既不加也不返回两个递归调用的总和; 2)您不会将本地人保存在堆栈中,因此它们会被递归调用清除——一旦您从调用中返回,您就使用了错误的值。这是我对您的代码的返工,其中一些更改是由于我在 OSX 下编写的:

递归子程序:

    .text
    .globl  _binom

_binom:
    pushq   %rbp                 # allocate space on stack for locals
    movq    %rsp, %rbp
    subq    $24, %rsp

    cmpl    %edi, %esi           # m == n ?
    je      equalorzero          # jump to equalorzero for returning of value 1
    cmpl    $0, %esi             # m == 0 ?
    je      equalorzero     

    movl    %esi, %edx
    subl    %edi, %edx
    cmpl    $1, %edx             # n - m == 1 ?
    je      oneoronedifference  

    subl    $1, %edi             # binom(n - 1, m) 
    movl    %edi, -4(%rbp)
    movl    %esi, -8(%rbp)
    callq   _binom

    movl    %eax, -12(%rbp)      # save result to stack

    movl    -4(%rbp), %edi
    movl    -8(%rbp), %esi
    subl    $1, %esi             # binom(n - 1, m - 1)
    callq   _binom

    addl    -12(%rbp), %eax      # add results of the two recursive calls
    addq    $24, %rsp            # release locals space on stack
    popq    %rbp
    retq

equalorzero:
    movl    $1, %eax             # return 1
    addq    $24, %rsp            # release locals space on stack
    popq    %rbp
    retq

oneoronedifference:
    movl    %edi, %eax           # return n
    addq    $24, %rsp            # release locals space on stack
    popq    %rbp
    retq

主程序:

#include <stdio.h>

extern unsigned int binom(int, int);

int main() {

    int n = 10;

    for (int i = 0; i <= n; i++) {
        printf("i=%d | %d\n", i, binom(n, i));
    }

    return 0;
}

结果:

i=0 | 1
i=1 | 10
i=2 | 45
i=3 | 120
i=4 | 210
i=5 | 252
i=6 | 210
i=7 | 120
i=8 | 45
i=9 | 10
i=10 | 1

【讨论】:

    猜你喜欢
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多