Overclock.net banner
1 - 15 of 15 Posts

·
Premium Member
Joined
·
3,789 Posts
Discussion Starter · #1 ·
What's the advantage of storing in stacks rather than directly into memory? I don't know if it's different depending on the language but I'm particularly talking about LC3 / Motorola's HC11.
 

·
Registered
Joined
·
1,012 Posts
Stack is mainly used for function calls where there is a sequence required. The heap is where you store things that don't need to be in a specific order or can be accessed randomly.

But if your talking about memory vs cache we're talking speed that is order of magnitudes faster.
 

·
Premium Member
Joined
·
3,789 Posts
Discussion Starter · #4 ·
Isn't the stack essentially still storing the value to memory? It's just not being stored into specific locations in memory that you define. Rather, it gets pushed onto stack?
 

·
Premium Member
Joined
·
5,282 Posts
Quote:


Originally Posted by Blizzie
View Post

What's the advantage of storing in stacks rather than directly into memory? I don't know if it's different depending on the language but I'm particularly talking about LC3 / Motorola's HC11.

Are you asking about the stack vs the heap? Your question is a bit confusing

Quote:


Originally Posted by psycho12345
View Post

In terms of performance, yeah its VASTLY faster to use the stack which is on the CPU then the memory, which is way far away (relatively).

Wat?
 

·
Premium Member
Joined
·
3,789 Posts
Discussion Starter · #6 ·
Quote:

Originally Posted by rabidgnome229 View Post
Are you asking about the stack vs the heap? Your question is a bit confusing
I was replying to the statement from psycho12345 who said it's faster than memory.. I thought stacks still store in memory? Stacks are on the CPU..??
 

·
Premium Member
Joined
·
5,282 Posts
Yes, the stack is just a region of memory. I have no idea what psycho is talking about. If you are asking about the stack vs the heap, allocating memory on the stack is simpler than dynamic memory allocation, which makes it a bit faster. However, stack space is limited and the difference in speed is not significant enough that you should chose the stack over the heap for that reason.
 

·
Premium Member
Joined
·
3,789 Posts
Discussion Starter · #8 ·
Quote:


Originally Posted by rabidgnome229
View Post

Yes, the stack is just a region of memory. I have no idea what psycho is talking about. If you are asking about the stack vs the heap, allocating memory on the stack is simpler than dynamic memory allocation, which makes it a bit faster. However, stack space is limited and the difference in speed is not significant enough that you should chose the stack over the heap for that reason.

I have no idea what the "heap" is. Maybe because it's not used in LC3 or HC11. But I was thinking.. one situation where stacks would be more useful is when you want to store variables only once. Instead of declaring a space in memory for the data, you can just push it onto the stack and then pop it back of?
 

·
Premium Member
Joined
·
13,477 Posts
Quote:


Originally Posted by Blizzie
View Post

I have no idea what the "heap" is. Maybe because it's not used in LC3 or HC11. But I was thinking.. one situation where stacks would be more useful is when you want to store variables only once. Instead of declaring a space in memory for the data, you can just push it onto the stack and then pop it back of?

These CPUs don't use memory? I find that hard to believe.

Anyway, one other advantage is that the stack is more likely to already be in the CPU's L1 cache, making it much faster to access. Just don't go crazy with it.
 

·
Banned
Joined
·
2,461 Posts
The stack is fixed-preallocated memory and the CPU instructions will typically be optimized to use the stack faster than regular memory. Both the "stack" and "regular memory" are stored in regular memory, but the instructions related to the stack will execute much faster, and the stack has a much higher chance of being cached. The stack was invented because a typical program has a lot of random needs for small amounts of temporary memory (like function calling). You'll notice in a lot of C++ programs variables like "TempInt1, TempInt2, TempTotal". Well with a stack, you don't need to waste time with that stuff. Just push and pop to your hearts content! The stack also lets you test various things with ease, such as call paths, execution profiling, and passed parameters!

http://en.wikipedia.org/wiki/Call_stack
 

·
Registered
Joined
·
821 Posts
In a debugger, step into malloc() or operator new() and you should get a feel right away as to how much slower it is to allocate memory on the heap versus the stack.
Believe me, the heap is wicked slow(er).
 

·
Premium Member
Joined
·
3,789 Posts
Discussion Starter · #13 ·
If anyone else have comments feel free to chip in. My midterm is tomorrow.


+rep to those that helped, or tried to help.
 

·
Banned
Joined
·
2,461 Posts
If you read through processor docs specific to what you are programming for, you can learn a lot about this kind of stuff:

AMD K10 (Phenom) Doc:

http://www.amd.com/us-en/assets/cont...docs/40546.pdf

Stack Optimizations:

Quote:


When saving or restoring registers in function prologues or epilogues, or when passing arguments through the stack, use PUSH or POP instructions to improve performance and to reduce code size.

When deallocating stack space at function exit, use RET imm to improve performance and to reduce code size.


Quote:


In spite of the implicit dependency between several successive PUSH or POP instructions on the stack-pointer (which PUSH and POP modify), special circuitry (the Sideband Stack Optimizer) tracks the value that the stack-pointer assumes, allowing parallel execution of more than one PUSH or POP. This is also true of instructions that reference the stack-pointer, either implicitly or explicitly, including:
  • Near CALL
  • Near RET
  • LEAVE
  • Instructions that specify the stack-pointer as a source register.
  • Instructions that specify the stack-pointer in the addressing mode of a memory operand without an index register.


Quote:


Faster PUSH/POP with the Sideband Stack Optimizer
AMD Family 10h processors have added a sideband stack optimizer (SSO). This special circuitry removes the dependency that arises during chains of PUSH and POP operations on the rSP register and, thereby, improves the efficiency of the PUSH and POP instructions.
The SSO also improves the performance of CALL and RET instructions, among others. (See "Stack Operations" on page 59.)

As you can see, AMD makes a lot of effort to improve stack operations to speed up common execution. You can push or pop up to 3 memory-to-memory values in a single clock cycle, which would take 6 clock cycles and 1 register if you used the mov instruction!
 

·
Premium Member
Joined
·
13,477 Posts
The compiler should be taking care of managing the stack for you. Now if you're writing assembly then you get to do it yourself.
 
1 - 15 of 15 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top