[net.sources] Stack Use on PDP-11s

jfw (01/22/83)

First, my humblest self abasements for submitting the wrong version the
first time.  Here is the "real" version, which I have hopes of accuracy for.
If you find fault with this version, please tell me || the net.
Once again, I am sorry for my original mistake and for my lack of
knowledge for the proper correction thereof*.  Those who will wish to flame
at me may instead take comfort in the knowledge that I am an active
participant in net.suicide...
-----
* For those others who currently don't know, /usr/spool/news is the
LOCAL copy of an article.  Check /usr/spool/uucp/MANGLED... for the
*real* outgoing version...live and learn.
----------------------------------------------------------------
.nr LL 6.5
.TH STACK 5 PDP-11
.SH NAME
stack, C subroutine discipline
.SH DESCRIPTION
A C subroutine stack frame looks like the following:
.nf
.in +5
.ta 8 16 24 32 40 48 56 64

------------------
|...nth argument |		push arguments in reverse order
------------------
|second argument |
------------------
| first argument |
------------------		JSR PC,*$_FOO
| return address |
------------------		JSR R5,CSV
|  old R5 value  | <----- 
------------------	|
|	r4	 |	|
------------------	|
|	r3	 |	|
------------------	|
|	r2	 |	|
------------------	|
| first local var|	| This is the top of the stack
------------------	| when the called routine "starts"
|     routine	 |	|
|    allocates	 |	|
|     storage	 |	|	SUB $n,SP
|    temporary	 |	|
------------------	|
| push arguments |	|
| of next routine|	|
------------------	|	JSR PC,*$_BAR
| return address |	|
------------------	|	JSR R5,CSV
| old R5 value---+-------
------------------	^
| r4/43/r2/...	 |	|
------------------
| and so on..... |

.fi
.in -5
Functions leave their return value in R0 (floating functions return it in FR0).
"long" functions leave return values in R1/R0; functions returning structures
leave a pointer to bss storage (one chunk of which is allocated for each such
routine) in R0, and the caller will copy from that bss storage to the
local destination.

Local variables are allocated in such a way that they are referred to
as "-N(R5)", arguments are referred to as "+N(R5)"; arguments start at 4(R5),
the first integer local declared will be at -10[8](R5).  SP always
points to the next available stack word.  If the function has no locals,
and calls no functions, it will allocate no stack and the word labelled
"first local var" will be unused.

It is important to note that routines know how many arguments they pass to a
function, and will adjust the stack accordingly after a function returns.

.SH NOTE:
Newer systems implement the c-save by JSR R0,CSAV, which is a more
bullet-proof version of the old call sequence.  The stack winds up
looking the same.
.SH AUTHOR
John F. Woods, MIT Concouse Computer Center