1

This is a pretty basic question related to the implementation of stacks in ARM and other register-based machines. LDM and STM commands can be used to move multiple values between memory and a group of general purpose registers for a stack operation or block copy. The LDM or STM operations don't appear to alter the rules of how we can implement stacks and help reduce the length of code required to perform multiple transfers (e.g., common transfers of multiple register contents at the start and end of functions). However, it's unclear whether the loaded registers behave like a stack after the values are loaded from memory.

I'm just a bit confused as to whether stacks are implemented in external memory, registers, or a combination of the two.

Thanks in adv!

BobT
  • 11
  • 1
  • 1
    Everything on the **ARM** is in memory in regard to the stack. The `LDM` and `STM` are part of the load-store that is a hallmark of RISC. See: [ARM link register and frame pointer](http://stackoverflow.com/questions/15752188/arm-link-register-and-frame-pointer) which have some more info. In particular, the [APCS](http://www.cl.cam.ac.uk/~fms27/teaching/2001-02/arm-project/02-sort/apcs.txt) document might be helpful. While the **ARM** itself doesn't restrict things, compiler conventions do. – artless noise Aug 14 '13 at 14:02

2 Answers2

3

The stack is memory. some processors it is a special internal memory but it is memory, most often it is really nothing more than a stack pointer register that points into system memory and it is up to the programmers (operating system and/or application) to make sure that their stack pointer doesnt point at some memory being used for something else.

The idea is that you have a limited set of registers, even if you have more than enough registers there may be times for example that you want to do recursion or some other such thing that requires you to re-use some registers being used for something else. The stack provides a simple/fast malloc and free basically for temporary storage of things, registers or local variables or return addresses or whatever you need to store temporarily.

Why you would use a stack instead of ram or even registers if you have many, is that with a stack you can as mentioned for example do recursion, you can enter the same code several/hundreds of times and that code for example could have some local variables and you could keep track of all of those individual instances of each local variable, hundreds of copies if need be. Because the way you use the stack is relative to the stack pointer, you dont have a hardcoded address you have a hardcoded reference to the stack pointer, so if you enter the function one time and the stack pointer happens to be at 0x1000 your first variable might then be at 0x1000 and the second at 0x1004, for example, your function may "add" 16 bytes of information to the stack and then call itself, this time when it enters the stack pointer would be at 0xFF0, and your first variable at 0xFF0 and second at 0xFF4, and so on until your stack pointer collides with something else or you are done.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29#Stack_in_registers_or_dedicated_memory – auselen Aug 14 '13 at 07:04
  • @auselen: His first sentence is the answer; you must have skimmed it? – artless noise Aug 14 '13 at 14:00
  • @artlessnoise while it is a very poor question, it asks about ARM and other register-based archs which removes any possible definition like "stack is memory". As it says in wikipedia link for `x87`, it is possible to have limited stack setup over registers. – auselen Aug 15 '13 at 06:28
1

ARM architecture doesn't have any possible stack implementation in registers. ARM has 16 general purpose registers in ARM mode and r13 is also defined as Stack Pointer / SP which gets banked under some situations so you get HW support for handling nested calls. Other than this ARM architecture doesn't provide any additional support for stacks.

For architectures providing stack implementations rather than in memory see Wikipedia article about Stack - Stack in registers or dedicated memory.

auselen
  • 27,577
  • 7
  • 73
  • 114