2

I have a c program:

#include <unistd.h>
#include <stdlib.h>
int main() {
  const char msg[] = "Hello, ARM!\n";   
  write(0, msg, sizeof(msg));
  exit(0);
}   

And I want to generate the Apple Silicon arm64 assembly of this C program. How do I do this on an x86 macOS?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
zendevil.eth
  • 974
  • 2
  • 9
  • 28
  • 1
    Does this answer your question? [How to get arm64 assembly when the -S flag in gcc?](https://stackoverflow.com/questions/67957722/how-to-get-arm64-assembly-when-the-s-flag-in-gcc) – Alex Lop. Jun 13 '21 at 12:22
  • 1
    https://godbolt.org/z/nb9q3eMa6 ? – Alex Lop. Jun 13 '21 at 12:27
  • 1
    `write(0, ...` are you sure? Do you know what is file descriptor `0`? – qrdl Jun 13 '21 at 12:46
  • Possibly a duplicate of [Can XCode build native Intel binaries on a M1 Mac](https://stackoverflow.com/a/67945229), but that mostly just links to https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary which tells you what version of Xcode to install to make universal binaries. (Building on either hardware). It doesn't go into detail about how to get the compiler to show you the asm for each target separately. Although possibly the `-target` options mentioned in [this Q&A](https://stackoverflow.com/q/67945226) would work with `-S`. – Peter Cordes Jun 13 '21 at 13:08
  • 1
    Not closing as a duplicate because it doesn't seem like a silly or obvious question, and future readers could probably benefit from someone putting the pieces together into an answer. (I don't have a Mac so I couldn't test it.) – Peter Cordes Jun 13 '21 at 13:10
  • 2
    you use a cross compiler that runs on x86 but generates aarch64...or godbolt – old_timer Jun 13 '21 at 15:30
  • @old_timer: Godbolt's compilers are set up to target Linux, not MacOS. The general picture is of course the same, but stuff like symbol names would be different, and IIRC section names like `.rodata` are different on MacOS. The example given just calls functions (if copying the string to the stack optimizes away), and if MacOS decorates symbol names with a leading `_` on AArch64 like on x86-64, that would also be a difference. – Peter Cordes Jun 13 '21 at 18:04
  • a few C calls are a few C calls, nothing here is os specific – old_timer Jun 13 '21 at 18:11

1 Answers1

-2

If you have installed the XCode Command Line Developer Tools, you can type in your Terminal:
clang -S FILE.c -o OUTPUTFILE
That's it!

  • 1
    The question asks to generate ARM assembly on an Intel Mac. On an Intel Mac, `clang` will default to Intel assembly. – Eric Postpischil Mar 28 '22 at 18:53
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '22 at 01:46