-2
我已经写了一些代码(主要在c,程序集x86中的子程序)递归地计算所有二项系数并打印出n = 10的所有二项系数,受m < = n限制。装配x86/C递归二项式系数Segfault /打印帕斯卡三角形
所以基本上我试图输出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)
标签'否则:'后面有4行,但没有结束代码。是否缺少'ret'?在最后一次'调用binom'后,CPU将继续执行内存中的任意半随机数据,并且会发生段错误,挂起或一般不正确的操作。您应该在调试器中运行您的代码。 –
我的理解是,当binom被调用时,它会递归到equalorzero或oneoronedifference,它们包含ret。 - 我会在那里添加一个ret来阻止它这样做。 –
这并没有解决segfault - 也许它修复了另一个虽然,我敢肯定,我需要在最后的ret,以防止你提到 –