I'm trying to make a simple bootloader, but running into issues with ld (I think).
When I compile my assembly file (below) with nasm -f bin
, it works and I get a nice 512 byte file. For that one I include org 0x7c00 at the top and everything works as expected.
However, now I'm trying to do something a bit more complicated and link in a C kernel (unclear if I'm on the right path there, but I'm sure I'll learn that soon). Anyway, when I compile it with nasm -f elf -o loader_elf bootloader.asm
and link it with i386-elf-ld loader_elf -o loader_exe -Ttext 0x7C00
, I get a file that is 4196 bytes (with a 1152 byte elf file).
What do I need to do for either the elf file or ld to get an executable with a proper file size?
Thanks for your help!
Here's the file in question:
bits 16 ; 16-bit Real Mode
;org 0x7c00 ; BIOS boot origin... only used for bin, not elf
global _start
Message db "Starting YADBL", 0x0
jmp _start ;Jump to start main() entry-point
;Print characters to the screen
Println:
lodsb ;Load string
or al, al
jz complete
mov ah, 0x0e
int 0x10 ;BIOS Interrupt 0x10 - Used to print characters on the screen via Video Memory
jmp Println ;Loop
complete:
call PrintNwL
;Prints empty new lines like '\n' in C/C++
PrintNwL:
mov al, 0 ; null terminator '\0'
stosb ; Store string
;Adds a newline break '\n'
mov ah, 0x0E
mov al, 0x0D
int 0x10
mov al, 0x0A
int 0x10
ret
;Reboot the Machine
AToHalt:
call GetPressedKey
; Inputted key is in al
cmp al, 'A'
jne rbt
; We got an A!!!
; mov si, Halting
; call Println
cli
hlt
rbt:
ret ; we're returning, not rebooting
;Sends us to the end of the memory
;causing reboot
;db 0x0ea
;dw 0x0000
;dw 0xffff
jmp 0xffff:0000h ; also restarts
;Gets the pressed key
GetPressedKey:
mov ah, 0
int 0x16 ;BIOS Keyboard Service
ret
;Bootloader entry-code
_start:
cli ;Clear interrupts
;Setup stack segments
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
sti ;Enable interrupts
;Print the first characters
mov si, Message
call Println
call AToHalt
times 510 - ($-$$) db 0 ;Fill the rest of the bootloader with zeros
dw 0xAA55 ;Boot signature