6

No matter how hard I google, I can't seem to find a (relatively) easy-to-follow instruction on how to install the GNU Assembler on a mac.

I know I can use gcc -c (Apple Clang on a Mac) to assemble .s / .S files, but I want to use actual GNU Binutils as.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Acharya
  • 128
  • 1
  • 6
  • Which assembler? XCode already comes with assemblers for Intel and ARM (if you have the iPhone SDK). – Carl Norum Nov 11 '09 at 01:04
  • I am looking to install 'gas' : I'm just starting out so please excuse me if I am not answering your question correctly. – Acharya Nov 11 '09 at 01:13
  • Do you have the developer tools installed? A quick check on my system shows assemblers for arm, i386, ppc, ppc64, and x86_64. Do you need a special assembler? What are you trying to do? – Carl Norum Nov 11 '09 at 01:17
  • Yes, I do. I understand that Xcode uses assemblers and therefore comes with one for Intel. Now I need to find a way to actually build my code using Xcode. – Acharya Nov 11 '09 at 01:20
  • I am taking a class on Computer Organization. We are learning IA-32 assembly. – Acharya Nov 11 '09 at 01:22
  • 2
    In XCode all you have to do is add an assembly file to your project. The right things will happen. The other option is to run the assembler from the command line: `as myfile.s` should do it for you. So should `gcc myFile.s`. – Carl Norum Nov 11 '09 at 01:26
  • Wouldn't this belong on Superuser or something? – fresskoma Nov 11 '09 at 01:31
  • Got it. Thanks. I was kinda lost. – Acharya Nov 11 '09 at 01:40
  • 2
    @CarlNorum Isnt it obvious that the GNU assembler uses AT&T syntax, and that is (i hope) what he wants? – imacake Nov 24 '11 at 14:14

4 Answers4

2

yasm works on Mac OSX (I got it from Homebrew), and it has a GNU as syntax parser which can be enabled with -p gas (it may be necessary to also add -r gas). It is not 100% complete, but it covers mostly everything. It can output to a variety of object formats (if cross-compiling is necessary), and in my opinion it's pretty cool. You can also use NASM syntax (which is completely supported) using -p nasm (again, -r nasm may be necessary).

Sorry about necroposting, but this is a still-relevant question and I believe that it needs an acceptable answer.

Arav K.
  • 51
  • 6
  • I think I read some mention of a macports build of `gobjdump`. Is there a similar build of `gas`? I don't have a Mac so I can test it. (But yes +1 for YASM, good idea. Unfortunately it's not been kept up to date; it doesn't support AVX512 instructions. But if you don't need that it's good, and has better long-NOP support than NASM) – Peter Cordes Jul 16 '19 at 08:30
  • I cannot find a `gas` build from MacPorts, unfortunately. I don't have AVX-512 on my Macbook, and so I didn't realize that YASM was incomplete in this regard. What assembler do you use, @PeterCordes? NASM doesn't have as much documentation or directive support as I'd like, and YASM doesn't look updated at all (see their GitHub https://github.com/yasm/yasm). I've heard about FASM, MASM, TASM, etc. but I haven't tried them out. – Arav K. Jul 16 '19 at 09:40
  • I don't have a Mac. I usually use YASM or NASM for hand-written asm experiments. (On GNU/Linux.) I use GAS / AT&T syntax for inline asm sometimes, but almost always I just read asm when doing actual programming, and tweak my C source to get efficient asm. If I was on a Mac, I'd probably just use GAS, probably with Intel-syntax mode, because NASM has had bugs with relocations for rip-relative and absolute addressing modes in its MachO64 output format support. But with a working version of NASM, sure I'd probably use that. – Peter Cordes Jul 16 '19 at 09:50
  • I think the OP is looking for a way to specifically get GNU as on their osx machine. That's what I was looking to do when I stumbled onto this post as well. – Chris Sep 03 '22 at 21:23
  • does yasm work on M1 machines like newer MacBooks? I seem to just get segmentation faults. There's very little authoritative documentation on assmblers for newer Macs. – Klapaucius Klapaucius Oct 08 '22 at 22:22
2

The GNU assembler cannot (yet) be used to create native object files (of Mach-O format). But you can of course use it to cross-assemble for some non-native object format, if that is what you want.

Fabel
  • 1,711
  • 14
  • 36
  • I found this post [Can't seem to get gcc or binutils to build a new linker or assembler](https://stackoverflow.com/questions/9574023/cant-seem-to-get-gcc-or-binutils-to-build-a-new-linker-or-assembler) where a responder mentions no support for as and ld on os x. – Chris Sep 03 '22 at 21:30
0

The gnu assembler is already installed on your mac (assuming that you installed the dev tools package). If you want to avoid XCode, you can invoke it from the command line with as, or with the preprocessor by using gcc yourfile.s $(OPTIONS).


Edit: as now points to the clang assembler; at the time this answer was written it pointed to (Apple's build of) the GNU assembler.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • 5
    That is true. Except that the included one is not the GNU assembler. – Fabel Oct 23 '11 at 00:14
  • That gcc is an alias for clang. gcc --version gives Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 8.1.0 (clang-802.0.38) Target: x86_64-apple-darwin16.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin – Olsonist May 04 '17 at 01:07
  • @Olsonist It was the gnu assembler *at the time this answer was written*. – Stephen Canon Dec 08 '17 at 02:29
  • gcc normally only runs asm through the C preprocessor for `.S` filenames, not `.s`. IDK about Apple clang (which is what `gcc` on MacOS runs these days!). – Peter Cordes Jul 16 '19 at 08:28
-2

Maybe as instead of gas ? If you want to have gas as command invoke this: echo "alias gas=as" >> $HOME/.profile

It was installed on my Mac Lion when i opened the terminal and typed it. It might have been because of MacPorts and/or XCode as mentioned in the comments of a previous answer.

imacake
  • 1,703
  • 7
  • 18
  • 26
  • 1
    as is the llvm assembler as --version gives Apple LLVM version 8.1.0 (clang-802.0.38) Target: x86_64-apple-darwin16.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin – Olsonist May 04 '17 at 01:09