What registers must be preserved by an x86 function?
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.
Currently I'm only restoring esp
and ebp
, while the return value is in eax
.
Are there any other registers I should be concerned about, or could I leave whatever pleases me in them?
assembly x86 calling-convention
add a comment |
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.
Currently I'm only restoring esp
and ebp
, while the return value is in eax
.
Are there any other registers I should be concerned about, or could I leave whatever pleases me in them?
assembly x86 calling-convention
1
There is no general rule -- you'll have to consult the C compiler's documentation to see what it expects.
– Jerry Coffin
Mar 7 '12 at 14:14
I'm pretty sure Jerry's right, and it depends on the calling convention; if memory serves, there are different "standard" conventions on Windows, Posix etc. Which compiler are you using?
– Tomer Gabel
Mar 7 '12 at 14:18
gcc. I found this searching en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions (see exit sequence), but it doesn't say anything about the registers, just that they should be restored to a state expected by the caller.
– bobbaluba
Mar 7 '12 at 14:26
add a comment |
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.
Currently I'm only restoring esp
and ebp
, while the return value is in eax
.
Are there any other registers I should be concerned about, or could I leave whatever pleases me in them?
assembly x86 calling-convention
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.
Currently I'm only restoring esp
and ebp
, while the return value is in eax
.
Are there any other registers I should be concerned about, or could I leave whatever pleases me in them?
assembly x86 calling-convention
assembly x86 calling-convention
edited Nov 20 '18 at 22:10
Peter Cordes
122k17184312
122k17184312
asked Mar 7 '12 at 14:09
bobbalubabobbaluba
1,63911834
1,63911834
1
There is no general rule -- you'll have to consult the C compiler's documentation to see what it expects.
– Jerry Coffin
Mar 7 '12 at 14:14
I'm pretty sure Jerry's right, and it depends on the calling convention; if memory serves, there are different "standard" conventions on Windows, Posix etc. Which compiler are you using?
– Tomer Gabel
Mar 7 '12 at 14:18
gcc. I found this searching en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions (see exit sequence), but it doesn't say anything about the registers, just that they should be restored to a state expected by the caller.
– bobbaluba
Mar 7 '12 at 14:26
add a comment |
1
There is no general rule -- you'll have to consult the C compiler's documentation to see what it expects.
– Jerry Coffin
Mar 7 '12 at 14:14
I'm pretty sure Jerry's right, and it depends on the calling convention; if memory serves, there are different "standard" conventions on Windows, Posix etc. Which compiler are you using?
– Tomer Gabel
Mar 7 '12 at 14:18
gcc. I found this searching en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions (see exit sequence), but it doesn't say anything about the registers, just that they should be restored to a state expected by the caller.
– bobbaluba
Mar 7 '12 at 14:26
1
1
There is no general rule -- you'll have to consult the C compiler's documentation to see what it expects.
– Jerry Coffin
Mar 7 '12 at 14:14
There is no general rule -- you'll have to consult the C compiler's documentation to see what it expects.
– Jerry Coffin
Mar 7 '12 at 14:14
I'm pretty sure Jerry's right, and it depends on the calling convention; if memory serves, there are different "standard" conventions on Windows, Posix etc. Which compiler are you using?
– Tomer Gabel
Mar 7 '12 at 14:18
I'm pretty sure Jerry's right, and it depends on the calling convention; if memory serves, there are different "standard" conventions on Windows, Posix etc. Which compiler are you using?
– Tomer Gabel
Mar 7 '12 at 14:18
gcc. I found this searching en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions (see exit sequence), but it doesn't say anything about the registers, just that they should be restored to a state expected by the caller.
– bobbaluba
Mar 7 '12 at 14:26
gcc. I found this searching en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions (see exit sequence), but it doesn't say anything about the registers, just that they should be restored to a state expected by the caller.
– bobbaluba
Mar 7 '12 at 14:26
add a comment |
3 Answers
3
active
oldest
votes
Using Microsoft's 32 bit ABI (cdecl
or stdcall
or other calling conventions), EAX
, EDX
and ECX
are scratch registers (call clobbered). The other general-purpose integer registers are call-preserved.
The condition codes in EFLAGS are call-clobbered. DF=0 is required on call/return so you can use rep movsb
without a cld
first. The x87 stack must be empty on call, or on return from a function that doesn't return an FP value. (FP return values go in st0
, with the x87 stack empty other than that.) XMM6 and 7 are call-preserved, the rest are call-clobbered scratch registers.
Outside of Windows, most 32-bit calling conventions (including i386 System V on Linux) agree with this choice of EAX, EDX and ECX as call-clobbered, but all the xmm registers are call-clobbered.
For x64 under Windows, you only need to restore RBX
, RBP
, RDI
, RSI
, R12
, R13
, R14
, and R15
. XMM6..15 are call-preserved. (And you have to reserve 32 bytes of shadow space for use by the callee, whether or not there are any args that don't fit in registers.) xmm6..15 are call-preserved.
See https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention for more details.
Other OSes use the x86-64 System V ABI (see figure 3.4), where the call-preserved integer registers are RBP
, RBX
, RSP
, R12
, R13
, R14
, and R15
. All the XMM/YMM/ZMM registers are call-clobbered.
EFLAGS and the x87 stack are the same as in 32-bit conventions: DF=0, condition flags are clobbered, and x87 stack is empty. (x86-64 conventions return FP values in XMM0, so the x87 stack registers always need to be empty on call/return.)
For links to official calling convention docs, see https://stackoverflow.com/tags/x86/info
add a comment |
32-bit: EBX, ESI, EDI, EBP
64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
64-bit Linux,BSD,Mac: RBX, RBP, R12-R15
For details see "Software optimization resources" by Agner Fog. Calling conventions are described in this pdf.
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
y'know, I was going to point that out too, but theret
instruction does take care of the instruction pointer for you.
– Stephen Canon
Mar 7 '12 at 14:39
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
add a comment |
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.
2
ThePOPA
andPOPAD
instructions don't actually pop the (E)SP register! Also please correct an error onPOPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!
– Sep Roland
Aug 9 '15 at 21:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9603003%2fwhat-registers-must-be-preserved-by-an-x86-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using Microsoft's 32 bit ABI (cdecl
or stdcall
or other calling conventions), EAX
, EDX
and ECX
are scratch registers (call clobbered). The other general-purpose integer registers are call-preserved.
The condition codes in EFLAGS are call-clobbered. DF=0 is required on call/return so you can use rep movsb
without a cld
first. The x87 stack must be empty on call, or on return from a function that doesn't return an FP value. (FP return values go in st0
, with the x87 stack empty other than that.) XMM6 and 7 are call-preserved, the rest are call-clobbered scratch registers.
Outside of Windows, most 32-bit calling conventions (including i386 System V on Linux) agree with this choice of EAX, EDX and ECX as call-clobbered, but all the xmm registers are call-clobbered.
For x64 under Windows, you only need to restore RBX
, RBP
, RDI
, RSI
, R12
, R13
, R14
, and R15
. XMM6..15 are call-preserved. (And you have to reserve 32 bytes of shadow space for use by the callee, whether or not there are any args that don't fit in registers.) xmm6..15 are call-preserved.
See https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention for more details.
Other OSes use the x86-64 System V ABI (see figure 3.4), where the call-preserved integer registers are RBP
, RBX
, RSP
, R12
, R13
, R14
, and R15
. All the XMM/YMM/ZMM registers are call-clobbered.
EFLAGS and the x87 stack are the same as in 32-bit conventions: DF=0, condition flags are clobbered, and x87 stack is empty. (x86-64 conventions return FP values in XMM0, so the x87 stack registers always need to be empty on call/return.)
For links to official calling convention docs, see https://stackoverflow.com/tags/x86/info
add a comment |
Using Microsoft's 32 bit ABI (cdecl
or stdcall
or other calling conventions), EAX
, EDX
and ECX
are scratch registers (call clobbered). The other general-purpose integer registers are call-preserved.
The condition codes in EFLAGS are call-clobbered. DF=0 is required on call/return so you can use rep movsb
without a cld
first. The x87 stack must be empty on call, or on return from a function that doesn't return an FP value. (FP return values go in st0
, with the x87 stack empty other than that.) XMM6 and 7 are call-preserved, the rest are call-clobbered scratch registers.
Outside of Windows, most 32-bit calling conventions (including i386 System V on Linux) agree with this choice of EAX, EDX and ECX as call-clobbered, but all the xmm registers are call-clobbered.
For x64 under Windows, you only need to restore RBX
, RBP
, RDI
, RSI
, R12
, R13
, R14
, and R15
. XMM6..15 are call-preserved. (And you have to reserve 32 bytes of shadow space for use by the callee, whether or not there are any args that don't fit in registers.) xmm6..15 are call-preserved.
See https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention for more details.
Other OSes use the x86-64 System V ABI (see figure 3.4), where the call-preserved integer registers are RBP
, RBX
, RSP
, R12
, R13
, R14
, and R15
. All the XMM/YMM/ZMM registers are call-clobbered.
EFLAGS and the x87 stack are the same as in 32-bit conventions: DF=0, condition flags are clobbered, and x87 stack is empty. (x86-64 conventions return FP values in XMM0, so the x87 stack registers always need to be empty on call/return.)
For links to official calling convention docs, see https://stackoverflow.com/tags/x86/info
add a comment |
Using Microsoft's 32 bit ABI (cdecl
or stdcall
or other calling conventions), EAX
, EDX
and ECX
are scratch registers (call clobbered). The other general-purpose integer registers are call-preserved.
The condition codes in EFLAGS are call-clobbered. DF=0 is required on call/return so you can use rep movsb
without a cld
first. The x87 stack must be empty on call, or on return from a function that doesn't return an FP value. (FP return values go in st0
, with the x87 stack empty other than that.) XMM6 and 7 are call-preserved, the rest are call-clobbered scratch registers.
Outside of Windows, most 32-bit calling conventions (including i386 System V on Linux) agree with this choice of EAX, EDX and ECX as call-clobbered, but all the xmm registers are call-clobbered.
For x64 under Windows, you only need to restore RBX
, RBP
, RDI
, RSI
, R12
, R13
, R14
, and R15
. XMM6..15 are call-preserved. (And you have to reserve 32 bytes of shadow space for use by the callee, whether or not there are any args that don't fit in registers.) xmm6..15 are call-preserved.
See https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention for more details.
Other OSes use the x86-64 System V ABI (see figure 3.4), where the call-preserved integer registers are RBP
, RBX
, RSP
, R12
, R13
, R14
, and R15
. All the XMM/YMM/ZMM registers are call-clobbered.
EFLAGS and the x87 stack are the same as in 32-bit conventions: DF=0, condition flags are clobbered, and x87 stack is empty. (x86-64 conventions return FP values in XMM0, so the x87 stack registers always need to be empty on call/return.)
For links to official calling convention docs, see https://stackoverflow.com/tags/x86/info
Using Microsoft's 32 bit ABI (cdecl
or stdcall
or other calling conventions), EAX
, EDX
and ECX
are scratch registers (call clobbered). The other general-purpose integer registers are call-preserved.
The condition codes in EFLAGS are call-clobbered. DF=0 is required on call/return so you can use rep movsb
without a cld
first. The x87 stack must be empty on call, or on return from a function that doesn't return an FP value. (FP return values go in st0
, with the x87 stack empty other than that.) XMM6 and 7 are call-preserved, the rest are call-clobbered scratch registers.
Outside of Windows, most 32-bit calling conventions (including i386 System V on Linux) agree with this choice of EAX, EDX and ECX as call-clobbered, but all the xmm registers are call-clobbered.
For x64 under Windows, you only need to restore RBX
, RBP
, RDI
, RSI
, R12
, R13
, R14
, and R15
. XMM6..15 are call-preserved. (And you have to reserve 32 bytes of shadow space for use by the callee, whether or not there are any args that don't fit in registers.) xmm6..15 are call-preserved.
See https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention for more details.
Other OSes use the x86-64 System V ABI (see figure 3.4), where the call-preserved integer registers are RBP
, RBX
, RSP
, R12
, R13
, R14
, and R15
. All the XMM/YMM/ZMM registers are call-clobbered.
EFLAGS and the x87 stack are the same as in 32-bit conventions: DF=0, condition flags are clobbered, and x87 stack is empty. (x86-64 conventions return FP values in XMM0, so the x87 stack registers always need to be empty on call/return.)
For links to official calling convention docs, see https://stackoverflow.com/tags/x86/info
edited Nov 20 '18 at 22:35
Peter Cordes
122k17184312
122k17184312
answered Mar 7 '12 at 14:43
NecrolisNecrolis
22.2k24694
22.2k24694
add a comment |
add a comment |
32-bit: EBX, ESI, EDI, EBP
64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
64-bit Linux,BSD,Mac: RBX, RBP, R12-R15
For details see "Software optimization resources" by Agner Fog. Calling conventions are described in this pdf.
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
y'know, I was going to point that out too, but theret
instruction does take care of the instruction pointer for you.
– Stephen Canon
Mar 7 '12 at 14:39
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
add a comment |
32-bit: EBX, ESI, EDI, EBP
64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
64-bit Linux,BSD,Mac: RBX, RBP, R12-R15
For details see "Software optimization resources" by Agner Fog. Calling conventions are described in this pdf.
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
y'know, I was going to point that out too, but theret
instruction does take care of the instruction pointer for you.
– Stephen Canon
Mar 7 '12 at 14:39
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
add a comment |
32-bit: EBX, ESI, EDI, EBP
64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
64-bit Linux,BSD,Mac: RBX, RBP, R12-R15
For details see "Software optimization resources" by Agner Fog. Calling conventions are described in this pdf.
32-bit: EBX, ESI, EDI, EBP
64-bit Windows: RBX, RSI, RDI, RBP, R12-R15, XMM6-XMM15
64-bit Linux,BSD,Mac: RBX, RBP, R12-R15
For details see "Software optimization resources" by Agner Fog. Calling conventions are described in this pdf.
answered Mar 7 '12 at 14:30
Evgeny KluevEvgeny Kluev
22.4k64277
22.4k64277
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
y'know, I was going to point that out too, but theret
instruction does take care of the instruction pointer for you.
– Stephen Canon
Mar 7 '12 at 14:39
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
add a comment |
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
y'know, I was going to point that out too, but theret
instruction does take care of the instruction pointer for you.
– Stephen Canon
Mar 7 '12 at 14:39
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
Don't forget RSP =)
– Stephen Canon
Mar 7 '12 at 14:34
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
@StephenCanon, and EIP/RIP :)
– Evgeny Kluev
Mar 7 '12 at 14:37
y'know, I was going to point that out too, but the
ret
instruction does take care of the instruction pointer for you.– Stephen Canon
Mar 7 '12 at 14:39
y'know, I was going to point that out too, but the
ret
instruction does take care of the instruction pointer for you.– Stephen Canon
Mar 7 '12 at 14:39
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
+1 for the links to Agner Fog's resources on optimization and calling conventions. A very helpful source of information, indeed.
– Eugene
Mar 8 '12 at 7:54
add a comment |
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.
2
ThePOPA
andPOPAD
instructions don't actually pop the (E)SP register! Also please correct an error onPOPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!
– Sep Roland
Aug 9 '15 at 21:13
add a comment |
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.
2
ThePOPA
andPOPAD
instructions don't actually pop the (E)SP register! Also please correct an error onPOPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!
– Sep Roland
Aug 9 '15 at 21:13
add a comment |
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.
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.
edited Aug 9 '15 at 21:16
answered Aug 9 '15 at 20:26
TanzerTanzer
14910
14910
2
ThePOPA
andPOPAD
instructions don't actually pop the (E)SP register! Also please correct an error onPOPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!
– Sep Roland
Aug 9 '15 at 21:13
add a comment |
2
ThePOPA
andPOPAD
instructions don't actually pop the (E)SP register! Also please correct an error onPOPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!
– Sep Roland
Aug 9 '15 at 21:13
2
2
The
POPA
and POPAD
instructions don't actually pop the (E)SP register! Also please correct an error on POPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!– Sep Roland
Aug 9 '15 at 21:13
The
POPA
and POPAD
instructions don't actually pop the (E)SP register! Also please correct an error on POPAD
: you forgot the EBX register. Furthermore you are wrongly stating that these instructions touch the segment registers!– Sep Roland
Aug 9 '15 at 21:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9603003%2fwhat-registers-must-be-preserved-by-an-x86-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There is no general rule -- you'll have to consult the C compiler's documentation to see what it expects.
– Jerry Coffin
Mar 7 '12 at 14:14
I'm pretty sure Jerry's right, and it depends on the calling convention; if memory serves, there are different "standard" conventions on Windows, Posix etc. Which compiler are you using?
– Tomer Gabel
Mar 7 '12 at 14:18
gcc. I found this searching en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions (see exit sequence), but it doesn't say anything about the registers, just that they should be restored to a state expected by the caller.
– bobbaluba
Mar 7 '12 at 14:26