So that's my main.c:
Code: Select all
void kernel_start()
{
clear_screen();
printk("Hi, this is my OS booting\n");
for (;;);
}
printk() just calls my OWN vsprintf() function and then calls putc() (also written by me):
Code: Select all
* printk() - print a message on the screen */
int printk(const char *format, ...)
{
???char buffer[MAXLEN], *foo;
???va_list arg;
???int i = 0;
???
???va_start(arg, format);
???vsprintf(buffer, format, arg);???
???
???*foo = buffer[0];
???while (*(foo++) != '\0' && i++)
??????putc(*foo);
???return i;
}
Code: Select all
/* vsprintf() - dissolve conversions */
void vsprintf(char *str, const char *format, va_list ap)
{
???char *p = str;
???char *s; int d; char c;
???int size = sizeof(str);???/* against buffer-overflow */
???
???*p = '\0';
???
???/* go through format until we reach a % */
???while (*(format++) != '\0' && size-- > 0)
???{
??????if (*format != '%')???/* ordinary character */
??????{
?????????*(p++) = *format;
?????????*p = '\0';
??????}
??????
??????else
??????{
?????????switch (*(format+1))
?????????{
????????????case 'c':???/* char */
???????????????c = (char) va_arg(ap, int);
???????????????*(p++) = *(format+1);
???????????????*p = '\0';
???????????????break;??????
????????????case 's':???/* string */
???????????????s = va_arg(ap, char *);
???????????????strcat(p, s);
???????????????break;
????????????case 'd':???/* integer */
????????????case 'i':
???????????????d = va_arg(ap, int);
???????????????itoa(d, s, 10);???/* convert to string */
???????????????strcat(p, s);
???????????????break;
?????????}
??????}
???}
}????????????
Code: Select all
/* putc() - puts a character on the screen */
int putc(u_char c)
{
???char *video_memory = (char *) 0xb8000;???/* video memory address */
???u_int video_index = 0;
???
???/* first of all, do a lock_mutex() for
??? * security reasons...
??? */
???lock_mutex(&putc_mutex);
???switch (c)
???{
??????case '\n':???/* new line */
??????case '\r':???
?????????screen_x = 0;
?????????screen_y++;
?????????break;
??????case '\b':???/* backspace */
?????????screen_x--;???/* overwrite last character */
?????????break;
??????case '\t':???/* tabulator */
?????????screen_x += 8;
?????????break;
??????default:???/* it's an ordinary character */
?????????video_index = ((screen_y + 80) + screen_x);
?????????video_index *= 2;
?????????video_memory[video_index] = c;???/* put c into video memory */
?????????video_memory[video_index+1] = 0x07;???/* white text on black screen */
?????????/* update coordinates */
?????????screen_x++;
?????????break;
???}
???/* check if line is full */
???if (screen_x > 79)
???{
??????screen_x = 0;
??????screen_y++;
???}
???/* check if we are at the bottom of the screen */
???if (screen_y > 24)
???{
??????/* copy all lines one up */
??????memcpy(video_memory, (void*) (0xb8000+(80*2)), 24*80*2);
??????/* clear the last line */
??????memset((void*) (0xb8000+(24*80*2)), 0x0720, 80);
??????screen_y = 24;
???}
???/* now update the cursor */
???gotoxy (screen_x, screen_y);
???/* at last, unlock the mutex */
???unlock_mutex (&putc_mutex);
???/* for the moment, putc() always returns 1 which
??? * means everything is fine
??? */
???return 1;???
}
I think it causes an exception (3), but I don't know why it should do that..
Please help, thanks!
