Skip to content

Instantly share code, notes, and snippets.

@DivinityArcane
Created December 12, 2012 06:16
Show Gist options
  • Save DivinityArcane/4265421 to your computer and use it in GitHub Desktop.
Save DivinityArcane/4265421 to your computer and use it in GitHub Desktop.
A simple bootsector/OS.
; Simple NASM bootloader, or OS, if you please.
; Boots up and displays a black screen, and prints a message.
[ORG 0x7C00] ; Specify where to load the code.
msg: db 'Hello!' ; Define the msg.
len: equ $-msg ; Define the msg length.
; Create our "PRINT" function, which takes two arguments:
; 1: The message
; 2: The message length
; Note: This is a NASM macro. Any time we reference this, all it does is
; insert this code at that point, replacing %1, %2 with the arguments.
%macro PRINT 2
MOV BX, 0 ; Clear register BX.
MOV CX, 0 ; Clear the count register.
MOV BX, %1 ; Store the address of the msg.
pchar: ; Start point; label.
MOV AH, 0x0e ; Prepare to write a char.
MOV AL, [BX] ; Copy the char to AL.
INT 0x10 ; Call the interrupt to print the char.
INC CX ; Increment CX.
INC BX ; Increment BX.
CMP CX, %2 ; Compare CX to the msg length.
JNE pchar ; If CX is less, jump back to the top.
%endmacro
PRINT msg, len ; Call the print function on our msg
jmp $ ; Continuously jump to $, which is this line.
times 510 -($ - $$) db 0 ; Pad up to the 510th byte
dw 0xAA55 ; Last two bytes: 0xAA55
; This creates a 512 byte binary file, which is a valid boot sector.
; Copying this to a floppy should yeild a black screen with the output:
; Hello!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment