운영 체제 2(커널 Hello World 자습서 작성)
2201 단어 MyOS
본문에 사용된 boot.s와 setup.s 두 파일은 이 블로그 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 파일4
#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("
");
}
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 4
;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
//결과