I am trying to implement a VGA print function but I cannot get data to be seen as a non null character. Even if I hardcode data inside this function it will still skip the while loop. I have a debug print that will print a Z if count is zero and it always prints it and the cursor does not move so I am pretty confident it is not entering the while loop
void vga_print(char *data, char mode) {
char *vga = (char *)(VGA_MEM_LOC);
uint8_t count = 0;
while (*data != '\0') {
if (*data == '\n') {
current_x = 0;
current_y++;
} else {
*vga++ = *data++;
*vga++ = mode;
current_x++;
if (current_x >= screen_x) {
current_x = 0;
current_y++;
}
}
count++;
}
// move the cursor
uint16_t location = current_x + ((current_y) * screen_x);
x86_port_out(VGA_CRT_INDEX, VGA_CURSOR_LOW);
x86_port_out(VGA_CRT_DATA, location);
x86_port_out(VGA_CRT_INDEX, VGA_CURSOR_HIGH);
x86_port_out(VGA_CRT_DATA, location >> 8);
}
EDIT: If I initialize the data string as char example[] = "xyz\0" instead of char *example = "xyz\0" it works
There are problems in the function, but none that explains the observed behavior. There might be something wrong with the initial values of the global variables
current_x,current_yandscreen_xor the way you call the function from code you did not post. The definitions ofVGA_CRT_INDEX,VGA_CURSOR_LOW,VGA_CRT_DATA,VGA_CURSOR_HIGH,VGA_MEM_LOCand the functionx86_port_outmight be incorrect too.Here are some problems:
current_xandcurrent_yget updated for each byte and the newline, but thevgapointer does not move so the next character appears glued to the previous output.datawhen*data == '\n': this would cause an infinite loop.current_y >= screen_y.Here is a modified version: