x86 Assembly - Why is [e]bx preserved in calling conventions?

后端 未结 3 1683
心在旅途
心在旅途 2021-01-06 01:22

I\'ve noticed that a lot of calling conventions insist that [e]bx be preserved for the callee.

Now, I can understand why they\'d preserve something like [e]sp or [e]

相关标签:
3条回答
  • 2021-01-06 02:17

    One of the main reasons, certainly for the i386 ELF ABI, is that ebx holds the address of the global offset table (GOT) register for position-independent code (PIC). See 3-35 of the specification for the details. It would be disruptive in the extreme, if, say, shared library code had to restore the GOT after every function call return.

    0 讨论(0)
  • 2021-01-06 02:21

    This is a compromise between not saving any of the registers and saving them all. Either saving none, or saving all, could have been proposed, but either extreme leads to inefficiencies caused by copying the contents to memory (the stack). Choosing to allow some registers to be preserved and some not, reduces the average cost of a function call.

    0 讨论(0)
  • Not all registers make good candidates for preserving:

    no (e)ax -- Implicitly used in some instructions; Return value
    no (e)dx -- edx:eax is implicity used in cdq, div, mul and in return values
    
       (e)bx -- generic register, usable in 16-bit addressing modes (base)
       (e)cx -- shift-counts, used in loop, rep
    
       (e)si -- movs operations, usable in 16-bit addressing modes (index)
       (e)di -- movs operations, usable in 16-bit addressing modes (index)
    
    Must (e)bp -- frame pointer, usable in 16-bit addressing modes (base)
    Must (e)sp -- stack pointer, not addressable in 8086 (other than push/pop)
    

    Looking at the table, two registers have good reason to be preserved and two have a reason not to be preserved. accumulator = (e)ax e.g. is the most often used register due to short encoding. SI,DI make a logical register pair -- on REP MOVS and other string operations, both are trashed.

    In a half and half callee/caller saving paradigm the discussion would basically go only if bx/cx is preferred over si/di. In other calling conventions, it's just EDX,EAX and ECX that can be trashed.

    EBX does have a few obscure implicit uses that are still relevant in modern code (e.g. CMPXGH8B / CMPXGH16B), but it's the least special register in 32/64-bit code.

    EBX makes a good choice for a call-preserved register because it's rare that a function will need to save/restore EBX because they need EBX specifically, and not just any non-volatile register. As Brett Hale's answer points out, it makes EBX a great choice for the global offset table (GOT) pointer in ABIs that need one.

    In 16-bit mode, addressing modes were limited to (any subset of) [BP|BX + DI|SI + disp8/disp16]), so BX is definitely special there.

    0 讨论(0)
提交回复
热议问题