yawnmoth said:
dsend(to, from, count)
char *to, *from;
int count;
{
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n 0);
}
}
If count % 8 is 7, the switch statement would skip the 'do' keyword
and the open bracket used in case 0, would go down the rest of the
cases, and, upon getting to case 1, would find a close bracket without
any corresponding open bracket, yielding a syntax error.
No. Syntax errors are a concept that is only relevant at the time of
translation. By the time the program has been translated and (if
necessary) linked, there is no way to get a syntax error.
The translation will result in some way of expressing the above in a
different language. For example, making up some (not terribly efficient)
assembly language syntax, and starting at the switch:
MOV R7, TO ; save the destination pointer in a register
MOV R1, COUNT ; save the count in a register
MOV R2, N ; save n in a register
MOV R3, R1 ; copy count to a temporary place
DIV R3, 8, R4 ; divide R3 by 8, storing the remainder in R4
C0:
CMP R4, 0 ; is count % 8 == 0?
JNZ C7 ; no
MOV R5, FROM ; yes, so save the source pointer in a register
MOV R6, *R5 ; deref source ptr
MOV *R7, R6 ; copy fetched value to destination
C7:
CMP R4, 7 ; is count % 8 == 7?
JNZ C6 ; no
MOV R5, FROM ; yes, so save the source pointer in a register
MOV R6, *R5 ; deref source ptr
MOV *R7, R6 ; copy fetched value to destination
C6:
; etc (cases 6, 5, 4, 3, 2...)
C1:
MOV R5, FROM ; save source ptr in register
MOV R6, *R5 ; deref source ptr
MOV *R7, R6 ; copy fetched value to destination
DEC R2 ; --n
CMP R2, 0 ; now decide whether to jump back to case 0
JNZ C0
So the mystery lies only in the C source, where the above is probably only
legal because nobody got around to forbidding it, but it is nevertheless
legal. As you can see, it isn't quite so mysterious, once you've worked
through how it would be translated into something a little more - um -
linear.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999