From 23771ca8a8d795ab58cdb3646fc54fbfdd08af65 Mon Sep 17 00:00:00 2001 From: Recolic Keghart <root@recolic.net> Date: Mon, 8 Jun 2020 17:49:59 +0800 Subject: [PATCH] init --- asm/Makefile | 6 ++++++ asm/{x16.asm => boot.asm} | 19 ++++++++++++++++--- asm/mbr_end.inc | 2 ++ asm/str.16.inc | 17 +++++++++++++++++ asm/str.32.inc | 20 ++++++++++++++++++++ asm/test.asm | 25 +++++++++++++++++++++---- boot.backup.c | 39 --------------------------------------- 7 files changed, 82 insertions(+), 46 deletions(-) create mode 100644 asm/Makefile rename asm/{x16.asm => boot.asm} (70%) create mode 100644 asm/mbr_end.inc create mode 100644 asm/str.16.inc create mode 100644 asm/str.32.inc delete mode 100644 boot.backup.c diff --git a/asm/Makefile b/asm/Makefile new file mode 100644 index 0000000..ce6142a --- /dev/null +++ b/asm/Makefile @@ -0,0 +1,6 @@ +build: + nasm -f bin boot.asm -o boot.img + +run: build + qemu-system-x86_64 boot.img + diff --git a/asm/x16.asm b/asm/boot.asm similarity index 70% rename from asm/x16.asm rename to asm/boot.asm index 4321a91..b7d842e 100644 --- a/asm/x16.asm +++ b/asm/boot.asm @@ -1,7 +1,7 @@ [bits 16] [org 0x7c00] -jmp _enter_prot_mode +jmp _init_prot_mode gdt_begin: @@ -36,7 +36,7 @@ gdt_desc: CODE_SEG_OFFSET equ gdt_entry_1 - gdt_begin DATA_SEG_OFFSET equ gdt_entry_2 - gdt_begin -_enter_prot_mode: +_init_prot_mode: cli lgdt [gdt_desc] mov eax, cr0 @@ -46,6 +46,7 @@ _enter_prot_mode: jmp CODE_SEG_OFFSET:_prot_begin [bits 32] +%include "./str.32.inc" _prot_begin: mov ax, DATA_SEG_OFFSET mov ds, ax @@ -56,8 +57,20 @@ _prot_begin: mov ebp, 0x90000 ; 600KB free space here, until 0x7c00 + 0x200 byte (MBR sector 0) mov esp, ebp + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 32BIT PROTECTED MODE BEGIN ;;;;;;;;;;;;;;;;;;;;;;;;;; +_dead_loop: + mov ebx, _motd_32 + call println_vga + mov ebx, _motd_32p + call println_vga + jmp _dead_loop + +_motd_32: + db '----- CPU is in INTEL x86 protected mode now -----', 0x0 +_motd_32p: + db '+++++ CPU is in INTEL x86 protected mode now +++++', 0x0 - +%include "./mbr_end.inc" diff --git a/asm/mbr_end.inc b/asm/mbr_end.inc new file mode 100644 index 0000000..97b1717 --- /dev/null +++ b/asm/mbr_end.inc @@ -0,0 +1,2 @@ +times 510-($-$$) db 0 +dw 0xaa55 diff --git a/asm/str.16.inc b/asm/str.16.inc new file mode 100644 index 0000000..ce51edf --- /dev/null +++ b/asm/str.16.inc @@ -0,0 +1,17 @@ + +println_bios: + ; Arg0: addr in register bx + ; while true: + ; if [bx] != NULL: + ; print [bx]; ++bx + mov ah, 0x0e +_loop_begin: + mov al, [bx] + cmp al, 0x0 + je _loop_exit + int 0x10 + inc bx + jmp _loop_begin +_loop_exit: + ret + diff --git a/asm/str.32.inc b/asm/str.32.inc new file mode 100644 index 0000000..5f8c933 --- /dev/null +++ b/asm/str.32.inc @@ -0,0 +1,20 @@ + +VGA_BEGIN_ADDR equ 0xb8000 +println_vga: + ; Arg0: addr in register ebx + pusha + mov edx, VGA_BEGIN_ADDR + mov ah, 0x0f +_loop_begin_1: + mov al, [ebx] + cmp al, 0x0 + je _loop_exit_1 + mov [edx], ax + inc ebx + add edx, 2 + jmp _loop_begin_1 +_loop_exit_1: + popa + ret + + diff --git a/asm/test.asm b/asm/test.asm index 4e8d2f4..086200f 100644 --- a/asm/test.asm +++ b/asm/test.asm @@ -3,8 +3,9 @@ ; [org 0x7c00] -mov bx, str -call println_bios +mov ebx, str +call println_vga +jmp $ println_bios: ; Arg0: addr in register bx @@ -22,13 +23,29 @@ _loop_begin: _loop_exit: ret +; [bits 32] +VGA_BEGIN_ADDR equ 0xb8000 println_vga: - ; Arg0: addr in register eax + ; Arg0: addr in register ebx + pusha + mov edx, VGA_BEGIN_ADDR + mov ah, 0x0f +_loop_begin_1: + mov al, [ebx] + cmp al, 0x0 + je _loop_exit_1 + mov [edx], ax + inc ebx + add edx, 2 + jmp _loop_begin_1 +_loop_exit_1: + popa + ret + str: db 'This is Recolic!', 0x0 -jmp $ times 510-($-$$) db 0 dw 0xaa55 diff --git a/boot.backup.c b/boot.backup.c deleted file mode 100644 index 5b0b903..0000000 --- a/boot.backup.c +++ /dev/null @@ -1,39 +0,0 @@ - -asm ("jmp 0x14"); - -//void print_char(char to_print) { -// asm inline ( -// "mov $0x0e, %%ah\n\t" -// "mov %0, %%al\n\t" -// "int $0x10" -// : -// : "Ir" (to_print) -// : "ax" -// ); -//} -// -//void println(char *s) { -// while(*s != 0) { -// print_char(*s); -// } -//} - - - -void main() { -_start_p: - asm ( - "mov $0x0e, %ah\n\t" - "mov $0x55, %al\n\t" - "int $0x10" - ); - //print_char('X'); - //println("Hello world!"); - //println("Recolic Booting OS HERE..."); -} - -asm ( -"jmp 0x-2\n\t" -); - - -- GitLab