Simple boot / C kernel issues

Recently, I became interested in writing my really really basic OS. I wrote (well, copied) some basic assembly that installs the stack and does some basic things, and this seemed to work fine, however, trying to inject C into the mix screwed everything up.

I have two main project files: loader.s, which is some NASM, which creates a stack and calls my C function, and kernel.c, which contains the basic C function.

At the moment, the problem is that QEMU freezes when I run the kernel.bin file. I assume that there is something wrong with my code: perhaps this question is not suitable for the StackOverflow format due to its exceptional specificity. My project files are as follows:

loader.s:

BITS 16                         ; 16 Bits

extern kmain                    ; Our 'proper' kernel function in C

loader:
    mov ax, 07C0h           ; Move the starting address [7C00h] into 'ax'
    add ax, 32              ; Leave 32 16 byte blocks [200h] for the 512 code segment
    mov ss, ax              ; Set 'stack segment' to the start of our stack
    mov sp, 4096            ; Set the stack pointer to the end of our stack [4096 bytes in size]

    mov ax, 07C0h           ; Use 'ax' to set 'ds'
    mov ds, ax              ; Set data segment to where we're loaded
    mov es, ax              ; Set our extra segment

    call kmain              ; Call the kernel proper

    cli                     ; Clear ints

    jmp $                   ; Hang


; Since putting these in and booting the image without '-kernel' can't find
; a bootable device, we'll comment these out for now and run the ROM with
; the '-kernel' flag in QEMU
        ;times 510-($-$$) db 0          ; Pad remained of our boot sector with 0s
        ;dw 0xAA55                      ; The standard 'magic word' boot sig

kernel.c:

#include <stdint.h>

void kmain(void)
{
        unsigned char *vidmem = (char*)0xB8000; //Video memory address
        vidmem[0] = 65; //The character 'A'
        vidmem[1] = 0x07; //Light grey (7) on black (0)
}

I compile everything like this:

nasm -f elf -o loader.o loader.s

i386-elf-gcc -I/usr/include -o kernel.o -c kernel.c -Wall -nostdlib -fno-builtin -nostartfiles -nodefaultlibs

i386-elf-ld -T linker.ld -o kernel.bin loader.o kernel.o

:

qemu-system-x86_64 -kernel kernel.bin

, - - .

.

+5
1

, ? (, ?)

loader.s (MBR). MBR . , loader.s, MBR: loader.s, MBR. loader.s MBR, . , , MBR...

loader.s, MBR, " ". - 436 . , , - C- (.. , MBR) . , 1, .

, kernel.c , . 436 BIOS ( EFI) ( ), . , , , , "" , , .

, , . - , SO. , , , OSDev. : , , , . , - , . (*)

(*): , dwalter, .; -)

: , , . i386-elf-gcc - 32- , "" , /, . loader.s - 16- ( BITS 16), , GCC, , GCC ... BAM.

+10

All Articles