Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack in assembly vs stack in c?

Tags:

c

assembly

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

like image 548
lucija fabijanic Avatar asked Aug 12 '13 09:08

lucija fabijanic


People also ask

What is stack in assembly?

A stack is an array-like data structure in the memory in which data can be stored and removed from a location called the 'top' of the stack. The data that needs to be stored is 'pushed' into the stack and data to be retrieved is 'popped' out from the stack.

Why do we use the stack in assembly?

The stack is mostly to keep calls/returns in order, also an interrupt (which needs to save the state of the CPU) would push existing values in registers it will use and pop them before returning.

What is stack 100h in assembly language?

STACK 100h : is a segment directive which defines 100h words as program STACK. The linker sets the values of SS and SP. . DATA : is a segment directive, followed by one or more data allocation directives to define the variable and constant used by program. Note: The data segment will be set by program.

What is a stack pointer in assembly?

A stack pointer is a small register that stores the memory address of the last data element added to the stack or, in some cases, the first available address in the stack.


2 Answers

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

like image 72
d-stroyer Avatar answered Sep 29 '22 04:09

d-stroyer


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.

like image 26
sh1 Avatar answered Sep 29 '22 05:09

sh1