Skip to content

Instantly share code, notes, and snippets.

@qookei
Created February 4, 2020 23:18
Show Gist options
  • Save qookei/eed92ff22ee7fa02803b29e599c680e9 to your computer and use it in GitHub Desktop.
Save qookei/eed92ff22ee7fa02803b29e599c680e9 to your computer and use it in GitHub Desktop.
DOS qword loader
; compile with: nasm -fbin bootqwrd.asm -o bootqwrd.com
; note: the DOS extender nasm uses causes this to triple fault
; when ran right after assembling
; usage: put a flat binary to be loaded at 1MB called QWORD.BIN in
; the current directory and run bootqwrd.com
; tested in qemu with DOS 6.22
org 0x100
bits 16
start:
mov dx, welcome
mov ah, 0x09
int 0x21
mov ah, 0x3D
xor al, al
mov dx, qword_file
xor cl, cl
int 0x21
jc .err
mov bx, ds
add bx, 0x1000
push bx
push ax
push ds
pop ax
add ax, 0x1000
push ax
.loop:
pop ds
pop bx
push bx
push ds
mov ah, 0x3F
xor dx, dx
mov cx, 0x2000
int 0x21
jc .err
cmp ax, 0x2000
jl .loaded
pop ax
add ax, 0x200
push ax
jmp .loop
.loaded:
pop bx
add sp, 2
pop dx
push ax
push bx
cli
mov ax, cs
mov ds, ax
xor ax, ax
mov es, ax
mov di, 0x550
mov word [es:di], 0xe3ff ; es:di -> jmp ebx
; memcpy(0000:0500, ds:gdt, 24)
mov di, 0x500
mov si, gdt
mov cx, 24
rep movsb
mov ebx, cs
shl ebx, 4
add ebx, .prot
pop cx
pop si
lgdt [es:0x500]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 0x08:0x550
.prot:
bits 32
mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov gs, ax
mov fs, ax
shl ecx, 4
shl edx, 4
mov ebx, esi
mov esi, edx
sub ecx, edx
add ecx, ebx
mov edi, 0x100000
rep movsb
mov eax, 0x100000
jmp eax
.err:
push es
pop ds
mov dx, err
mov ah, 0x09
int 0x21
.end:
mov ah, 0x4C
int 0x21
qword_file:
db "QWORD.BIN", 0
err:
db "An error occured$"
welcome:
db "Loading qword.$"
gdt:
dw 23
dd 0x500
dw 0
dq 0x00cf9a000000ffff
dq 0x00cf92000000ffff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment