if <assignment>:

maney at pobox.com maney at pobox.com
Tue Dec 3 11:42:48 EST 2002


Greg Ewing <see_reply_address at something.invalid> wrote:
> maney at pobox.com wrote:
>> The op-assign (+=, et. al.) were largely present to take advantage
>> of PDP instructions that could do that to most any two operands in
>> one op;
> 
> People keep saying that, but I don't think it's true.
> 
> Didn't the first C implementation pre-date the PDP-11
> one?

>From "The C Programming Language", Ritchie, Johnson, Lesk, Kernighan,
Bell System Tec. Journal, July-August 1978 (there have been numerous
reprintings and/or derived version of this article in more recent
years; this entire BSTJ issue was published by Prentice-Hall as _UNIX
System Readings and Applications_ volume 1, in 1987, which is the only
reason I have a copy):

  "C was originally written for the PDP-11 under UNIX..."

(in fact, C was the successor to B, which was itself written for the
earliest Unix implementation on the PDP-11.  [op. cit. and "The UNIX
Timesharing System", Ritchie and Thompson, same issue of BSTJ])

You are probably thinking of the fact that Unix itself predated C on
the PDP-11, and that the PDP-11 was supported only by the second
version: the first ran on PDP-7 and PDP-9 hardware.

However, the section of TCPL that (briefly) describes the assignment
operators says only (text follows several code examples with brief
textual commentaries):

  "... adds 3 to the element selected from the array p, calculating the
  subscript only once, and, more importantly, requiring it to be
  written out only once.  Compound assignment operators also seem to
  correspond well to the way we think; 'add 3 to x' is said, if not
  written, much more commonly than 'assign x+3 to x'"

I can't find anything that ties the op= form to the PDP-11 instruction
set in the time I'm able to spend on this now.  It's possible that it's
a false memory, though it is certainly likely that the decision to
implement that form would have been made at a time when the PDP-11 was
not only the native platform for C and Unix, but the one the developers
were actively engaged with at the time.

> I think it's more likely that C and the PDP-11 both
> have these sorts of operations for the same reason --
> that they're just useful things to have.

I'm sure that would have to be part of any complete explanation of it!

Actually, most CPUs implement binary operations as 'des op= src'; there
are (or have been) three-address machines that could specify two
operand addresses and an independent destination address, but it's less
common.  What was novel, at least in the realm of minicomputers at that
time, about the PDP-11 was that it allowed both addresses to be
non-registers.  This made the implementation of op= very much simpler,
since the compiler could emit a single instruction, as no shuffling of
operand(s) into and result out of a register was necessary.  So it
seems likely that the PDP-11 instruction set at least influenced the
early evolution of the C language.  It's interesting to wonder if C
would have achieved the same ubiquity as it has, had the PDP-11 not
itself had so large an influence on so many of the CPUs that have come
along since its day... but that would be the topic for not only a
different thread, but a different group!



More information about the Python-list mailing list