Linking C and ASM files
Posted: Mon Feb 07, 2011 12:55 am
I have been studying theory for a while to make my own OS, so now I am at the very beggining of this.
I have made my own bootloader, wich boots from a CD correctly. Then, it loads a program off the disk and jump to it.
My problem is that I can run a program written in asm, but once it comes to C, Im not able to compile/assemble/link everything in the right way. I always end up with some error, eg. ld:cannot perform PE operations on non-PE file.
So, my question is, could someone explain me clearly how to do this ?
I am working under Windows with Cygwin.
boot.asm
loader.asm
kernel.c
I have made my own bootloader, wich boots from a CD correctly. Then, it loads a program off the disk and jump to it.
My problem is that I can run a program written in asm, but once it comes to C, Im not able to compile/assemble/link everything in the right way. I always end up with some error, eg. ld:cannot perform PE operations on non-PE file.
So, my question is, could someone explain me clearly how to do this ?
I am working under Windows with Cygwin.
boot.asm
Code: Select all
[ORG 0]
jmp 07C0h:start ; Goto segment 07C0
start:
; Update the segment registers
mov ax, cs
mov ds, ax
mov es, ax
reset: ; Reset the floppy drive
mov ax, 0 ;
mov dl, 0 ; Drive=0 (=A)
int 13h ;
jc reset ; ERROR => reset again
read:
mov ax, 1000h ; ES:BX = 1000:0000
mov es, ax ;
mov bx, 0 ;
mov ah, 2 ; Load disk data to ES:BX
mov dl, 0 ; Drive=0
mov dh, 0 ; Head=0
mov ch, 0 ; Cylinder=0
mov cl, 2 ; Sector=2
mov al, 5 ; Load 5 sectors
int 13h ; Read!
jc read ; ERROR => Try again
program:
jmp 1000h:0000 ; Jump to the program
times 510-($-$$) db 0
dw 0AA55h
Code: Select all
global loader
extern _kmain ; kmain from kernel.c
section .text
align 4
; reserve initial kernel stack space
STACKSIZE equ 0x4000 ; that's 16k.
loader:
mov esp, stack+STACKSIZE ; set up the stack
mov al, 'R'
mov ah, 0Eh ; Print AL
mov bx, 7
int 10h
push eax ; pass Multiboot magic number
push ebx ; pass Multiboot info structure
call _kmain ; call kernel proper
cli
hang:
hlt ; halt machine should kernel return
jmp hang
section .bss
align 4
stack:
resb STACKSIZE ; reserve 16k stack on a doubleword boundary
Code: Select all
void kmain( void* mbd, unsigned int magic )
{
unsigned char *videoram = (unsigned char *) 0xb8000;
videoram[0] = 65;
videoram[1] = 0x07;
}