Since the addition of i386 and x86_64 to the Mac OS’s repertoire several years back, remembering which registers are used for what has become difficult, and this can complicate the debugging of code for which you have no symbols. So here is my cheat-sheet (posted here, mostly so that I can find it again without google-ing for old mailing list posts; but, I figure someone else may find it useful as well):
arm (before prolog)
$r0➡ arg0 (self)
$r1➡ arg1 (_cmd)
$r3➡ arg0 (self)
$r4➡ arg1 (_cmd)
i386 (before prolog)
*($esp)➡ arg0 (self)
*($esp+4)➡ arg1 (_cmd)
i386 (after prolog)
*($ebp+4)➡ Return addr
*($ebp+8)➡ arg0 (self)
*($ebp+12)➡ arg1 (_cmd)
*($ebp)➡ Previous $ebp
$rdi➡ arg0 (self)
$rsi➡ arg1 (_cmd)
So, if you have a method defined as:
-(id)method:(id)foo bar:(id)bar baz:(id)baz
you can print each of the parameters with:
|arm||ppc/ppc64||x86_64||i386 (before prolog)||i386 (after prolog)|
As Blake mentioned in his comment, on i386, if you’re at the beginning of a function or method, before the prolog has executed (i.e. the bit of code responsible for saving registers, adjusting the stack pointer, etc.), then ebp won’t have been set up for you yet.
So, I’ve amended the above table.
That complexity is another reason I long for the simplicity of PowerPC asm, not to mention M68k asm; at least x86_64 has made the step towards using registers for parameters where possible.
Edited to add: In case it isn’t obvious, these particular stack offsets and registers assignments only make sense when dealing with pointer and integer parameters and return values. When structures and floating point values come into the mix, things can get more complicated.
Edited to add: I’ve added registers/stack offsets for arm. But note that these are for before the prolog has executed. Arm code seems much looser about what happens in its function prologs, so there really isn’t a standard layout post-prolog