3 ms·
TIL the "switch" keyword in C was originally a PDP-11 assembly helper subroutine in the Unix system library (this was written before the invention of C). It exp
by segfaultbuserr 6y ago
TIL the "switch" keyword in C was originally a PDP-11 assembly helper subroutine in the Unix system library (this was written before the invention of C). It explains everything about C's behavior.
NAME switch -- switch on value
SYNOPSIS (switch value in r0)
jsr r5,switch;
swtab
(not—found return)
swtab: val1; lab1;
valn; labn
..; 0
DESCRIPTION switch compares the value of r0 against each
of the vali; if a match is found, control is
transferred to the corresponding lab. (after
popping the stack once). If no match has been
found by the time a null labi occurs, switch
returns.
- vincent-manis 6y agoAnd I think that this switch subroutine mirrors one way of compiling the BCPL switchon statement (on which C's switch statement was explicitly modelled), though the BCPL compiler I used in the mid-1970s was smarter if the cases formed a compact integer sequence. C was based upon B, which was based upon BCPL. Clearly, the Bell Labs crew were trying to give themselves some BCPL goodness, even in assembly language.
- pjmlp 6y agoAnd BCPL was desiged as very thin language just to be good enough to write the CPL compiler, it was never planned to be used to write full applications. Bootstrap CPL.
- contingencies 6y agoBCPL (1967), B (1969), PDP-11 (1970), C (1972).