Pyjong
Pyjong

Reputation: 3197

cl.exe produces weird assembly code

I compiled this C code:

void foo() {
    int i = 0;
    i = 0;
    i = 0;
}

and I got this:

push ebp
mov ebp,esp
push ecx
mov dword ptr ss:[ebp-4],0
mov dword ptr ss:[ebp-4],0
mov dword ptr ss:[ebp-4],0
mov esp,ebp
pop ebp
retn

My question is why is there push ecx? and how come there is no sub esp,4 or something to make space on the stack? No compiler options used.

Upvotes: 0

Views: 237

Answers (1)

Fred Larson
Fred Larson

Reputation: 62083

Either way will make 4 bytes of space available on the stack, and the push saves a couple of bytes over the sub. Maybe the compiler writer decided to optimize this case by pushing a register.

Upvotes: 4

Related Questions