Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:55:12+00:00 2026-05-27T20:55:12+00:00

I am trying to learn writing assembly language for 64 bit Mac OS. I

  • 0

I am trying to learn writing assembly language for 64 bit Mac OS. I have no problem with 32 bit Mac OS and both 32 bit and 64 bit Linux.

However, Mac OS 64 bit is different and I couldn’t figure out. Therefore I am here to ask for help.

I have not problem using system call to print. However, I would like to learn how to call C functions using 64 bit assembly language of Mac OS.

Please look at the following code

.data
_hello:
    .asciz "Hello, world\n"


.text
.globl _main
_main:
    movq $0, %rax
    movq _hello(%rip), %rdi
    call _printf

I use
$ gcc -arch x86_64 hello.s

to assemble and link.

It generates binary code. However, I got a segmentation fault when running it.

I tried adding “subq $8, %rsp” before calling _printf, still the same result as before.

What did I do wrong?

By the way, is that any way to debug this code on Mac? I tried adding -ggdb or -gstab or -gDWARF, and
$gdb ./a.out, and can’t see the code and set break points.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:55:12+00:00Added an answer on May 27, 2026 at 8:55 pm

    You didn’t say exactly what the problem you’re seeing is, but I’m guessing that you’re crashing at the point of the call to printf. This is because OS X (both 32- and 64-bit) requires that the stack pointer have 16-byte alignment at the point of any external function call.

    The stack pointer was 16-byte aligned when _main was called; that call pushed an eight-byte return address onto the stack, so the stack is not 16-byte aligned at the point of the call to _printf. Subtract eight from %rsp before making the call in order to properly align it.


    So I went ahead and debugged this for you (no magic involved, just use gdb, break main, display/5i $pc, stepi, etc). The other problem you’re having is here:

    movq _hello(%rip), %rdi
    

    This loads the first eight bytes of your string into %rdi, which isn’t what you want at all (in particular, the first eight bytes of your string are exceedingly unlikely to constitute a valid pointer to a format string, which results in a crash in printf). Instead, you want to load the address of the string. A debugged version of your program is:

    .cstring
    _hello: .asciz "Hello, world\n"
    
    .text
    .globl _main
    _main:
        sub  $8, %rsp           // align rsp to 16B boundary
        mov  $0, %rax
        lea  _hello(%rip), %rdi // load address of format string
        call _printf            // call printf
        add  $8, %rsp           // restore rsp
        ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn dependency injection and have come across a problem, when unit
I am trying to learn PHP with codeigniter, had have come across a problem.
Writing a compiler in school, last milestone is generating assembly code. Trying to learn
I'm still trying to learn SQL as of this writing, so I'm a bit
I'm trying to learn Python, (i have 2.5.4) by writing a snake game, but
I am trying to learn log4Net, however, I do not have Visual Studio installed
I'm trying to learn a little more on vectormath through writing a simple ray
I'm trying to learn to use SDL for a little game I'm writing ,
I'm trying to learn Lisp (elisp, actually), and I tried writing the following function
greetngs, i am trying to learn Java and Swing by writing a simple game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.