ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

64位ATT汇编语言as汇编ld链接,执行报错Segmentation fault

2026/8/16 11:16:29 拓冰建站 浏览量
64位ATT汇编语言as汇编ld链接,执行报错Segmentation fault

absCallAndPrintAbsAsLd.s里边的内容如下:

.section .datastringToShow:.ascii "The abs of number is %d\n\0"
.global _start
.section .text
_start:pushq %rbpmovq %rsp,%rbpmovq $-5,%rdicall absmovq $stringToShow,%rdimovq %rax,%rsicall printfpopq %rbpmovq %rax,%rdimovq $0x3c,%raxsyscall

as -g absCallAndPrintAbsAsLd.s -o absCallAndPrintAbsAsLd.o进行汇编。
ld -g absCallAndPrintAbsAsLd.o -o absCallAndPrintAbsAsLd -lc -I /usr/lib64/ld-linux-x86-64.so.2进行链接。
./absCallAndPrintAbsAsLd执行报错Segmentation fault
在这里插入图片描述

我把rsp中的地址加上8之后,就不报错了,因为这属于平栈了。

.section .datastringToShow:.ascii "The abs of number is %d\n\0"
.global _start
.section .text
_start:pushq %rbpmovq %rsp,%rbpmovq $-5,%rdicall abssubq $8,%rspmovq $stringToShow,%rdimovq %rax,%rsicall printfaddq $8,%rsppopq %rbpmovq %rax,%rdimovq $0x3c,%raxsyscall

as -g absCallAndPrintAbsAsLd.s -o absCallAndPrintAbsAsLd.o进行汇编。
ld -g absCallAndPrintAbsAsLd.o -o absCallAndPrintAbsAsLd -lc -I /usr/lib64/ld-linux-x86-64.so.2进行链接。
./absCallAndPrintAbsAsLd执行
在这里插入图片描述