Undefined behaviour
Undefined behaviour
Posted Sep 7, 2018 18:41 UTC (Fri) by Wol (subscriber, #4433)In reply to: Undefined behaviour by dvdeug
Parent article: C considered dangerous
Given that C has always been considered a "super-assembly" language, and most of the undefined behaviours were, I believe, down to the fact that the original writers of C knew that different hardware behaves in different ways, I would have thought that the C spec should have said "When behaviours are undefined because different hardware behaves differently, it is down to the compiler writers to provide a definition for their implementation".
Bit like the Z80 scenario where the right shift instruction behaved as expected (integer divide by two) but thanks to a bug the left shift did a "double and increment" operation. If the C language didn't specify the behaviour of a left shift thanks to this, then it should have placed the onus on the compiler writers to specify it, not just leaving the behaviour in doubt.
(And this silicon bug really did exist, and quite a few assembler programs took advantage of it, I believe ... left-shift-and-increment it got known as :-)
Cheers,
Wol
Posted Sep 9, 2018 15:40 UTC (Sun)
by jem (subscriber, #24231)
[Link] (3 responses)
The Z-80 did have a working shift left instruction (SLA), in addition to two working shift right instructions (SRA and SRL). The "bug" you are referring to is an undocumented opcode which, by chance, behaved like you describe.
Posted Sep 10, 2018 18:14 UTC (Mon)
by Wol (subscriber, #4433)
[Link] (2 responses)
You mention SRA and SRL. Therefore you should have a matching SLA and SLL. The story as I heard it was that some programmers used that exact logic, worked out what the opcode for SLL should be, and discovered what I called "shift left and increment".
In other words, a perusal of the documentation led to people deducing a "missing" opcode, and when they tried it, it ALMOST did what was expected.
Cheers,
Posted Sep 10, 2018 19:17 UTC (Mon)
by mgb (guest, #3226)
[Link]
How would you have expected the functionality of these hypothetical SLA and SLL to have differed?
Posted Sep 10, 2018 22:28 UTC (Mon)
by anselm (subscriber, #2796)
[Link]
The main difference between an arithmetic shift to the right and a logical shift to the right is that the logical shift will make the MSB zero while the arithmetic shift will keep the MSB (the sign of a signed number) whatever it was before the shift. This doesn't make a huge amount of sense when shifting to the left, because the LSB in a signed number isn't special, so processor makers often didn't bother with arithmetic shifts to the left (especially with the simpler late-20th-century-vintage CPUs).
Posted Sep 10, 2018 15:08 UTC (Mon)
by anselm (subscriber, #2796)
[Link]
The C standard, at least back when I was still programming in C, did in fact make that distinction. When dealing with a C implementation, you distinguish between behaviour that is specified in the standard, “implementation-defined behaviour” that is not specified in the standard but requires a consistent (and documented) approach by the implementation, and “undefined behaviour”, where anything can happen, including unpredictable and inconsistent effects.
Undefined behaviour
Undefined behaviour
Wol
Undefined behaviour
Undefined behaviour
Undefined behaviour
I would have thought that the C spec should have said "When behaviours are undefined because different hardware behaves differently, it is down to the compiler writers to provide a definition for their implementation".
