Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding disassembly for -(BOOL) f { return true; }

The assembly for the method -(BOOL) f { return true; } (on my iMac) is:

test`-[AppDelegate f]:
    0x1000014d0 <+0>:  pushq  %rbp
    0x1000014d1 <+1>:  movq   %rsp, %rbp
    0x1000014d4 <+4>:  movb   $0x1, %al
    0x1000014d6 <+6>:  movq   %rdi, -0x8(%rbp)
    0x1000014da <+10>: movq   %rsi, -0x10(%rbp)
->  0x1000014de <+14>: movsbl %al, %eax
    0x1000014e1 <+17>: popq   %rbp
    0x1000014e2 <+18>: retq   

(to generate this I set a breakpoint on the return statement and Debug -> Debug Workflow -> Always show disassembly).

I was surprised it is eight instructions.

pushq  %rbp
movq   %rsp, %rbp
:
popq   %rbp
retq

^ this seems to be standard boilerplate for managing the stack and returning.

movb   $0x1, %al
movsbl %al, %eax

^ this loads hex 00 00 00 01 into EAX, which is the register used for the return value.

movq   %rdi, -0x8(%rbp)
movq   %rsi, -0x10(%rbp)

^ but what are these doing? Aren't the above 6 lines sufficient?

EDIT: I found http://www.idryman.org/blog/2014/12/02/writing-64-bit-assembly-on-mac-os-x/ helpful.

like image 789
P i Avatar asked Feb 06 '23 22:02

P i


1 Answers

In ObjC there are two implicit parameters to every method, self and _cmd. These are passed in %rdi and %rsi (that's the rules of the 64-bit ABI). They're being saved to the stack in case we overwrite those registers with another function call somewhere in this method. If you turn on optimizations, you'll see that those instructions are removed (since we never actually need the saved values).

like image 165
Rob Napier Avatar answered Feb 13 '23 05:02

Rob Napier