In protected mode, I am attempting to write a function in C that prints a string to the screen. Here is my code so far...
Code: Select all
#define NULL 0
#define HEIGHT 25
#define WIDTH 80
#define WHITE_BACKGROUND 0x0F
#define BRIGHT_RED_TEXT 0x0C
void printChar(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y);
void start()
{
const char *message = "Hello world\0";
char tmpChar = 1;
int counter;
for (counter = 0; tmpChar != NULL; counter++)
{
tmpChar = message[counter];
printChar(tmpChar, BRIGHT_RED_TEXT, WHITE_BACKGROUND, counter, 10);
}
}
void printChar(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y)
{
unsigned short attrib = (backcolour << 4) | (forecolour & 0x0F);
volatile unsigned short *where;
where = (volatile unsigned short *)0xB8000 + (y * 80 + x);
*where = c | (attrib << 8);
}
I tried both and they dont work, for example, the first option.* Write a short function which just calls main() and halts. This way, the first function in the program doesn’t contain any literal strings.
* Use the gcc option -fwritable-strings. This will cause gcc to put literal strings in the data section of the executable, away from any code.
Code: Select all
#define NULL 0
#define HEIGHT 25
#define WIDTH 80
#define WHITE_BACKGROUND 0x0F
#define BRIGHT_RED_TEXT 0x0C
void printChar(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y);
void start();
void call_start()
{
start();
while (1);
}
void start()
{
const char *message = "Hello world\0";
char tmpChar = 1;
int counter;
for (counter = 0; tmpChar != NULL; counter++)
{
tmpChar = message[counter];
printChar(tmpChar, BRIGHT_RED_TEXT, WHITE_BACKGROUND, counter, 10);
}
}
void printChar(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y)
{
unsigned short attrib = (backcolour << 4) | (forecolour & 0x0F);
volatile unsigned short *where;
where = (volatile unsigned short *)0xB8000 + (y * 80 + x);
*where = c | (attrib << 8);
}
Second option (compiling like this)
gcc -ffreestanding -fwritable-strings. -c main.c -o main.o
doesnt work either...
Can anyone help me here?
thanks
EDIT: Just realised, I forgot to tell GCC the entry point when using the first option. But still, I just get repeated reboots
Here are my compile option
ld -e _call_start -Ttext 0x1000 -o kernel.o main.o
ld -i -e _call_start -Ttext 0x1000 -o kernel.o main.o