22

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?

4

10 回答 10

13

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.

于 2008-09-26T03:09:29.973 回答
5

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.

于 2008-09-26T03:18:53.257 回答
5

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.

于 2008-09-26T03:50:45.410 回答
3

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.

于 2008-09-26T03:12:58.577 回答
1

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'.

于 2008-09-26T03:08:54.093 回答
1

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.

于 2008-09-26T03:21:52.267 回答
0

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.

于 2008-09-26T03:07:45.843 回答
0

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.

于 2008-09-27T12:48:19.390 回答
0

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

于 2008-11-13T20:39:58.450 回答
0

For PPC try Lightsoft

于 2008-11-16T01:58:58.770 回答