Character apparently doesn't get translated to its ASCII value in a C kernel

122 views Asked by At

I'm developing a basic C kernel in a Kali machine and testing it inside QEMU. This is the kernel code:

void kernel_main();
void print();

void print() {
    char* vidMem = (char*)0xb8000;
    *vidMem = "A";
    vidMem++;
    *vidMem = 0x0f;
}

void entry() {
    kernel_main();
    while(1);
}

void kernel_main() {
    print();
}

This prints Z instead of A, but if I replace "A" with the ASCII value for A, 0x41, it works as expected and prints A to the screen. Additionally, I also get this error:

kernel.c:6:10: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
    6 |  *vidMem = "A";
      |          ^

If I understand it correctly, the compiler thinks I'm trying to change the value of vidMem itself to a different value of a different data type; however, I'm dereferencing the vidMem variable and only changing the byte of memory pointed to by vidMem, so I'm confused as to why I get this error. I don't get the error when I replace "A" with 0x41, so all I see is the compiler is treating "A" and 0x41 differently. Why is that and how can I print a character I want without needing to provide its ASCII value?

1

There are 1 answers

0
Tordek On BEST ANSWER
*vidMem = "A";

This is equivalent to

vidMem[0] = "A";

"A" is a string, not a character, so you're assigning (the lowest byte of) the address of the string "A" into that memory position.

You want to do:

vidMem[0] = 'A';

Note the single quotes, which makes it a char literal.