Page 1 of 2

Need help with C Kernel

Posted: Wed Jul 21, 2010 4:55 pm
by haxifix
Hey guys, I finally got a C kernel with assembly to boot but there is a problem. It is not displaying the text that it is supposed to be. Can someone please tell me what is going wrong?

kernel.c

Code: Select all

#define WHITE_TXT 0x07 // white on black text

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);


k_main() // like main in a normal C program
{
	k_clear_screen();
	k_printf("Hi!\nHow's this for a starter OS?", 0);
};

void k_clear_screen() // clear the entire text screen
{
	char *vidmem = (char *) 0xb8000;
	unsigned int i=0;
	while(i < (80*25*2))
	{
		vidmem[i]=' ';
		i++;
		vidmem[i]=WHITE_TXT;
		i++;
	};
};

unsigned int k_printf(char *message, unsigned int line) // the message and then the line #
{
	char *vidmem = (char *) 0xb8000;
	unsigned int i=0;

	i=(line*80*2);

	while(*message!=0)
	{
		if(*message=='\n') // check for a new line
		{
			line++;
			i=(line*80*2);
			*message++;
		} else {
			vidmem[i]=*message;
			*message++;
			i++;
			vidmem[i]=WHITE_TXT;
			i++;
		};
	};

	return(1);
};
kernel.h

Code: Select all

#define WHITE_TXT 0x07 // white on black text

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);
kernel.asm

Code: Select all

[BITS 32]

[global start]
[extern _k_main] ; this is in the c file

start:
  call _k_main

  cli  ; stop interrupts
  hlt ; halt the CPU
This is what I'm using to compile,

C Source files - gcc -c -o kernel.o kernel.c
ASM - nasm -f win32 -o asmkernel.o kernel.asm
Linker - ld -o kernel.img kernel.o asmkernel.o

The linker script used to be ld -o kernel.bin kernel.o asmkernel.o but I changed it to .img so I can write it to a floppy. Hopefully, this is not the problem as I do not know if you can even convert a .bin to a .img file, or if you can write a .bin file to a floppy.

Thanks for any help.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:09 pm
by gerryg400
It is not displaying the text that it is supposed to be. Can someone please tell me what is going wrong?
It would be better if you gave some useful information like 'I'm operating on real hardware and the screen is entirely blank' or 'I'm testing on Vmware and the text is the wrong colour', so let me guess.

You're using Qemu and not all your chars are printed ?

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:13 pm
by haxifix
No, I'm actually using a virtual floppy drive and raw write to write the .img file to the virtual drive. Then I boot it up using Virtual PC and no text is showing.

Close though, and sorry...first time poster.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:23 pm
by JackScott
Are you writing the kernel straight to the floppy, or are you combining it with other things first? That kernel will not boot by itself, it needs another program (called a bootloader) to make the transition from 16bit real mode to 32bit protected mode and load the kernel.

I suggest reading up in the wiki about bootloaders and GRUB. Reading the Bare Bones page may also be helpful.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:33 pm
by haxifix
OK, can you tell me how to make a simple bootloader to allow me to run this code in Virtual PC using a floppy?

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:36 pm
by JackScott
I wouldn't suggest writing your own at this stage. Instead I would get GRUB (which is a very good bootloader written by the folks at GNU) and use that to load your kernel. The barebones tutorial I linked above will show you how to do that. The Disk Images page has a link to an image with GRUB already on it.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:42 pm
by haxifix
Yes, but all of that tutorial is expecting you are running a *nix system. I am running Windows :(

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:45 pm
by JackScott
It looks like you're using cygwin to compile your kernel. That's enough of a UNIX environment to work with.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:47 pm
by gerryg400
I am running Windows
Why ? I guess if your computer is used for more than one purpose it could make sense, but with the tools for O/S development available to a unix user it may be worth considering the switch.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:51 pm
by haxifix
Don't know lol I think I might partition my HDD and have a Linux development environment. Any suggestions on linux distro's I should use?

@topic
Can you tell me where and how to install cygwin? Never used it before, and using GRUB can I still use my virtual floppy drive and virtual pc to boot?

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 5:59 pm
by JackScott
Yes, GRUB is compatible with those tools.

I suggest Googling to find cygwin and install it.

I recommend Ubuntu for an easy-to-use desktop system. Debian is another popular choice (and what I use).

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 6:05 pm
by gerryg400
Sorry everyone about my crack against windows. I'm not really a Linux nut, it's just that it really does make things a little easier.
Don't know lol I think I might partition my HDD and have a Linux development environment. Any suggestions on linux distro's I should use?
For a very long time I did all my O/S building in a VMware virtual machine running a command line version of Ubuntu. I kept my source repo, editor, documents etc. on the host machine and used Ubuntu only to build and make images. I felt that was simpler than Cygwin.

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 6:14 pm
by haxifix
That actually sounds like a pretty good idea.

I was reading that link you sent me, and the chapter 1 at the end has a part called The Build Process and everything in there sounds like I can do it in Windows.

Do they have a link to the bootloader and kernel they are using in that tutorial?

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 6:16 pm
by gerryg400
One more tip for the OP

Code: Select all

  cli  ; stop interrupts
  hlt ; halt the CPU
Make sure interrupts are enabled when you halt and put a loop around the halt just in case the cpu wakes up. Some emulators stop processing instructions if you halt with interrupts disabled. That means the screen won't be updated.

Code: Select all

  sti  ; enable interrupts
HALT:
  hlt  ; halt the CPU
  jmp HALT 

Re: Need help with C Kernel

Posted: Wed Jul 21, 2010 6:23 pm
by haxifix
Thanks, but what is the easiest way for me to boot this kernel.bin file?