嘗試運行GDB並在C主程序進入主函數後繼續發生分段錯誤。C/Assembly子程序分段錯誤
GDB錯誤:
Breakpoint 1, main() at binom_main.c:7
7 n=10;
(gdb) s
10 0;
(gdb) s
12 +){
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005c4 in otherwise()
(gdb)
我編譯的代碼這樣:
as binom.s -o binom.o
gcc -S -Og binom_main.c
gcc -c binom_main.s
gcc binom_main.o binom.o -o runtimes
我想學習如何使用GDB更有效率些,但像這樣的段錯誤是相當含糊和限制。爲什麼這個段錯誤是在函數開始的時候引起的?我有錯誤地鏈接了兩個文件嗎?
主營:
#include <stdio.h>
unsigned int result,m,n,i;
unsigned int binom(int,int);
int main(){
n=10;
i=0;
for (i=1; i<2;i++){
result = binom(n,i);
printf("i=%d | %d \n", i, result);
}
return 0;
}
子:
.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
call printf
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
ret
是你運行的所有命令?我沒有看到兩個'.o'文件的鏈接。你需要做一些像'gcc -o binom binom_main.o binom.o'。 – kaylum
第二次看,聽起來像是一個堆棧溢出的教科書案例(哈哈)。請注意,否則()會得到錯誤'',因此在輸入'main'時絕對不對。 PS:你也使用'printf',這可能是也可能不是一個促成因素。 – Jester
@kaylum是的!我忘了把它包含在OP中。我雖然編輯它。對於那個很抱歉。 –