有人能解释一下这几条装配线吗?

发布于 2024-12-08 05:18:40 字数 1029 浏览 0 评论 0原文

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;

有几件事我不明白。

(1) G++ 的堆栈对齐为 16 字节,而在 Visual Studio 中执行此操作为 228 字节?

(2) 在Windows上这样做,堆栈是向上增长还是向下增长?我很困惑。我知道堆栈应该是什么样子

[Parameter n          ]
...
[Parameter 2          ]
[Parameter 1          ]
[Return Address       ]   0x002CF744
[Previous EBP         ]   0x002CF740  (current ebp)
[Local Variables      ]   

那么最低地址是向下的吗?

(3) 当我们将变量a压入堆栈时,它是ebp - 8。为什么是8个字节呢? (4) 同样,为什么是 int b ebp - 14 ?

有人可以向我解释一下吗? (分别为-4、-8)

使用GDB,偏移量对我来说更有意义。

谢谢。

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          ]
[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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-12-15 05:18:40

调试模式下编译时,Microsoft 编译器会向生成的代码添加大量填充和其他安全检查代码。用 0xCC 字节填充堆栈是这些检查之一。与生成的 gcc 代码相比,这可能会让您的解释感到困惑。

发布模式下,这些安全检查通常是关闭的,但优化是打开的。优化可能会使您的汇编代码更难理解。

为了获得最佳结果,您可以尝试从发布模式开始创建新配置,并特别关闭优化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文