I'm trying to write my own little kernel in C, and actually I would like to write a print function to display string. Thus, I would like to write to the video memory (at 0xB8000).
So, I tried like this :
unsigned char *video = (unsigned char *)0xB8000;
*video = 'A';
This actually works, but following does not :
char x = 0;
unsigned char *video = (unsigned char *)(0xB8000 + x);
*video = 'A';
After a few research, I fould that the cause could be the compiler's optimizations, and OSDev give me the solution : use the volatile
keyword. So I made some research about this keyword. OSDev suggest this :
char x = 0;
volatile unsigned char *video = (volatile unsigned char *)(0xB8000 + x);
*video = 'A';
This way, it should work. The compiler assumes that the value pointed to by video
pointer can change, and then do not optimize it. But, if later I want to change it, like :
video = (volatile unsigned char *)(video + 2);
Should I not define the pointer as volatile
, like unsigned char * volatile video
? So the compiler know that the address can change ?