Skip to content

Instantly share code, notes, and snippets.

@MyCatShoegazer
Created February 2, 2018 08:05
Show Gist options
  • Save MyCatShoegazer/38dc3ee7db9627ff3a20ebdb8910f983 to your computer and use it in GitHub Desktop.
Save MyCatShoegazer/38dc3ee7db9627ff3a20ebdb8910f983 to your computer and use it in GitHub Desktop.
Hello World NASM boot loader example.
@echo on
echo Bulding bin file...
nasm loader.asm -f bin -o loader.bin
echo Writing bin file to ploppy
partcopy loader.bin 0 200 -f0
# Build bin file
nasm loader.asm -f bin -o loader.bin
# Write bin file to floppy
dd if=loader.bin bs=512 of=/dev/fd0
[BITS 16] ;Tells the assembler that its a 16 bit code
[ORG 0x7C00] ;Origin, tell the assembler that where the code will
;be in memory after it is been loaded
MOV SI, HelloString ;Store string pointer to SI
CALL PrintString ;Call print string procedure
JMP $ ;Infinite loop, hang it here.
PrintCharacter: ;Procedure to print character on screen
;Assume that ASCII value is in register AL
MOV AH, 0x0E ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00 ;Page no.
MOV BL, 0x07 ;Text attribute 0x07 is lightgrey font on black background
INT 0x10 ;Call video interrupt
RET ;Return to calling procedure
PrintString: ;Procedure to print string on screen
;Assume that string starting pointer is in register SI
next_character: ;Lable to fetch next character from string
MOV AL, [SI] ;Get a byte from string and store in AL register
INC SI ;Increment SI pointer
OR AL, AL ;Check if value in AL is zero (end of string)
JZ exit_function ;If end then return
CALL PrintCharacter ;Else print the character which is in AL register
JMP next_character ;Fetch next character from string
exit_function: ;End label
RET ;Return from procedure
;Data
HelloString db 'Hello World', 0 ;HelloWorld string ending with 0
TIMES 510 - ($ - $$) db 0 ;Fill the rest of sector with 0
DW 0xAA55 ;Add boot signature at the end of bootloader
@alimkoca
Copy link

Why 16 bit?

@MyCatShoegazer
Copy link
Author

Why 16 bit?

Because it is simple to load 16 bit code. Then you can switch CPU to 32/64 bit mode, use protected mode and etc.

@alimkoca
Copy link

Thanks

@kostiakova
Copy link

Can smbody help? How I might to call my extern C function from assembly code?

@Sid110307
Copy link

Sid110307 commented Sep 10, 2023

Can smbody help? How I might to call my extern C function from assembly code?

Just add extern functionName and call functionName wherever needed. Make sure to link the files first

@boredcoder411
Copy link

Can smbody help? How I might to call my extern C function from assembly code?

Just add extern functionName and call functionName wherever needed. Make sure to link the files first

Don't forget to setup 32 bit mode or compile your c for 16 bit, and use an appropriate linker

@DevelopeBatu
Copy link

Can smbody help? How I might to call my extern C function from assembly code?

Just add extern functionName and call functionName wherever needed. Make sure to link the files first

Don't forget to setup 32 bit mode or compile your c for 16 bit, and use an appropriate linker

Can you give linker.ld and boot.s

@boredcoder411
Copy link

No I don't know how to do it. I just know what to do, and why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment