自己动手写操作系统二(编写内核Hello World 教程)

   By EvilBinary 小E 

   本文用到的boot.s 和setup.s 两个文件见本blog MyOs  分类相关文章。

    我们编写start.s 来启动初始化信息,然后进入main.c内核代码,然后显示Evilbinary os字样

//start.s 文件

global _start
extern old_pgdt
extern pgdt
extern gdt
extern pidt
extern setup_gdt
extern setup_idt
extern setup_ldt
extern init_8259a
extern main

section .text
jmp _start	;0x8:0x400

	stack times 128 db 0
_start:
	mov ax,ds
	mov ds,ax
	mov ss,ax
	mov es,ax
	mov fs,ax
	mov gs,ax
	mov eax,stack
	mov esp,eax
	xor eax,eax

	sgdt [old_pgdt]
	call init_8259a
	call setup_gdt
	call setup_idt
	lgdt [pgdt]
	lidt [pidt]
	jmp 0x08:kernel
kernel:
	jmp main
	jmp $
	halt
	
//main.c 文件

#include 
#include 
void welcome(){
	clrscr();
	e8 welcome[]={"Welcome to EvilBinary OS."};
	e32 i;
	setxy(18,0);
	for(i=0;i<24;i++){
		e8 c=welcome[i];
		write_char(c,LIGHTGREEN);
	}
	printf("\n");
}

int main()
{	
	welcome();
	for(;;)
	return 0; 
}	
用到的相关函数及头文件

//type.h

#ifndef	_TYPE_H
#define _TYPE_H

typedef char   e8;
typedef short  e16;
typedef int    e32;
typedef double e64;
typedef int  ebool;
typedef unsigned char   u8;
typedef unsigned short  u16;
typedef unsigned int    u32;
typedef double u64;

typedef void (*function_handl)();

#endif


//io.c

void write_char(e8 ch,e8 color){
	e8* pVideo=(e8*)0xb8000; 
	u16 offset;
	outb(0x3d4,14);
	offset=inb(0x3d5)<<8;
	outb(0x3d4,15);
	offset|=inb(0x3d5);
	pVideo+=offset*2;
	*pVideo=ch;
	pVideo++;
	*pVideo= color;
	offset++;
	outb(0x3d5,(u8)offset);
	outb(0x3d4,14);
	outb(0x3d5,(u8)(offset>>8));
}
//kernellib.s

;extern outb(u16 port,u8 data)
_outb:	
	push ebp
	mov ebp,esp
	mov al,[esp+12]
	mov dx,[esp+8]
	out dx,al
	mov esp,ebp
	pop ebp
	ret 

;u8 inb(u16 port)
_inb:

	push ebp
	mov ebp,esp
	mov dx,[esp+8]
	in al,dx
	mov esp,ebp
	pop ebp

	ret

;extern outw(u16 port,u16 data)
_outw:	
	push ebp
	mov ebp,esp
	mov ax,[esp+12]
	mov dx,[esp+8]
	out dx,ax
	mov esp,ebp
	pop ebp
	ret 

;u16 inw(u16 port)
_inw:

	push ebp
	mov ebp,esp
	mov dx,[esp+8]
	in ax,dx
	mov esp,ebp
	pop ebp
	ret
//结果







你可能感兴趣的:(MyOS)