开发者

Can someone explain these couple assembly lines?

C++

int main(void)
{
    int a = 3;
    int b = 10;
    int c;
    c = a + b;
    return 0;
}
008C1353  sub         esp,0E4h 
......
008C135C  lea         edi,[ebp+FFFFFF1Ch]  
008C1362  mov         ecx,39h  
008C1367  mov         eax,0CCCCCCCCh  
008C136C  rep stos    dword ptr es:[edi]  
     3:     int a = 3;
008C136E  mov         dword ptr [ebp-8],3  
     4:     int b = 10;
008C1375  mov         dword ptr [ebp-14h],0Ah  
     5:     int c;
     6:     c = a + b;

A couple things that I don't understand.

(1) G++ will have stack alignment 16 bytes, and doing this in Visual Studio is 228 bytes??

(2) Doing this on Windows, does the stack grows upward or downward? I am confused. I know how the stack should look like

[Parameter n          ]
...
[Parameter 2  开发者_如何学Python        ]
[Parameter 1          ]
[Return Address       ]   0x002CF744
[Previous EBP         ]   0x002CF740  (current ebp)
[Local Variables      ]   

So would the lowest address be the downward?

(3) When we push the variable a to the stack, it is ebp - 8.How come it's eight bytes? (4) Similarly, why is int b ebp - 14 ?

Can someone please explain this to me? (-4, -8, respectively)

Using GDB, the offset makes more sense to me.

Thanks.


When compiling in debug mode, the Microsoft compiler adds quite a lot of padding and other safety-checking code to your generated code. Filling the stack with 0xCC bytes is one of those checks. That may be confusing your interpretation compared to the generated gcc code.

In release mode, these safety checks are generally turned off, but optimisation is turned on. Optimisation may make your assembly code even harder to follow.

For best results, you might try creating a new configuration starting with release mode, and specifically turning optimisations off.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