一个内核的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/

你可能感兴趣的:(gcc,kernel)