Code: Select all
//------------------------------------------------------------------------------------------------------------//
// Code By Ekrem KOCAK //
// [email protected] //
// Kırşehir 2006 // //
// //
//----------------------------------------------------------------------------------------------------------- //
program CreateKernel;
{$APPTYPE CONSOLE}
uses
Windows,SysUtils,Classes,Dialogs;
Type
TMultibootheader= packed record
magic :DWORD;
flags :DWORD;
checksum :DWORD;
header_addr :DWORD;
load_addr :DWORD;
load_end_addr :DWORD;
bss_end_addr :DWORD;
entry_addr :DWORD;
mode_type :DWORD;
width :DWORD;
height :DWORD;
depth :DWORD;
end;
//------------- Kernel Code Begin --------------------------
procedure Main(); stdcall;forward;
procedure loader; stdcall;
asm
cli
call main
hlt
end;
function Screen():PChar; stdcall; //Video bellek bölgesini gösteren işaretçi
begin
Result:=PChar($B8000);
end;
procedure Cls(); stdcall;
var
i: integer;
begin
for i:=0 to 2000 do
begin
Screen[i*2]:=#0;
Screen[i*2-1]:=char(6); //color white
end;
end;
procedure putpixel(X, Y: integer; text:Char;Color:Byte); stdcall;
var
address: Word;
begin
address:= X*2 + Y * 160;
Screen[address]:= text;
Screen[address+1]:= char(Color);
end;
procedure WriteLn(X, Y: integer;Text : PCHAR; Color:Byte); stdcall;
var
address: Word;
i: integer;
begin
i:=0;
repeat
address:= X*2 + Y * 160;
Screen[address]:= (text[i]);
Screen[address+1]:= char(Color);
inc(x);
i:=i + 1;
until text[i] = #0
end;
procedure Main(); stdcall;
var
Str:pchar;
begin
Cls();
str:='Merhaba Dünya';
WriteLn(1,2,Str,4);
end;
procedure loader_end();
begin
end;
//------------- Kernel Code End --------------------------
var
Multibootheader:TMultibootheader;
MemoryStream:TMemoryStream;
pFunc: Pointer;
dwSize: DWORD;
fwSize: DWORD;
pBuff: Pointer;
ImageBase : Integer;
Entry_addr : Integer;
begin
// Project > Options... > Packages Tab > Runtime packages group box > Build witg runtime packages check box true
ImageBase := $00400000; // Project > Options... > Linker Tab > Memory sizes group box > Image Base
Entry_Addr:= DWORD(@loader) - ImageBase ;
//showmessage(inttohex(entry_addr,8) );
MemoryStream:=TMemoryStream.Create;
try
FillChar(Multibootheader, 48, #0);
Multibootheader.magic := ($1BADB002);
Multibootheader.flags := (1 shl 16) ;
Multibootheader.checksum := DWORD(-($1BADB002 + (1 shl 16)));
Multibootheader.header_addr := ($00400000);
Multibootheader.load_addr := ($00400000);
Multibootheader.load_end_addr := ($00000000);
Multibootheader.bss_end_addr := ($00000000);
Multibootheader.entry_addr := DWORD(ImageBase + Entry_Addr );
Multibootheader.mode_type := ($00000000);
Multibootheader.width := ($00000000);
Multibootheader.height := ($00000000);
Multibootheader.depth := ($00000000);
MemoryStream.Write(Multibootheader, SizeOf(Multibootheader));
dwSize := Entry_Addr - Sizeof(Multibootheader) ;
GetMem(pBuff, dwSize);
ZeroMemory(pBuff, dwSize);
MemoryStream.Write(pBuff^, dwSize);
FreeMem(pBuff, dwSize);
pFunc := @loader;
fwSize := DWORD(@loader_end) - DWORD(@loader);
dwSize := $1000 - (fwSize);
GetMem(pBuff, dwSize);
ZeroMemory(pBuff, dwSize);
MemoryStream.Write(pFunc^, fwSize);
MemoryStream.Write(pBuff^, dwSize);
FreeMem(pBuff, dwSize);
MemoryStream.SaveToFile('Kernel.bin');
finally
MemoryStream.Free;
end;
end.