Live data from Hacker News

Subroutine calls in the ancient world, before computers had stacks or heaps

devblogs.microsoft.com

61–70 of 241 posts

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#61
The old IMB 360 use a 'display' where you just chose a register by convention, pointed it at some memory then did your own linked list on call and return.

In fact call was really jump-and-link(?) where you got the return address in a register. Anticipating that if your subroutine took more than a little code you'd just store it temporarily in your allocated 'display'.

No push/pop at all! Unless you wanted to write it that way. You were just on your own.

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#62
post #7

Am I the only one who read it as the ancient world before computers existed had stacks or heaps? English is so weird sometimes...

It's a garden-path sentence: https://en.wikipedia.org/wiki/Garden-path_sentence

One is lead to believe the subject is "the ancient world before computers", as it would be in "the ancient world before computers had gladiators and triremes", but the subject is "computers", and the ancient world is the topic.

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#63
post #59

Earlier quoted context omitted.

Most recursion is about as bad as goto coding methodologies. Personally, I think it should be avoided in modern compiled languages too. Haskell is fun to play around on, but it is horrific to consider using in a stable production environment. =)

It's interesting because I love using Haskell in a stable production environment. In fact I'd hate to use anything else! On the other hand, I almost never use naked recursion in Haskell. It typically do recursion indirectly through more familiar combinators such as for loops.

I like Haskell in many ways as it shifts ones perspectives, but it is just way too easy to leave unpredictable behaviors unconstrained. =)

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#64

The article does not distinguish between recursive and nested subroutine calls. I understand, why the provided example does not allow for recursion, but doesn't it also prevent a nested call to another subroutine? If I remember correctly before FORTRAN90 we already had nested subroutine calls. How did that work? EDIT: I think I get it. The hidden global variables are prefixed with the sub's name. This is pretty waste…

On some systems, arguments were stored at the call site, typically immediately after the call instruction, where the callee could use the return address to find and skip over them. This made calls using constant arguments easy.

   CALL MUL
   2
   3
   BNE 6, FAIL

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#66
post #49

For the kind of programs I write for AVR-8 it seems insane to use the calling conventions of C, so if I write assembly I can sometimes keep the inner loop variables in registers (big register file) and otherwise use the methods he describes. I like “coloring” functions in an app like that, if I know a red and a green function will never be active at once I can reuse locals/parameters for them.

Yes, C's stack usage can be unintuitive when working in a constrained environment, especially when accustomed to the affordances of desktop operating system. I once joined a project where several developers had spent a couple weeks trying to track really hard to pin down bugs in several subsystems they were developing in a microcontroller codebase. They would move things around and the bugs would move. After tracing…

Putting a `out-of-stack? branch` check in every function prolog made an embedded chip programmed in C much nicer to work with. Cost a few instructions which was contentious but definitely a development aid.

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#67
post #47
post #15

I wrote a Forth interpreter for a SUBLEQ machine ( https://github.com/howerj/subleq ), and for a bit-serial machine ( https://github.com/howerj/bit-serial ), both of which do not have a function call stack which is a requirement of Forth. SUBLEQ also does not allow indirect loading and stores as well and requires self-modifying code to do anything non-trivial. The approach I took for both machines was to build a virt…

I was about to talk about subleq, but it's damn difficult even to write a "Hello world".

Yeah, it really is a pain to do anything in it. I got around that by writing in another language instead as soon as possible. You can paper over many issues with a VM, although it will slow things down on such limited computers.

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#68

"There was just one catch: You can't do recursion." You could do tail recursion, because only the return address of the first call would be needed to be stored. `branch_with_link` would be used for the initial call, but the recursive calls would have to be regular branches.

Most recursion is about as bad as goto coding methodologies. Personally, I think it should be avoided in modern compiled languages too. Haskell is fun to play around on, but it is horrific to consider using in a stable production environment. =)

I agree. People love making things clever instead of making things done.

Recursion for iteration is just more complicated iteration. I've never seen a good argument for it in modern programming, it just ends up being the classic backwards rationalization of something people want to believe.

Recursion for traversing a tree is just using the call stack as a stack data structure.

Any balanced tree is never going to exceed 64 levels deep (really 48 on a 48 bit memory addressing cpu) and that could easily be put in a static array that gets used as a stack without recursing. This is easier to limit and debug.

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#69
Raymond Chen has to be one of the few OG legends where the title of the post alone is just like: “Today is Old New Thing day, and that’s just going to be the coolest thing I learn today.” I started salivating before my eyes scanned so far as the domain name.

And as usual, worth reading at least one or two more times.

Fucking. Legend.

Re: Subroutine calls in the ancient world, before computers had stacks or heaps

#70
post #49

For the kind of programs I write for AVR-8 it seems insane to use the calling conventions of C, so if I write assembly I can sometimes keep the inner loop variables in registers (big register file) and otherwise use the methods he describes. I like “coloring” functions in an app like that, if I know a red and a green function will never be active at once I can reuse locals/parameters for them.

Yes, C's stack usage can be unintuitive when working in a constrained environment, especially when accustomed to the affordances of desktop operating system. I once joined a project where several developers had spent a couple weeks trying to track really hard to pin down bugs in several subsystems they were developing in a microcontroller codebase. They would move things around and the bugs would move. After tracing…

Recursion is a bad thing in embedded systems and generally overrated in CS pedagogy.

It can be useful in analyzing algorithms but often in cases like search it is a "second best" answer compared to say, using nondeterminism.

Post reply on HN