3B1 c compiler *incompatibility* with some sources

Mike Levin mml at magnus.UUCP
Sun Jul 3 15:43:06 AEST 1988


	This concerns a small, but rather significant, difference between
the C compiler on the 3B1, versus some of the rest of the world.  It is that
the 'char' declaration by default on the 3B1 is 'signed', which is not what
some other program(mer)s assume.  I first ran into it in porting over a 6502
cross-assembler.  The assembler used 'char' types to hold the object code it
generated.  But, what would happen is-

	a piece of object code (on the printout, or the output file) which had
high-bit clear would print as:
	02 0A 7F 53 22 10 etc.
	the instant anything had high bit set, it would do the following:
	02 0A 7F 53 22 10 FFFFFF83 7D FFFFFFC8 etc.

	This is due to the signed nature.  The typical way that the char data
was handed to the output file was with something like:
	printf("%02X",(int) CHAR); (this is of course an oversimplification).

	What happens, of course, is that the char being negative 'sign-extends'
when it is cast, so an 80 becomes FFFFFF80 (which is mathamatically equal),
but which sure screws up something which assumes the data was one byte, so it
will return one byte.
	I have run into this again in porting a few programs that deal with
binary data files.  The functions include CRC'ing the data, which was a total
disaster, to re-arranging, recognizing, and decoding data.

	We fixed the problem by simply changing all the (implicitly signed)

	'char'

declarations in our code to-

	'unsigned char'

and our problems evaporated.  BEWARE, however, that somebody, someplace, may
be aware the char's are naturally signed on this machine, who may be relying
on that fact.  DON'T BE INDESCRIMINATE, look at the code and see what it is
doing.  We managed to (successfully) port 3 rather involved programs from a
Ridge 3200 RISC-based super-mini to a 3B1 in about a day-and-a-half, changing
nothing but the char declarations.  Other than that, they appear to have no
differences.

		G O O D    L U C K ! ! ! ! ! !

		&

		Happy Hacking.


Mike Levin
-- 
+---+  P L E A S E    R E S P O N D   T O: +---+  *  *  *  *  *  *  *  *  *  *
| Mike Levin, Silent Radio Los Angeles (magnus)| I never thought I'd be LOOKING
| Path {csun|kosman|mtune|srhqla}!magnus!levin |    for something to say! ! !
+----------------------------------------------+------------------------------+



More information about the Unix-pc.sources mailing list