The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] "The programmer must have been carried away ...
@ 2021-06-23 16:01 Angelo Papenhoff
       [not found] ` <F46AB5BC-C48F-4060-AA26-DF802E62B14C@hotmail.co.uk>
  0 siblings, 1 reply; 3+ messages in thread
From: Angelo Papenhoff @ 2021-06-23 16:01 UTC (permalink / raw)
  To: tuhs

... when he declared all the parameters for this procedure"

I'm sure some of you remember this quote expressing John Lions'
puzzlement over the declaration of printf in the UNIX kernel:

printf(fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc)

Note that parameters x2 and following are never referenced by name
in the function body, only by pointer arithmetic from &x1.

The historical reason for this long list of parameters is probably
not well known, so I want to explain it and hope that you will find
it as enjoyable as I did when I came across it some time ago while
studying leftover files of B.

To call a function in B, there's a little dance you have to do:
first push the function address onto the stack, remember the stack
pointer, push all the arguments, then restore the stack pointer and
do the actual call.

In diagrams, this is what happens to the B stack:

push func addr:

+------------+
|            |  <- sp
+------------+
|  func addr |
+------------+


mark (n2):

+------------+
|            |  <- sp
+------------+
|            |  <- old sp (saved on native stack)
+------------+
|  func addr |
+------------+


push arguments:

+------------+
|            |  <- sp
+------------+
|    arg n   |
+------------+
|    ...     |
+------------+
|    arg 0   |
+------------+
|            |  <- old sp (saved on native stack)
+------------+
|  func addr |
+------------+


call (n3):

pop old sp from stack
jump to func addr on stack
set ret addr and old fp
+------------+
|    arg n   |
+------------+
|    ...     |
+------------+
|    arg 0   |
+------------+
|  ret addr  |
+------------+
|   old fp   |  <- sp, fp
+------------+


The callee then sets sp, so has to know how many args and automatics!

+------------+
|            |  <- sp
+------------+
|   auto n   |
+------------+
|    ...     |
+------------+
|   auto 0   |
+------------+
|    arg n   |
+------------+
|    ...     |
+------------+
|    arg 0   |
+------------+
|  ret addr  |
+------------+
|   old fp   |  <- fp
+------------+


So because the arguments to a functions were grouped together with
the automatics and not below the ret addr and saved fp, there has
to be space on the stack lest they clash. This of course means that
this printf in B would probably break if called with more arguments
than it expects.
So there you have it. Just a line of B code that wasn't updated to C.


Cheers,
aap

^ permalink raw reply	[flat|nested] 3+ messages in thread
* Re: [TUHS] "The programmer must have been carried away ...
@ 2021-06-25 22:30 Paul Ruizendaal via TUHS
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Ruizendaal via TUHS @ 2021-06-25 22:30 UTC (permalink / raw)
  To: TUHS main list

> So there you have it. Just a line of B code that wasn't updated to C.
> 
> Cheers,
> aap

I love posts like this, thank you! “Sherlock Holmes and the mysterious case of the excessive parameter list"


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-25 23:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-23 16:01 [TUHS] "The programmer must have been carried away Angelo Papenhoff
     [not found] ` <F46AB5BC-C48F-4060-AA26-DF802E62B14C@hotmail.co.uk>
2021-06-23 20:08   ` [TUHS] [EXT] " Angelo Papenhoff
2021-06-25 22:30 [TUHS] " Paul Ruizendaal via TUHS

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).