我有以下 C 程式:
#include <stdio.h>
int main() {
int i = 0;
int N = 10;
while(i < N) {
printf("counting to %d: %d", N, i);
//i = i 1;
}
return 0;
}
我想先將其編譯為匯編,然后再編譯為用于教學目的的二進制檔案。所以,我發出以下命令:
$ gcc -S count.c -o count.s
$ as -o count.o count.s
$ ld -o count -e main -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/libc.so count.o -lc
它們分別將 C 編譯為匯編,將匯編匯編為二進制,然后分別鏈接包含該printf函式的庫。
這有效。輸出:
counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0counting to 10: 0
等等,直到我 ctrl-c 程式。
但是,當我取消注釋該i = i 1行時:
Segmentation fault (core dumped)
這里出了什么問題?
更新:這是count.s(i = i 1包括行)
.file "count.c"
.text
.section .rodata
.LC0:
.string "counting to %d: %d"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -8(%rbp)
movl $10, -4(%rbp)
jmp .L2
.L3:
movl -8(%rbp),
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357359.html
上一篇:錯誤:在1003次通過后無法找到所有標簽的有效值;停滯1001,放棄
下一篇:程式集中的間接尋址(x86)
