10

I've been wondering whats the difference between stack in C and stack in assembler for processors like RISC or 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)

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.

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

4

3 回答 3

9

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

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

Some references on the web : 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)

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.)

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

于 2013-08-12T09:28:09.313 回答
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.

Then there are concepts like 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.

于 2013-08-12T10:50:18.197 回答
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.

于 2013-08-12T11:37:11.193 回答