views:

140

answers:

4

Okay. I want to learn how to assemble programs on my Mac (Early 2009 MBP, Intel Core 2 Duo). So far, I understand only that Assembly languages are comprised of direct one-to-one mnemonics for CPU instructions. After some Googling, I've seen a lot of terms, mostly "x86" and "x86_64". I've also seen MASM, NASM, and GAS, among others.

Correct me if I'm wrong:

  1. x86 and x86_64 are instruction sets. If I write something using these instruction sets (as raw machine code), I'm fine so long as my program stays on the processor it was designed for.
  2. NASM, MASM, and GAS are all different assemblers.
  3. There are different Assembly languages. There's the AT&T syntax and the Intel syntax, for example. Support for these syntaxes differ across assemblers.

Now, questions:

  1. As a Mac user, which instruction sets should I be concerned about?
  2. Xcode uses GCC. Does this mean it also uses GAS?
  3. If it does use GAS, then should I be learning the AT&T syntax?
  4. Is there a book I can get on this. Not a tutorial, not a reference manual on the web. Those things assume to much about me; for example, as far as I know, a register is just a little bit of memory on the CPU. That's how little I really know.

Thanks for your help.

A: 

I'll answer the first question: Macs use Intel chips now, and modern processors are 64-bit.

Steven Sudit
So I need x86_64?
Audacitor
Yes. (I'd leave it at word, but there's a minimum.)
Steven Sudit
+2  A: 

If you want to learn assembly language, start with the x86 instruction set. That's the basic set.

A good book on the subject is Randall Hyde's the Art of Assembly Language, which is also available on his website. He uses a high-level assembler to make things easy to grasp and to get going, but deep down it uses GAS.

I don't believe that XCode comes with any assembler, but you can for example find GAS in MacPort's binutils package.

If you just want to make programs on your Mac and you're not that interested in the life of all the bits in the CPU, you're much better off with a more high-level language like Python or Ruby.

kari
Thanks for providing more Mac-specific information than I was able to. I do want to say that, while you're certainly right about an HLL being more productive, it's clear from Audacitor's comments that they don't yet have a basic understanding of ASM. It's this understanding of machine architecture and optimizations, which they'll quickly gain with practice, that continues to have value even when programming far away from the metal. In short, I think they're doing themselves a big favor by learning a bit of ASM now, even if they never expect to use it professionally.
Steven Sudit
True, it was just the "assemble programs" bit in the original question that had me worried about what we're after here.
kari
+1  A: 

"I'm fine so long as my program stays on the processor it was designed for." Not really. In many cases, assembler programs will also make assumptions about the operating system they run on (e.g. when they call library functions or make system calls). Otherwise, your assumpptions are correct.

Onto questions:

  1. Current Macs support both x86 and x86-64 (aka AMD64 aka EM64T aka Intel64). Both 32-bit and 64-bit binaries can be run on recent systems; Apple itself ships its libraries in "fat" (aka "universal") mode, i.e. machine code for multiple architectures.
  2. Use "as -v" to find out what precise assembler you have; mine reports as "Apple Inc version cctools-698.1~1, GNU assembler version 1.38". So yes, it's GAS.
  3. Yes.
  4. http://stackoverflow.com/questions/4845/good-x86-assembly-book
Martin v. Löwis