一个内核的helloworld

项目代码 https://github.com/killinux/mkernel

下载qemu,模拟器http://qemu.weilnetz.de/w32/qemu-20130616-w32.exe
安装后使用
E:\Program Files (x86)\qemu>qemu-system-i386.exe
代码如下
kernel.asm


bits 32
section .text
;multiboot spec
align 4
dd 0x1BADB002 ;magic
dd 0x00 ;flags
dd - (0x1BADB002 + 0x00) ;checksum. m+f+c should be zero

global start
extern kmain ;this is defined in the c file

start:
cli ;block interrupts
call kmain
hlt ;halt the CPU


kernel.c

void kmain(void)
{
char *str = "my first kernel";
//video memory begins at address 0xb8000
char *vidptr = (char*)0xb8000;
unsigned int i = 0;
unsigned int j = 0;

//this loops clears the screen
//there are 25 lines each of 80 columns; each element takes 2 bytes
while(j < 80 * 25 * 2) {
//blank character
vidptr[j] = ' ';
//attribute-byte
vidptr[j+1] = 0x07;
j = j + 2;
}

j = 0;

//this loop write the string to video memory
while(str[j] != '\0') {
//the character's ascii
vidptr[i] = str[j];
//attribute-byte: give character black bg and-light grey fg
vidptr[i+1] = 0x07;
++j;
i = i + 2;
}
return;
}


link.ld

OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
. = 0x100000;
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss) }
}



nasm -f elf32 kernel.asm -o kasm.o
gcc -m32 -c kernel.c -o kc.o
ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o
sz kernel

E:\Program Files (x86)\qemu>qemu-system-i386.exe -kernel kernel

参考
http://www.geekfan.net/8663/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值