views:

434

answers:

3

Hello, There has been one question here which talked about stack growth direction. To which Micahel Burr had replied saying in ARM processors stack growth direction can be configured - i.e. either descending(normal behaviour) stack grows towards zero address(lower address) in memory or descending,i.e. stack grows towards higher address in memory.

http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems/664779#664779

My question is in ARM processors how can i make stack grow in ascending direction?

How do i confiure stack as ascending as by default it is descending? Any register bit set/reset, etc..

Thank You. -Ajit.

+6  A: 

Well, the ARM processors don't maintain a stack directly-- but they do have instructions that are designed with that in mind: LDM and STM. So if you use STMDB at the start of a function and LDMIA at the end, you effectively have a full+descending stack: the assemblers I remember using allowed you to write "STMFD" and "LDMFD" as aliases. (A "full" stack is one where the stack pointer points to the latest word on the stack, as opposed to the next location to use)

So it's not something you can simply reconfigure at runtime: although if you were writing your own operating system with its own call convention, you could choose to use an ascending stack. Similarly, you could also choose not to use R13 as the stack pointer- that's just part of the calling convention too. This choice effectively gets embedded into the implementation of every function that uses the stack.

araqnid
Good answer, except that Thumb depends on r13 being the stack pointer.
Mads Elvheim
Ah, I did use an ARM7TDMI for a bit but never used the Thumb mode.
araqnid
A: 

Hmmm thumb/thumb2 might limit you to push/pop, and with thumb2 only ARMs out there I dont know that we can generically say you can go both ways. Traditional arm instructions, yes you can ldmia or ldmdb (increment after or decrement before) and stmdb and stmia. How do you make a C compiler for example climb up in addresses instead of down autmatically? Dont know.

It is like big endian on ARM, just because you can you probably dont want to because of the headaches it brings along with it.

dwelch
A: 

You have the __user_initial_stackheap() function which helps you change the SP using Stack-Start,Stack-End & heap relocation using Heap-Start,Heap-End. This function can be used during the time of the initialization since the ARM would use this to redirect Stack and Heap.

Also, you have option to use a Single-region or two-memory model[depending on your requirement]. I have used this API when I was writing UseCases which were using ARM926EJ-S.

This document was of help during my development and might be helpful to you as well.

Hope this helps.

-hjsblogger

hjsblogger