在汇编中堆叠与在c中的堆栈?

时间:2021-03-31 03:18:45

I've been wondering whats the difference between stack in C and stack in assembler for processors like RISC or ARM?

我一直想知道C中的堆栈和RISC或ARM等处理器的汇编器堆栈之间的区别是什么?

Proffesor said, be cautious, stack is different than stack you've learned about on other subject(Algorithms and Structures, where we learn about C)

Proffesor说,要谨慎,堆栈不同于你在其他主题上学到的堆栈(算法和结构,我们学习C的地方)

As far as I can recall, both are just data saved in memory, both function on LastInFirstOut scheme, both need to be cleaned up after usage.

据我所知,两者都只是保存在内存中的数据,两者都在LastInFirstOut方案上运行,两者都需要在使用后进行清理。

I mean, they cant be the same because they are in two different "worlds" but am I missing something important that differs them? Maybe thats it, but its been bugging me ever since.

我的意思是,他们不能相同,因为他们处于两个不同的“世界”但是我错过了一些重要的东西吗?也许就是这样,但从那时起它就一直困扰着我。

Thank you

3 个解决方案

#1


9  

The stacks are exactly the same. One can write a program mixed assembly / C and they use the same stack.

堆栈完全相同。可以编写程序混合程序集/ C,它们使用相同的堆栈。

The C compiler uses some conventions on how to use the stack : a well-formed stack frame is filled in at each function entry ; and cleaned-up at function leaving. There are compiler directives specific for altering the stack management. For example : gcc stack checking

C编译器使用一些关于如何使用堆栈的约定:在每个函数入口处填充格式良好的堆栈帧;并在功能离开时清理。有一些特定于改变堆栈管理的编译器指令。例如:gcc stack checking

Some references on the web : google : c stack frame

网上的一些参考文献:google:c stack frame

In Assembly, the stack has to be managed entirely by the programmer. It is a good practise to have rules on how to manage the stack (and mimicking C rules for example)

在Assembly中,堆栈必须完全由程序员管理。有关如何管理堆栈的规则(例如模仿C规则)是一个很好的做法

