[TUHS] Memory management in Dennis Ritchie's C Compiler

Doug McIlroy doug at cs.dartmouth.edu
Wed Aug 26 23:24:59 AEST 2020


>     That's always true on the PDP-11 and Vax, no matter what the OS,
>     because the processor architecture (which has pre-increment and
>     post-decrement instructions, but not their counterparts) makes
>     anything but a downward-growing stack unmanageable.

I hadn't heard this urban legend before. A stack is certainly
manageable without auto-increment/decrement (AID) instructions. In
fact Dennis's compiler did not use AID instructions for that purpose.

AID instructions are nice for a LIFO stack, in which a stacked
item disappears as soon as it's used--as do intermediate
results in expression evaluation. But when the stack contains
local variables that are accessed multiple times, the accesses
are offset from the stack pointer. If AID instructions set the
pointer, then the offset of a particular local varies throughout
the code. The compiler can cope with that (I once wrote a
compiler that did so), but a debugger will have a devil of a
time doing a backtrace when the offset of the return address
in each stack frame depends on the location counter.

AID instructions are fine for sequentially accessing arrays, and
in principle Ken's ++ and -- operators can exploit them. Yet
idiomatic C typically wants post-increment and pre-decrement
instructions--the opposite of what DEC offered. Examples:

        char a[N], b[N];
        char *ap = a;
        char *bp = b;
        int i;
        for(i=0; i<N; i++)
                *ap++ = *bp++;

        int a[N], b[N];
        int i = N;
        while(--i >= 0)
                a[i] = b[i];

Doug


More information about the TUHS mailing list