Windows 汇编堆和堆栈?

发布于 2024-08-24 12:22:23 字数 251 浏览 5 评论 0原文

操作系统:Windows 7 32位

所以像c++一样有一个堆和一个堆栈。但我最近开始进行一些汇编学习,但没有看到任何类似的东西,只有一个堆栈,但它看起来就像纯粹的内存。 那么堆和栈的实现是特定于 C++ 和其他语言的吗?或者您仍然在汇编中分配堆和堆栈吗?当启动一个可执行文件时,Windows 在为进程分配内存方面做了什么? 进程如何知道堆栈大小需要有多大?

编辑

:也许有人可以提供有关 CPU/OS 如何处理进程的堆和堆栈内存的链接

OS: Windows 7 32bit

So in like c++ one has a heap and a stack. But i've been starting on some assembly learning lately and haven't seen anything of the sort, only a stack but it just looks like pure memory.
So is heap and stack implementation specific for c++ and other languages? Or do you still get allocated a heap and stack in assembly? When starting a executable what does windows do in terms of allocating memory for the process?
And how does a process know how big the stack size needs to be?

Whats the go

EDIT: Perhaps someone could provider a link on how heap and stack memory is handled for a process by the CPU/OS

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

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

发布评论

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

评论(2

£冰雨忧蓝° 2024-08-31 12:22:23

我的大部分知识都不是特定于 Windows 的,所以请耐心等待:

堆和堆栈指的是内存中的不同区域(但我们仍然在每种情况下讨论主内存)。这并不是任何语言所特有的。堆位于内存低地址并向上增长;堆栈位于内存高地址并向下增长。这是为了防止它们重叠(这将是非常糟糕的)。

在 32 位架构上,EBP 和 ESP 寄存器跟踪当前堆栈帧。 EBP是基指针——它指向当前堆栈帧的高地址。 ESP是堆栈指针,它指向当前堆栈帧的低地址。

请记住,释放/分配的堆和堆栈内存的概念主要与应用程序级别相关。在机器级别,所有内存看起来都是一样的 - 由程序员(或编译器)来跟踪正在使用哪些内存段。

堆栈由以下组合管理:调用函数的指令以及对 EBP 和 ESP 的显式修改。任何低于 ESP 的都被视为已释放;因此,要释放内存,您只需添加到 ESP 即可。

堆是通过内存分配方法来管理的;可以在此处找到文档。我不确定 Winows 的具体情况,但一般来说,会有一些内存管理器负责确保没有内存块分配给多个应用程序。

Most of my knowledge is not Windows-specific, so bear with me:

The heap and the stack refer to different areas in memory (but we are still talking about main memory in each case). This is not particular to any language. The heap lives in the low memory addresses and grows upwards; the stack lives in the high memory addresses and grows downwards. This is to keep them from overlapping (which would be very bad).

On a 32-bit architecture, the EBP and ESP registers keep track of the current stack frame. EBP is the base pointer - this points to the high address of the current stack frame. ESP is the stack pointer and it points to the low address of the current stack frame.

Remember that the concept of free / allocated heap and stack memory is mostly relevant at the application level. At the machine level, all memory looks the same - it is up to the programmer (or compiler) to keep track of which memory segments are in use.

The stack is managed by a combination of: instructions that call functions, and explicit modifications to EBP and ESP. Anything below ESP is considered freed; so to free memory you can just add to ESP.

The heap is managed by memory allocation methods; documentation can be found here. I am not sure about the particulars of Winows, but in general there will be some memory manager that has the responsibility of making sure no block of memory is allocated to more than one application.

千里故人稀 2024-08-31 12:22:23

堆栈主要由CPU维护(PUSH/POP/CALL/RET命令);堆纯粹是操作系统/运行时库功能。因此,堆栈访问在汇编中是很自然的。对于堆访问,您只需从汇编代码(HeapAlloc/HeapFree,或其他库)调用相关 API。与堆栈不同,汇编语言中没有用于堆内存管理的低级原语。

您不必担心 Windows 上的堆栈大小。随着你使用的越来越多,它会透明地增长。用低级术语来说,Windows 在堆栈底部下方设置一个保护内存页面(假设堆栈向下增长)。当堆栈到达保护页面时,CPU 中会生成访问冲突异常。 Windows 内核会捕获它,注意到这种情况并增加堆栈。

The stack is maintained mostly by the CPU (PUSH/POP/CALL/RET commands); the heap is purely an OS/run-time library feature. Therefore stack access is natural in assembly. For heap access you just call the relevant APIs from your assembly code (HeapAlloc/HeapFree, or from some other library). Unlike stack, there are no low-level primitives in the assembly language for heap memory management.

You don't have to worry about stack size on Windows. As you use up more and more of it, it will grow transparently. In low-level terms, Windows sets up a guard memory page below the stack bottom (assuming stack grows down). When your stack reaches the guard page, an access violation exception is generated in the CPU. Windows kernel would catch it, notice the situation and grow the stack.

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