I currently have a custom bootloader(Yes i know its crap, but it gets the job done):
NASM:
Code: Select all
; boot1.asm stand alone program for floppy boot sector
; Compiled using nasm -f bin boot1.asm
; Written to floppy with dd if=boot1 of=/dev/fd0
; Boot record is loaded at 0000:7C00,
ORG 7C00h
; load message address into SI register:
LEA SI,[msg]
; screen function:
MOV AH,0Eh
print: MOV AL,[SI]
CMP AL,0
JZ done ; zero byte at end of string
INT 10h ; write character to screen.
INC SI
JMP print
; wait for 'any key':
done: ;MOV AH,0
;INT 16h ; waits for key press
; AL is ASCII code or zero
; AH is keyboard code
;===================================
; load the kernel at 0800h:0000h
; 10 sectors starting at:
; cylinder: 0
; sector: 2
; head: 0
; BIOS passes drive number in dl,
; so it's not changed:
mov ah, 02h ; read function.
mov al, 10 ; sectors to read.
mov ch, 0 ; cylinder.
mov cl, 2 ; sector.
mov dh, 0 ; head.
; dl not changed! - drive number.
; es:bx points to receiving
; data buffer:
mov bx, 0800h
mov es, bx
mov bx, 0
; read!
int 13h
jmp 0800h:0000h
; store magic value at 0040h:0072h to reboot:
; 0000h - cold boot.
; 1234h - warm boot.
;MOV AX,0040h
;MOV DS,AX
;MOV word[0072h],0000h ; cold boot.
;JMP 0FFFFh:0000h ; reboot!
msg DB 'Welcome, I have control of the computer.',13,10
DB 'Press any key to reboot.',13,10
DB '(after removing the floppy)',13,10,0
; end boot1
TIMES 510 - ($ - $$) db 0 ;Fill the rest of sector with 0
DW 0xAA55 ;Add boot signature at the end of bootloader
NASM:
Code: Select all
; boot1.asm stand alone program for floppy boot sector
; Compiled using nasm -f bin boot1.asm
; Written to floppy with dd if=boot1 of=/dev/fd0
global loader
[BITS 32]
extern _kmain
section .text
; Boot record is loaded at 0000:7C00,
;ORG 0000h
; load message address into SI register:
LEA SI,[msg]
; screen function:
MOV AH,0Eh
print: MOV AL,[SI]
CMP AL,0
JZ done ; zero byte at end of string
INT 10h ; write character to screen.
INC SI
JMP print
; wait for 'any key':
done: ;MOV AH,0
;INT 16h ; waits for key press
; AL is ASCII code or zero
; AH is keyboard code
call _kmain
; store magic value at 0040h:0072h to reboot:
; 0000h - cold boot.
; 1234h - warm boot.
MOV AX,0040h
MOV DS,AX
MOV word[0072h],0000h ; cold boot.
JMP 0FFFFh:0000h ; reboot!
msg DB 'Welcome, I(k2) have control of the computer.',13,10
DB 'Press any key to reboot.',13,10
DB '(after removing the floppy)',13,10,0
; end boot1
TIMES 512 - ($ - $$) db 0 ;Fill the rest of sector with 0
Code: Select all
void k_clear_screen();
void kmain()
{
/* Print a letter to screen to see everything is working: */
unsigned char *videoram = (unsigned char *) 0xb8000;
videoram[0] = 65; /* character 'A' */
videoram[1] = 0x07; /* forground, background color. */
/* Write your kernel here. */
k_clear_screen();
}
void k_clear_screen() // clear the entire text screen
{
char *vidmem = (char *) 0xb8000;
unsigned int i=0;
while(i < (80*25*2))
{
vidmem[i]=' ';
i++;
vidmem[i]= 0x07;
i++;
};
};
To compile the Kernel(2nd NASM code) i used:
nasm -f elf -0 k2.o k2.asm
To compile the GCC (C) code i used:
gcc -o kernel2.o -c kernel2.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
They both compiled sucessfully!
Now i linked them using:
ld -o k2.bin k2.o kernel2.o
They linked without any errors! So i said cool! So i loaded my bootload onto my floppy, and the k2.bin after it. The bootloader loaded up and displayed it text. Next the letter A should have appeared and the screen should have cleared. Unfortunately neither happened. I know for a fact the code is being loaded and jumped to in the right part of the memory. I think its just not linking properly. Does anyone have an exmaple of how to link these two files together right (Link them so they come out in the same format as a NASM binary file) so it can work properly?
Also i have tried having the bootloader jump to a kernel written in just ASM compiled into binary and it works.
Would it be easier to take all assembly out of the kernel, and just compile the C source to an object, then Link it to flat binary? If i did that how would i call the function kmain from my bootloader?
Thanks!