|
|
Log in / Subscribe / Register

Would you like signs with those chars?

Would you like signs with those chars?

Posted Oct 24, 2022 21:31 UTC (Mon) by lisch (subscriber, #36574)
Parent article: Would you like signs with those chars?

Thus, x86 developers can get into the habit of thinking of char as always being signed and, as a result, write code that will misbehave on some other systems.
In my experience, x86 developers are more likely to unwittingly think of char as unsigned. For example,
char c = '\xfc';
int array[256] = { ... };
do_something(array[c]);   // BOOM!


to post comments

Would you like signs with those chars?

Posted Oct 25, 2022 6:42 UTC (Tue) by eru (subscriber, #2753) [Link] (6 responses)

In my experience, x86 developers are more likely to unwittingly think of char as unsigned.

Maybe because some early C compilers for the PC worked this way. Like Lattice C. The original 16-bit 8086 did not have the sign-extending 8-bit load as a single instruction, so it was more efficient to treat char as unsigned.

Would you like signs with those chars?

Posted Oct 25, 2022 9:51 UTC (Tue) by pbonzini (subscriber, #60935) [Link] (4 responses)

I suspect most x86 developers have never heard of Lattice C. :)

> The original 16-bit 8086 did not have the sign-extending 8-bit load as a single instruction, so it was more efficient to treat char as unsigned.

"mov al, [x] + cbw" (sign extending) is even one byte shorter than "xor ax, ax + mov al, [x]", so there is no particular x86-specific reason to pick unsigned over signed. Unsigned chars are simply more intuitive; signed-by-default chars are a relic of K&R C not having the signed keyword at all.

Would you like signs with those chars?

Posted Oct 25, 2022 16:20 UTC (Tue) by khim (subscriber, #9252) [Link] (1 responses)

> I suspect most x86 developers have never heard of Lattice C. :)

They may not have heard about it, but they definitely know its properties. Lattice C is maiden name of Microsoft C and I doubt x86 developers who have never heard about Microsoft C exist.

P.S. Yes, Microsoft C 3.0 is independent rewrite but it had to be compatible with Microsoft C 1.x and 2.x and thus it had to be compatible with Lattice C, too.

Would you like signs with those chars?

Posted Oct 26, 2022 14:29 UTC (Wed) by eru (subscriber, #2753) [Link]

I recall Microsoft C switched to signed characters in that rewrite, probably to be compatible with most Unix compilers. Xenix used Microsoft C as its system compiler, so compatibility helped porting. That is when I found out about the indeterminate behaviour of char in real life.

Would you like signs with those chars?

Posted Oct 25, 2022 16:34 UTC (Tue) by khim (subscriber, #9252) [Link] (1 responses)

cbw only works with accumulator, though. If you want speed and use register variables (means you use di and si) then you cannot use it.

Whether that was actual reason for the use of unsigned char or not we would never know, of course.

Would you like signs with those chars?

Posted Oct 26, 2022 8:18 UTC (Wed) by pbonzini (subscriber, #60935) [Link]

Compilers at the time weren't super smart on register allocation (also because only AX and BX were both 8-bit accessible and not given special duties by the ISA; there simply wasn't a lot of freedom). Loading from memory into the accumulator was by far the common case. Even though a simple char+char addition would have had to use "XCHG BX,AX" or something like that in order to sign extend both operands, CBW+XCHG would be the same length as clearing BH.

Would you like signs with those chars?

Posted Oct 25, 2022 10:16 UTC (Tue) by adobriyan (guest, #30858) [Link]

XLATB treats AL as unsigned, too.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds