2

Answers to this question about compiler efficiency for 8-bit CPUs seem to imply that allowing for recursion makes the C language inefficient on these architectures. I don't understand how recursive function calling (of the same function) is different from just repeated function calling of various functions.

I would like to understand why this is so (or why seemingly learned people think it is). I could guess that maybe these architectures just don't have the stack-space, or perhaps the push/pop is inefficient - but these are just guesses.

Rob
  • 14,746
  • 28
  • 47
  • 65
Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • maybe the reason is that stack space is too small. recursive call is usually done far more times than a repeated function invocation – mangusta Jun 01 '20 at 00:28
  • reads to me that C is not well adapted to some of the funky 8bit cpus , they have all sorts of clever tricks that are hard ot expose in C – pm100 Jun 01 '20 at 00:29

1 Answers1

6

Because to efficiently implement the C stack, you need the ability to efficiently load and store to arbitrary offsets within the current frame. For example, the 8086 processor provided the indexed and based address modes, that allowed loading a stack variable within a single instruction. With the 6502, you can only do this with the X or Y register, and since those are the only general purpose registers, reserving one for a data stack pointer is extremely costly. The Z80 can do this with its IX or IY registers, but not the stack pointer register. However, indexed load instructions on the Z80 take a long time to execute, so it is still costly, along with the fact you either reserve a second register for the stack pointer, or have to load the stack pointer from the SP register any time you want to access variables.

By comparison, if recursive calls are not supported, then a second instance of the function can not start inside a call whilst an existing is still in progress. This means only a single set of variables is needed at a time and you can just allocate each function its own static piece of memory to use for variables. Since the memory has a fixed location, you can then use fixed address loads. Some implementations of fortran used this approach.

user1937198
  • 4,987
  • 4
  • 20
  • 31
  • 2
    In particular this is not inherent to the cpu being 8-bit. Rather it's a matter of the existing 8-bit cpus having utterly awful ISA design. – R.. GitHub STOP HELPING ICE Jun 01 '20 at 03:05
  • 1
    I wouldn't describe the ISA as awful, just limited. – user1937198 Jun 01 '20 at 09:08
  • Just to add to this answer. Realistically on the 6502 only the post indirect addressing mode can be used to manipulate local variables inside a recursive function. So e.g. LDA (pointer),Y and STA (pointer),Y. That limits the flexibility because the Y register would need to be constantly re-loaded to access different local variables off the stack frame - and this would also tie the Y register up for using it in loops. – Guillermo Phillips Jun 02 '20 at 15:17