7

I compiled the code below with the VC++ 2010 compiler:

__declspec(dllexport)
unsigned int __cdecl __mm_getcsr(void) { return _mm_getcsr(); }

and the generated code was:

push ECX
    stmxcsr [ESP]
    mov EAX, [ESP]
pop ECX
retn

Why is there a push ECX/pop ECX instruction pair?

user541686
  • 205,094
  • 128
  • 528
  • 886

3 Answers3

12

The compiler is making room on the stack to store the MXCSR. It could have equally well done this:

sub esp,4
stmxcsr [ESP]
mov EAX, [ESP]
add esp,4
retn

But "push ecx" is probably shorter or faster.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
3

The push here is used to allocate 4 bytes of temporary space. [ESP] would normally point to the pushed return address, which we cannot overwrite.

ECX will be overwritten here, however, ECX is a probably a volatile register in the ABI you're targeting, so functions don't have to preserve ECX.

The reason a push/pop is used here is a space (and possibly speed) optimization.

Maister
  • 4,978
  • 1
  • 31
  • 34
0

It creates an top-of-stack entry that ESP now refers to as the target for the stmxcsr instruction. Then the result is stored in EAX for the return.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251