c - X64 程序集 : terminated by signal SIGBUS (Misaligned address error)

标签 c x86 64-bit

我正在编写一个简单的 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("%s\n", str);
}

但为什么我得到 './output' 被信号 SIGBUS(未对齐的地址错误)终止。谁能给我解释一下?

最佳答案

.s 文件的第一行切换到数据段,并且永远不会切换回来。这使得数据段中的 main 函数无法执行。

在开始编写任何代码之前,使用 .text 切换回文本段。 (或者切换到数据段并在 main 之后定义您的字符串常量。)

关于c - X64 程序集 : terminated by signal SIGBUS (Misaligned address error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298935/

相关文章:

c++ - 为什么我的 VC 2005 x64 调试 session 在第一次试用时总是失败(但在第二次试用时却没有)?

c - 什么是评价?

ios - 应用扩展 : multiple instances interfering with each other

ARM架构的Linux交叉编译

linux - 我如何在 nasm 中获取系统调用 getcwd 返回字符串的真实长度?

c# - 使用反射从 .NET 64 位 exe 调用 .NET 32 位 dll 中的方法

assembly - x64 - 为什么仍然使用 'call' ?

CodeLite 静态库找不到包含文件

c - C 中的结构数组

c++ - 为什么在这个递归斐波那契代码中 GCC 生成的程序比 Clang 更快?