0

In lecture we talked about how parameters can be passed in assembler. We distinguished the 3 different mechanism: Memory, Register and Stack. One characterization of those were that: Memory and Register do not support recursion in a sub program, while Stack does.

Why is that?

I guess in case of the register, caller/callee save register are neglected, thus entries of the register are constantly overwritten, even though they might be needed for later (recursion)

But what about memory?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Imago
  • 521
  • 6
  • 29

1 Answers1

3

When you say memory, it seems you're talking about static fixed-size allocations, like global variables. They can't support recursion because they're fixed size with no support for a pointer to the last one in use (no indexed addressing). Registers also have both those limitations.

However, there are other things you can store in memory. e.g. the call stack is part of memory.

You could implement recursion using a "manual" stack data structure in a large fixed-size global array. That would be like the call stack, because it would eventually fault when you cause a Stack Overflow. (In a real system, the call stack has limited size, so an infinitely-recursive function will quickly segfault).


TL:DR: I think you can implement recursion with anything that you can implement a stack data structure on top of, but you can't do that with either registers or a bunch of int globals.


See also a recent answer where I explained what recursion is in asm, on a question about a made-up instruction set which the OP (incorrectly) thought didn't support call / ret.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    Of course, registers can be organized as a stack (as in the XTensa ISA) or even part of the registers in a stack (e.g., SPARC, Itanium) where the other registers might be unused (or hold constants) in the recursive portion. With versioned memory (typically associated with hardware transactional memory but not inherently bound to such an interface), the same memory address could have multiple storage locations. –  Aug 21 '16 at 23:54
  • @PaulA.Clayton: oh good point. x86's x87 FP register stack could be used by a recursive function, too, with a very limited recursion depth. – Peter Cordes Aug 22 '16 at 00:07