C
Chuck F.
Keith said:Chuck F. said:Jordan Abel wrote: [...]Speaking of which, I think it would have been better for
them to take the FILE* argument first, to match fprintf.
However, the reasons for this inconsistency lie forgotten in
the depths of history. (It strikes me as especially odd
given that UNIX functions that take a file descriptor number
do take it first. Anyone here who was at Bell Labs around
that time have any comment on this? I forget, does DMR read
this newsgroup?)
If you think of the assembly language historically needed to
implement those functions, all becomes clear (especially if
you have done such implementations). fprintf needs the FILE
first to allow the variadic mechanisms to functions. For the
others, remember that the usual stack passing mechanism is
last argument pushed first. Thus, for fputc(c, f) the stack
at function entry will look like:
stack-mark f c <top of stack>
This allows the routine to examine c and decide on such things
as 'does this get printed', or 'should I convert into a tab
sequence' before worrying about the actual destination. This
preliminary work can be done without disturbing the stack-mark
or f.
In this case the gain is fairly small. However when the
parameters include such things as fieldsize or precision, the
gains can be significant.
You seem to be assuming that the generated code can't easily
access items lower on the stack before accessing items higher on
the stack. In all the assembly languages I'm familiar with, it's
easy to access any item on the stack (within reason) by using a
stack-pointer-plus-offset addressing mode. All parameters are
available simultaneously and equally easily. (Some CPUs might
limit the offset to some small value, but plenty for a couple of
parameters.)
Historically, I know this is true for the PDP-11. What about
the PDP-7 (isn't that the first system where C was implemented)?
Did some CPUs actually require upper stack items to be popped
before lower stack items could be accessed?
In effect, yes. Take the 8080 as an example. I had routines to
address things offset from TOS, but they were relatively complex
and slow. However manipulating TOS, and possibly NOS, was fairly
easy. Using these methods it was possible to write pure code.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>