Simple C++ Kernel
Posted: Fri May 29, 2009 1:21 am
Hello, as you probably expect I am fairly new to OS dev. I have finished reading through the baby steps tutorials (found them in another topic and people seemed to get annoyed when the OP hadn't already put forth some effort) and found it a little difficult to understand how to correctly enter protected mode (I get that it is setting a single bit, but am unsure how to go about accomplishing this).
Now, I understand that many of you will read that previous paragraph and believe that there isn't a point in wasting your time with the rest of this, that may be the case (depending on how helpful I can be in answering questions and clarifying the problems I am encountering), but I hope to get some help.
I am attempting to create a simple Kernel (print out "Hello world") with C++ (mainly because Assembly is not my strong point, and I only started looking into it about 3 weeks ago). So I am trying to get as quick a sprint to something I am more comfortable with (C/C++) as possible.
On to the point. As the later baby steps tutorials left me a little puzzled, I chose to try and strip out as much as possible from them and get the desired effect (which I have failed at obtaining). Here is all my code:
boot.asm
Video.h
Video.cpp
Kernel.cpp
Link.ld
Compiling, linking, and floppy burning directives:
Clear out the video memory (effectively clearing the screen)
Printing out "Hello World!!!!" to the screen
At this point that isn't happening. After reading through this thread it sounds like you have to enter protected mode to be able to do any of this.
As far as I can tell, the issue is with the boot.asm file which I believe I want to have as a binary file instead of a .o, the only problem there is that nasm won't allow you to make external calls from a binary file.
Any help/ guidance is greatly appreciated,
Beta
Now, I understand that many of you will read that previous paragraph and believe that there isn't a point in wasting your time with the rest of this, that may be the case (depending on how helpful I can be in answering questions and clarifying the problems I am encountering), but I hope to get some help.
I am attempting to create a simple Kernel (print out "Hello world") with C++ (mainly because Assembly is not my strong point, and I only started looking into it about 3 weeks ago). So I am trying to get as quick a sprint to something I am more comfortable with (C/C++) as possible.
On to the point. As the later baby steps tutorials left me a little puzzled, I chose to try and strip out as much as possible from them and get the desired effect (which I have failed at obtaining). Here is all my code:
boot.asm
Code: Select all
[BITS 32]
[global start]
[extern main]
start:
call main
hang:
jmp hang
times 510-($-$$) db 0
db 0x55
db 0xAA
Code: Select all
#ifndef VIDEO_HEADER
#define VIDEO_HEADER
class Video{
private:
unsigned short* videoMemory;
unsigned int row;
unsigned int column;
public:
Video();
// ~Video();
void clear();
void write(char* p);
void put(char c);
};
#endif
Code: Select all
#include "Video.h"
Video::Video(){
row = column = 0;
videoMemory = (unsigned short*)0xb800;
}
//Video::~Video(){
//}
void Video::clear(){
for(unsigned int i=0; i<(80*25); i++){
videoMemory[i] = (unsigned char)' '|0x0700;
}
row = column = 0;
}
void Video::write(char* cp){
char* str = cp;
for(char* ch = str; *ch && *ch!='\0'; ch++){
put(*ch);
}
}
void Video::put(char c){
if(column >= 80 || c == '\n'){
column = 0;
row += 80;
}
if(row >= (80*25)){
clear(); // scroll the screen later on.
}
videoMemory[row+column] = (unsigned char)c|0x0700;
column++;
}
Code: Select all
#include "Video.h"
int main(void){
Video vid;
vid.clear();
vid.write("Hello World!!!!");
}
Code: Select all
OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS{
.text 0x100000 : {
code = .; _code = .; __code = .;
*(.text)
. = ALIGN(4096);
}
.data : {
date = .; _data = .; __data = .;
*(.data)
. = ALIGN(4096);
}
.bss : {
bss = .; _bss = .; __bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .; _end = .; __end = .;
}
Desired effect:nasm -f aout boot.asm -o boot.o
g++-2.95 -c Video.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions
g++-2.95 -c Kernel.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions
ld -T Link.ld -o Kernel.bin boot.o Kernel.o Video.o
dd if=Kernel.bin of=/dev/fd0
Clear out the video memory (effectively clearing the screen)
Printing out "Hello World!!!!" to the screen
At this point that isn't happening. After reading through this thread it sounds like you have to enter protected mode to be able to do any of this.
As far as I can tell, the issue is with the boot.asm file which I believe I want to have as a binary file instead of a .o, the only problem there is that nasm won't allow you to make external calls from a binary file.
Any help/ guidance is greatly appreciated,
Beta