The stack management instructions are also quite processor dependant (instructions like push and pop on x86, or stmia / ldmfd on ARM. Similarly, some processors have dedicated registers for stack pointer (esp on x86), for some other it is only conventional (r13 on ARM7.)

堆栈管理指令也非常依赖于处理器(如x86上的push和pop,或ARM上的stmia / ldmfd等指令。类似地,某些处理器具有用于堆栈指针的专用寄存器(尤其是x86),对于其他一些处理器,它只是常规的(r13)在ARM7上。)

A good way to learn on stack management is to use a debugger and do some backtracing to see the frame contents.

学习堆栈管理的一个好方法是使用调试器并进行一些回溯以查看帧内容。

For a good understanding of the x86 stack at assembly level, I'd recommend this Wikipedia article and this one for stack frames

为了更好地理解汇编级别的x86堆栈,我推荐这篇*文章和这篇文章用于堆栈框架

#2


2  

I have seen compilers which use an overlay model rather than a stack model for their automatic variables. While the language presents the allocation and deallocation of automatic variables as a stack, the underlying implementation does not need to be so.

我见过编译器使用覆盖模型而不是堆栈模型用于自动变量。虽然语言将自动变量的分配和释放表示为堆栈,但底层实现不一定如此。

On some compilers the C stack exists but is separate from the hardware stack.

在某些编译器上,C堆栈存在,但与硬件堆栈是分开的。

Then there are concepts like register-windows.

然后是像register-windows这样的概念。

The list goes on, but I couldn't guarantee that any of these are what your professor had in mind, or even that I'm on the right track. There's only one person who can answer that reliably.

这个名单还在继续,但我不能保证这些都是你的教授想到的,甚至是我走在正确的轨道上。只有一个人能够可靠地回答这个问题。

Most of these variations are broadly conceptually consistent with stacks, but the implementation details are something you do need to be aware of if you're working with both languages.

大多数这些变体在概念上与堆栈大致相同,但如果您使用这两种语言,则需要注意实现细节。

#3


1  

One thing that's different about the conceptual stack vs. an x86 stack (and possibly other architectures) is the direction the stack grows. It's common to teach the stack as growing "up" when (depending on architecture) it may actually grow down in memory.

关于概念堆栈与x86堆栈(以及可能的其他架构)不同的一点是堆栈增长的方向。将堆栈教导为“向上”时(通常取决于架构)它可能实际上在内存中增长是很常见的。

#1


9  

The stacks are exactly the same. One can write a program mixed assembly / C and they use the same stack.

堆栈完全相同。可以编写程序混合程序集/ C,它们使用相同的堆栈。

The C compiler uses some conventions on how to use the stack : a well-formed stack frame is filled in at each function entry ; and cleaned-up at function leaving. There are compiler directives specific for altering the stack management. For example : gcc stack checking

C编译器使用一些关于如何使用堆栈的约定:在每个函数入口处填充格式良好的堆栈帧;并在功能离开时清理。有一些特定于改变堆栈管理的编译器指令。例如:gcc stack checking

Some references on the web : google : c stack frame

网上的一些参考文献:google:c stack frame

In Assembly, the stack has to be managed entirely by the programmer. It is a good practise to have rules on how to manage the stack (and mimicking C rules for example)

在Assembly中,堆栈必须完全由程序员管理。有关如何管理堆栈的规则(例如模仿C规则)是一个很好的做法

The stack management instructions are also quite processor dependant (instructions like push and pop on x86, or stmia / ldmfd on ARM. Similarly, some processors have dedicated registers for stack pointer (esp on x86), for some other it is only conventional (r13 on ARM7.)

堆栈管理指令也非常依赖于处理器(如x86上的push和pop,或ARM上的stmia / ldmfd等指令。类似地,某些处理器具有用于堆栈指针的专用寄存器(尤其是x86),对于其他一些处理器,它只是常规的(r13)在ARM7上。)

A good way to learn on stack management is to use a debugger and do some backtracing to see the frame contents.

学习堆栈管理的一个好方法是使用调试器并进行一些回溯以查看帧内容。

For a good understanding of the x86 stack at assembly level, I'd recommend this Wikipedia article and this one for stack frames

为了更好地理解汇编级别的x86堆栈,我推荐这篇*文章和这篇文章用于堆栈框架

#2


2  

I have seen compilers which use an overlay model rather than a stack model for their automatic variables. While the language presents the allocation and deallocation of automatic variables as a stack, the underlying implementation does not need to be so.

我见过编译器使用覆盖模型而不是堆栈模型用于自动变量。虽然语言将自动变量的分配和释放表示为堆栈,但底层实现不一定如此。

On some compilers the C stack exists but is separate from the hardware stack.

在某些编译器上,C堆栈存在,但与硬件堆栈是分开的。

Then there are concepts like register-windows.

然后是像register-windows这样的概念。

The list goes on, but I couldn't guarantee that any of these are what your professor had in mind, or even that I'm on the right track. There's only one person who can answer that reliably.

这个名单还在继续,但我不能保证这些都是你的教授想到的,甚至是我走在正确的轨道上。只有一个人能够可靠地回答这个问题。

Most of these variations are broadly conceptually consistent with stacks, but the implementation details are something you do need to be aware of if you're working with both languages.

大多数这些变体在概念上与堆栈大致相同,但如果您使用这两种语言,则需要注意实现细节。

#3


1  

One thing that's different about the conceptual stack vs. an x86 stack (and possibly other architectures) is the direction the stack grows. It's common to teach the stack as growing "up" when (depending on architecture) it may actually grow down in memory.

关于概念堆栈与x86堆栈(以及可能的其他架构)不同的一点是堆栈增长的方向。将堆栈教导为“向上”时(通常取决于架构)它可能实际上在内存中增长是很常见的。