I have a simple OS (real mode), written in NASM (only bootloader) and most in C.
I want to print this character: ñ, and I use this function that uses int 10h to print a character to screen:
void putch(char chr)
{
__asm__ __volatile__ ("int $0x10"
:
: "a" ((0x0e<<8) | chr),
"b" (0x0000));
}
void println(char *str)
{
while (*str)
putch(*str++);
}
Now, I attempt to print ñ:
println("ñ\r\n");
But when I compile and execute (in qemu and VB box), the character "ñ" is ignored. I saved source code in CP-437 encoding, but the problem stills. This affects also all the extended ASCII characters.
The problem is simple. In x86 compilers char is signed. 'ñ', i.e. 0xA4 is considered negative (-92). A signed char be promoted to int (usual arithmetic promotions) for |. This happens through sign-extension.
The resulting value will be of course -92 (0xFFA4), which |ed with 0x0E00 will result in 0xFFA4... which will mean that instead of function AH=0Eh we're now calling the function AH=FFh... if it even exists.
One solution is to have putch take an argument as int and convert it to unsigned char, just like the C functions putchar & al do:
void putch(int chr)
{
__asm__ __volatile__ ("int $0x10"
:
: "a" ((0x0e<<8) | (unsigned char)chr),
"b" (0x0000));
}
Or just make it accept the argument as an unsigned char like suggested in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With