Using NASM assembler and GCC compiler
in "initSegs.inc"
Code: Select all
%define BOOT_SEG 0x07c0
%define KERNEL_SEG 0x1000
%define VIDEO_SEG 0xb800
%define FAT_SEG 0x0120
%define DIR_SEG 0x0240
%define LDR_SEG 0x0200
%define NUM_KERNEL_SECTOR 1
%define NUM_KROUTINE_SECTOR 1
SysCodeSelector EQU 0x08
SysDataSelector EQU 0x10
SysVideoSelector EQU 0x18
;available aux
UsrCodeSelector EQU 0x4b
UsrDataSelector EQU 0x53
in "boot.asm"
Code: Select all
;============================================
;Boot records (FAT12 compatible)
;============================================
jmp boot_start
db 90h
db 'chos ' ;need 8bytes
dw 512 ;byte per sector
db 1 ;sectors per sector
dw 1 ;reserved sectors
db 2 ;numbers of FAT
dw 224 ;entries of root directory
dw 2880 ;total sectors
db 0f0h
dw 9 ;sector per FAT
dw 18 ;sectors per track
dw 0 ;numbers of hidden sectors
dw 0
dd 0
db 0 ; disk driver number(A:0, c:2)
db 0
db 029h
dd 0
db 'CHOS BIN '
db 'FAT12 '
jmp BOOT_SEG:boot_start
boot_start:
cli
mov ax, cs
mov ds, ax
mov fs, ax
mov gs, ax
mov ss, ax
.read_boot_sector
mov ax, KERNEL_SEG
mov es, ax
mov bx, 0x00
mov ah, 0x02
mov al, NUM_KERNEL_SECTOR
mov ch, 0x00
mov cl, 0x02
mov dh, 0x00
mov dl, 0x00
int 0x13
jc .read_boot_sector
.read_kRoutine
mov ax, 0x2000
mov es, ax
mov bx, 0x00
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, 0x03
mov dh, 0x00
mov dl, 0x00
int 0x13
;jc .read_kRoutine
jmp KERNEL_SEG:0x0000 ;0x1000:0x0000
times 510-($-$$) db 0
dw 0xaa55
Code: Select all
%include "initSegs.inc"
[BITS 16]
[ORG 0h]
jmp boot_start
db 90h
db 'chos ' ;need 8bytes
dw 512 ;byte per sector
db 1 ;sectors per sector
dw 1 ;reserved sectors
db 2 ;numbers of FAT
dw 224 ;entries of root directory
dw 2880 ;total sectors
db 0f0h
dw 9 ;sector per FAT
dw 18 ;sectors per track
dw 0 ;numbers of hidden sectors
dw 0
dd 0
db 0 ; disk driver number(A:0, c:2)
db 0
db 029h
dd 0
db 'CHOS BIN '
db 'FAT12 '
jmp BOOT_SEG:boot_start
boot_start:
cli
mov ax, cs
mov ds, ax
mov fs, ax
mov gs, ax
mov ss, ax
.read_boot_sector
mov ax, KERNEL_SEG
mov es, ax
mov bx, 0x00
mov ah, 0x02
mov al, NUM_KERNEL_SECTOR
mov ch, 0x00
mov cl, 0x02
mov dh, 0x00
mov dl, 0x00
int 0x13
jc .read_boot_sector
[b] .read_kRoutine
mov ax, 0x2000
mov es, ax
mov bx, 0x00
mov ah, 0x02
mov al, 0x01
mov ch, 0x00
mov cl, 0x03
mov dh, 0x00
mov dl, 0x00
int 0x13
;jc .read_kRoutine
[/b]
jmp KERNEL_SEG:0x0000
times 510-($-$$) db 0
dw 0xaa55
Code: Select all
%include "initSegs.inc"
[BITS 16]
[ORG 0h]
kernel_start:
mov ax, cs
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
lgdt[gdtr]
mov eax, cr0
or eax, 0x00000001
mov cr0, eax
jmp $+2
nop
nop
jmp dword SysCodeSelector:pm_start
;====================================================
;--------------start of protected mode---------------
;====================================================
[BITS 32]
pm_start:
mov ax, SysDataSelector
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
lea esp, [kernel_start]
call clear_scr
call print_kernel_msg
[b] jmp 0x2000:0x0000[/b]
.kernel_stop
HLT
jmp .kernel_stop
clear_scr: ;clear screen
.proc_in
push eax
push ecx
push edi
push esi
push ds
push es
pushfd
;get source addr [ds:esi]
mov ax, SysDataSelector
mov ds, ax
mov si, clear_scr.bg_color ;get addr of .bg_color
;get target addr [es:edi]
mov ax, SysVideoSelector
mov es, ax
xor edi, edi
;copy source addr to target addr
xor ecx, ecx
mov ecx, 0x7ff
.loop
mov ax, word [ds:esi]
mov word [es:edi], ax
add edi, 2
dec cx
jnz .loop
.proc_out
popfd
pop es
pop ds
pop esi
pop edi
pop ecx
pop eax
ret
.bg_color
db '.', 0x67
print_kernel_msg:
.proc_in
push eax
push ebx
push edi
push esi
push es
pushfd
;get source addr [esi]
lea esi, [.kernel_msg]
mov bl, byte [print_kernel_msg.bg_color]
mov ax, SysVideoSelector
mov es, ax
xor edi, edi
.loop
mov al, byte [esi]
or al, al
jz .porc_out
mov byte [es:edi], al
inc esi
inc edi
mov byte [es:edi], bl
inc edi
jmp .loop
.porc_out
popfd
pop es
pop esi
pop edi
pop ebx
pop eax
ret
.kernel_msg
db 'LOADING...', 0
.bg_color
db '.', 0x23
;========================================
;-------------- G D T ---------------
;========================================
gdtr:
dw gdt_end - gdt - 1 ;limit of GDT
dd gdt + 0x10000 ;base addr of GDT
gdt:
;NULL descriptor
dd 0
dd 0
;SysCodeSelector EQU 0x08
dw 0xffff
dw 0x0000
db 0x01
db 0x9a
db 0xcf
db 0x00
;SysDataSelector EQU 0x10
dw 0xffff
dw 0x0000
db 0x01
db 0x92
db 0xcf
db 0x00
;SysVideoSelector EQU 0x18
dw 0xffff
dw 0x8000
db 0x0b
db 0x92
db 0xcf
db 0x00
;sys aux EQU 0x20
dd 0
dd 0
;sys aux EQU 0x28
dd 0
dd 0
;sys aux EQU 0x30
dd 0
dd 0
;sys aux EQU 0x38
dd 0
dd 0
;sys aux EQU 0x40
dd 0
dd 0
;UsrCodeSelector EQU 0x4b
dw 0x00ff
dw 0x0000
db 0x10
db 0xfa
db 0xcf
db 0x00
;UsrDataSelector EQU 0x53
dw 0x00ff
dw 0x0000
db 0x10
db 0xf2
db 0xcf
db 0x00
;for usr aux
gdt_end:
times (512*NUM_KERNEL_SECTOR) -($-$$) db 0
in "kRoutineAsm.asm"
Code: Select all
[b]printk:
push ebp
mov ebp, esp
push eax
push edi
push es
pushad
mov ax, SysVideoSelector
mov es, ax
mov edi, 0
mov byte [es:edi], 'H'
inc edi
mov byte [es:edi], 0x06
inc edi
mov byte [es:edi], 'i'
inc edi
mov byte [es:edi], 0x06
inc edi
popad
pop es
pop edi
pop eax
mov esp, ebp
pop ebp
ret[/b]
halt:
HLT
jmp halt
times 512 - ($-$$) db 0
Code: Select all
#include <stdio.h>
int main(void){
printk();
halt();
}
Code: Select all
all: boot.asm kernel.asm kRoutineAsm.asm kRoutine.c
nasm -f bin boot.asm -o boot.bin
nasm -f bin kernel.asm -o kernel.bin
gcc -c kRoutine.c
nasm -f elf32 kRoutineAsm.asm
ld -Ttext 0x00020000 -e main -o kRoutine kRoutine.o kRoutineAsm.o
objcopy -R .note -R .comment -S -O binary kRoutine kRoutine.bin
cat boot.bin kernel.bin kRoutine.bin > disk.img
and linking like above
but I can't see "Hi" in screen
what's wrong??
If you help me. I am really appreciate you
Thanks for your reading!!
Have a nice day[/code]