tags:

views:

2437

answers:

11

I'd like to learn assembler. However, there are very few resources for doing assembler with OS X.

Is there anyone out there who has programmed in assembly on a Mac? Where did you learn?

And, is there any reason I shouldn't be doing assembly? Do I risk (significantly) crashing my computer irreparably?

A: 

There's no reason why you shouldn't; there is nothing you can do in assembly language that you can't do in a higher level language like C.

As far as tools go, you might want to install MacPorts and get the GNU assembler. That may or may not be the easiest way, but it's free and you can probably find tutorial documentation for writing Unix programs in GNU assembler somewhere on the net.

Greg Hewgill
+1  A: 

XCode (ie. GCC) has great support for writing assembler. It's a fun thing to learn (although you're unlikely to need it much), and the worst you can do is crash the program you're writing, same as in C. Just Google for 'gcc inline asm x86 tutorial' and you should find plenty of starting points. Don't worry that some will seem to be Linux specific, they'll generally work just as well in XCode.

(edit) ...assuming you have an Intel Mac of course; if not then replace 'x86' with 'ppc'.

+6  A: 

If you're using a PowerPC Mac, look into gcc inline assembler. Otherwise, look into nasm. I can't give any decent references to PPC ASM (they're few and far between), but I suggest the following things to learn x86 asm:

Also, if you're not in kernel mode then there's no chance of screwing anything up, really, and even if you are in kernel mode it's hard to really destroy anything.

Edit: Also, get gcc and such from XCode not Macports or somesuch. You're in for a world of malformed Mach-O files if you don't. Not fun to diagnose file format issues when you're just starting asm hacking.

Cody Brocious
+1 on the Reversing book - excellent book.
Erik Forbes
+2  A: 

Nasm/yasm are your best bet; gcc inline syntax is quite crippling and can be very painful to use at times, plus there are literally some things it cannot do. Nasm's macro syntax is also much much more useful, a godsend in a language like assembly that has no built-in templating features.

Dark Shikari
+4  A: 

The assembler language is determined by the hardware platform, not the operating system. Given that OS X runs on Intel platform and is 64-bit, you should look for information on x64 (also called AMD64) assembler. Check the Wikipedia article (http://en.wikipedia.org/wiki/X86-64) for a lot of links to documentation about x64.

Also, the OS X tools documentation might contains a lot of information about x64 assembler. In particular, the Netwide Assembler (NASM - http://developer.apple.com/documentation/DeveloperTools/nasm/nasmdoc0.html) might have documentation on how to build OS X applications using assembler.

Franci Penov
Learning x86-64 ASM before learning x86 ASM is much like writing an epic poem before you've learned your first word. So many things in it that will trip new people up. Definitely not recommended.
Cody Brocious
It takes effort to write a 64bit OSX app; they're 32bit by default.
Granted, I am not a Mac developer. However, OSX is 64-bit and http://www.apple.com/macosx/technology/64bit.html claims Xcode 3.0 supports building 64-bit apps, so isn't it just a matter of developer's choice? And knowing what you do of course, but that applies equally for building 32-bit apps.
Franci Penov
+1  A: 

here

I programmed assembly on a Mac. It was Motorola 680x0 assembler using MPW. I've touched on the PowerPC assembler a few times in CodeWarrior and ProjectBuilder. Now ProjectBuilder is called XCode, and there is Intel. The assembler is one of the many tools within XCode.

I originally learned assembler on the Apple II: the 6502 machine language monitor built in ROM, the Sweet16 mini-assembler, and others. Later, I used Intel 80186 assembler to speed up slow bits of C code, and work paid for a one day course on Intel 80186 assembler at a university. Later, I had to maintain some 680x0 assembly for the Mac. That was a long time ago.

I don't think there is any reason not to do assembly. Learning is great. Learn all you can. Drop into a low enough level debugger and look at the disassembled code.

My advice is:

Don't be scared.

Mark Stock
+2  A: 

To start learning assembly, you might want to start with simple C programs and ask GCC to generate the assembler code for it using the -S option:

gcc -S hello.c -o hello.asm

You will then be able to understand how to call functions, pass arguments, etc.

Martin Cote
A: 

There are two three things you to know need for writing assembly language on a system with an operating system (as opposed to 'bare metal' assembly which is a world of its own):

  1. How the instruction set works - loads of resources for Intel X86 if you have an Intel Mac, still reasonable set for PPC for example Mac OS X Internals.

  2. How to assemble link your programmes - if you have the Developer tools installed you have GCC and associated tools

  3. How to talk to the OS - here is where Mac assembly is a lot less well documented than Windows or Linux. It may be you have to write equivalent C programs and use 'gcc -S' to see what calling/stack restoration conventions are appropriate. It depends what you want to do but at a miniumum you need OS system calls for IO and memory allocation.

A good starting point is here.

domgblackwell
A: 

If you've got Developer Tools installed, you can simply open Terminal and type as (GNU Assembler - part of Binutils).

Tommy Lillehagen
A: 

For PPC try Lightsoft

A: 

See this question.

Tom McCann