我正在编写一个简单的x64程序,该程序调用C程序来打印字符串。我正在使用 Mac OS X
X64:
.data
hello_world: .asciz "hello world!n"
.globl _print_string
.globl _main
_main:
pushq %rbp
movq %rsp, %rbp
leaq hello_world(%rip), %rdi
callq _print_string
movq %rbp, %rsp
popq %rbp
C 程序:
#include <stdio.h>
void
print_string(char *str)
{
printf("%sn", str);
}
但是为什么我得到"./输出"被信号 SIGBUS 终止(地址未对齐错误(。谁能给我解释一下?
.s
文件的第一行会切换到数据细分受众群,并且永远不会切换回来。这会将 main
函数保留在数据段中,该数据段不可执行。
在开始编写任何代码之前,使用 .text
切换回文本段。(或者切换到数据段并在 main
后定义字符串常量。