What registers must be preserved by an x86 function?

后端 未结 3 1915
猫巷女王i
猫巷女王i 2020-11-29 11:09

I\'m writing a function in x86 assembly that should be callable from c code, and I\'m wondering which registers i have to restore before i return to the caller.

Curr

3条回答
  •  情话喂你
    2020-11-29 11:38

    if you are unsure about the registers' situation, these instructions below could save the day easily.

    PUSHA/PUSHAD -- Push all General Registers
    POPA/POPAD -- Pop all General Registers

    These instructions push and pop the general purpose and SI/ESI , DI/EDI registers in certain order.

    The order for PUSHA/PUSHAD instruction is as follows.

    Opcode  Instruction  Clocks   Description
    
    60      PUSHA        18       Push AX, CX, DX, BX, original SP, BP, SI, and DI
    60      PUSHAD       18       Push EAX, ECX, EDX, EBX, original ESP, EBP ESI, and EDI
    

    And the order for POPA/POPAD instruction is as follows. (in reverse order)

    Opcode   Instruction   Clocks   Description
    
    61       POPA          24       Pop DI, SI, BP, SP, BX, DX, CX, and AX
    61       POPAD         24       Pop EDI, ESI, EBP, ESP(***),EBX, EDX, ECX, and EAX
    

    *** The ESP value is discarded instead of loaded into ESP.

提交回复
热议问题