Search code examples
assemblyx86calling-conventionstack-frame

Why are ebx, esi and edi unusable when operating with a stackframe?


I'm having problems operating with such a small number of registers when making a function in assembly - what's inside those registers that's preventing me from using them? Can I copy their contents into a parameter while coding and reset it before exiting the function to not break its purpose?


Solution

  • Why are ebx, esi and edi unusable

    This is entirely dependent on the ABI and calling conventions used by the platform, though, it so happens that most compilers conform to the pattern where the registers eax, ecx and edx are considered volatile across function calls, unlike the other registers.

    Can I copy their contents into a parameter while coding and reset it before exiting the function to not break its purpose?

    Yes, in fact, it's a pretty common idiom in x86 code to restore registers that are supposed to be preserved across function calls like so:

    my_fn:
        push ebx
        push esi
    
    
        ; code ...
    
        pop esi
        pop ebx
        ret
    

    Assuming that the ; code ... part didn't unbalance the stack, esi and ebx are restored to their old values before leaving the function.