The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: Angelo Papenhoff <aap@papnet.eu>
To: tuhs@minnie.tuhs.org
Subject: [TUHS] "The programmer must have been carried away ...
Date: Wed, 23 Jun 2021 18:01:02 +0200	[thread overview]
Message-ID: <YNNavhVcD/nd8ahr@indra.papnet.eu> (raw)

... 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

             reply	other threads:[~2021-06-23 16:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-23 16:01 Angelo Papenhoff [this message]
     [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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YNNavhVcD/nd8ahr@indra.papnet.eu \
    --to=aap@papnet.eu \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).