views:

242

answers:

5

i have some code(inline assembly).

void NativeLoop()
{
    int m;
    __asm
    {
        PUSH ECX
        PUSH EDX
        MOV  ECX, 100000000
NEXTLOOP:
        MOV  EDX, ECX
        AND  EDX, 0X7FFFFFFF
        MOV  DWORD PTR m, EDX
        DEC  ECX
        JNZ  NEXTLOOP
        POP  EDX
        POP  ECX
    }
}

MS C++ Automagicaly adds these codes(marked with **) to my procedure.
Why?
how to avoid it?

  **push        ebp  
  **mov         ebp,esp 
  **push        ecx  
  push        ecx  
  push        edx  
  mov         ecx,5F5E100h 
NEXTLOOP:
  mov         edx,ecx 
  and         edx,7FFFFFFFh 
  mov         dword ptr m,edx 
  dec         ecx  
  jnz         NEXTLOOP
  pop         edx  
  pop         ecx  
  **mov         esp,ebp 
  **pop         ebp  
  **ret
+5  A: 

It's maintaining the call stack. If you defined the function as

int NativeLoop() { }

You would see the same assembly.

David Gladfelter
that made more code.
Behrooz
maybe because David's function is of type int
phresnel
You probably saw something with eax, such as xor eax, eax if you said "return 0;" for the return value. This is because eax is the return value register and an xor of identical values results in zero.
David Gladfelter
@phresnel, Oh, right, his was void.
David Gladfelter
@David Gladfelter:It was xor eax, eax.(I think,bad memory)
Behrooz
A: 

If you can do a search on C++ calling conventions, you'll understand better what the compiler is doing.

gatorfax
+6  A: 

I remember that you can __declspec(naked) in MSVC++, meaning that you have to take care of the stack yourself, that means you must save every register you clobber, and restore it.

There is no the-one-rule to do that properly, as it depends on calling convention. See http://en.wikipedia.org/wiki/X86_calling_conventions .

Sidenote: In gcc, you explitly state to the compiler what you will drive invalid, so that gcc will output more optimal save/restore/stackframe-code, if any. In MSVC, asm is mostly a blackbox to the compiler, for which it will often/always the worst.

See http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#ss5.3 , gcc inline asm syntax is more ugly, but effectively more effective.

phresnel
99.9% of the time the compiler is going to generate better ASM than you in any case, and it's probably a good idea to not use ASM in normal code. Perhaps this is why MSVCC doesn't even bother looking at ASM code sections.
Billy ONeal
Of course. But in systems programming (not in my case, btw), you often need (inline) assembler, and then it's good when the compiler can optimize even that. But then, MSVC++ is anyways a wrong choice, as it doesn't even support amd64 targets (i.e. you'll have to write pure assembly and hook this up to your program)
phresnel
+14  A: 

It is the standard function entry and exit code. It establishes and tears down the stack frame. If you don't want it you can use __declspec(naked). Don't forget to include the RET if you do.

However, your snippet relies on a valid stack frame, your "m" variable requires it. It is addressed at [ebp-10]. Without the preamble, the ebp register won't be set correctly and you'll corrupt the stack frame of the caller.

Hans Passant
not only RET, but also stackframe/save/restore ;)
phresnel
It's okay, he's not using the stack frame. No function arguments, no local variables.
Hans Passant
@nobugz: Huh? And what do you think his `m` is if not a local variable? If fact, his reference to `dword ptr m` will be translated into a `ebp`-based memory access, which is why the code inserted by the compiler is absolutely necessary.
AndreyT
@andrey: missed that, you are correct.
Hans Passant
note that even when there are no arguments, in most cases you'll mutate registers, and then you should know if your calling convention will leave the registers in a fine state, of if you have to write save/restore code by hand.
phresnel
+3  A: 
  **push        ebp  ;save EBP register
  **mov         ebp,esp  ;Save the stackframe
  **push        ecx  ; So that the variable `m` has an address
;...
  **mov         esp,ebp ;restore the stack frame to it's original address
  **pop         ebp   ;restore EBP register
  **ret ;return from function call
Earlz
+1 for cool explaination.
Behrooz
`push ecx` is its way of making `m` addressable. The standard requires that all non-reference, non-type identifiers (IIRC) have an address. So, VC is putting `m` (stored in `ecx`) onto the stack, where it will have a memory address (which is then referenced in the assembly). It's not being "dumb."
greyfade
I wasn't sure.. my C/assembly mixing is a bit rusty.
Earlz