齐天大圣
齐天大圣

Reputation: 1189

X64 assembly: terminated by signal SIGBUS (Misaligned address error)

I am writing a simple x64 program which calls a C program to print a string. I am using 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 program:

#include <stdio.h>

void 
print_string(char *str) 
{
    printf("%s\n", str);
}

But why am i getting './output' terminated by signal SIGBUS (Misaligned address error). Can anyone please explain to me?

Upvotes: 0

Views: 939

Answers (1)

user149341
user149341

Reputation:

The first line of your .s file switches to the data segment, and never switches back. This leaves the main function in the data segment, which is not executable.

Switch back to the text segment using .text before you start writing any code. (Or switch to the data segment and define your string constant after main.)

Upvotes: 2

Related Questions