3 ms·
Before COUs supported stacks, quite a few compilers produced self-modifying code. Instead of using a stack, many CPUs stored the address to return to in a spec
by Someone 8y ago
Before COUs supported stacks, quite a few compilers produced self-modifying code.
Instead of using a stack, many CPUs stored the address to return to in a specific register. If a function wanted to call other functions and return afterwards, it had to store that return address somewhere. Popular solutions where “directly before the start of the function” and “in the jump instruction at the end of the function”. The former is easier for the compiler writer, the latter leads to faster code (the return becomes a simple unconditional jump. With the other approach, you have to load the return address and then jump to it)
And yes, you can’t have reentrant code or even recursion that way. That’s one reason early COBOL and early FORTRAN didn’t support recursion.
- benj111 8y agoThanks for that. Presumably you could have tail call recursion. Although if this is pre stacks, tail call detection is probably asking a bit much.
- Someone 8y agoYou could, but I’m not sure the idea had been discovered yet. https://en.wikipedia.org/wiki/Tail_call#History https://en.wikipedia.org/wiki/Tail_call#History gives 1977 as its first mention (Steele’s ”Debunking the 'Expensive Procedure Call' Myth“ https://dspace.mit.edu/handle/1721.1/5753#files-area https://dspace.mit.edu/handle/1721.1/5753#files-area) Also, many tail-calling functions eventually have to return.