Skip to content

Instantly share code, notes, and snippets.

@ssg
Created August 24, 2010 00:22
Show Gist options
  • Save ssg/546634 to your computer and use it in GitHub Desktop.
Save ssg/546634 to your computer and use it in GitHub Desktop.
My own operating system bootloader
; ESCape Bootstrap-Loader v0.04 beta - (c) 1994 SSG
; This is a bootstrap loader I coded for one of my own "operating system" projects.
; It didn't go much farther than the loader and some init code anyways.
model tiny
.386
cseg segment use16
org 7c00h
bufferoffset equ 0fe00h
assume cs:cseg,ds:cseg,es:cseg,ss:cseg,fs:cseg,gs:cseg
jmp short @Init
@Sides: db 2
@SPT: db 18
@Tracks: dw 80
@RootDir: dd 9 ;root-directory pointer
@Init:
mov ax,3 ;Clear-screen
int 10h
cld
mov ax,cs ;Initialize segments
mov es,ax
mov ds,ax
mov si,offset @InitText ;Load hot-dir
call @WriteStr
mov edi,dword ptr @RootDir
mov si,7e00h
call @LoadBlock
mov al,byte ptr [si+20h] ;Check if is it a directory
cmp al,1
jne @Error
call @WriteStr
mov si,offset @NewLine
call @WriteStr
mov si,7e00h
mov edi,dword ptr [si+22h]
@StartSearch:
mov si,7e00h ;Init search system
call @LoadBlock
mov si,offset @SearchText
call @WriteStr
mov si,7e00h
lodsd
mov dword ptr @NextBlock,eax
mov cx,127
@SearchLoop:
lodsd ;if directory entry empty
or eax,eax
je @Continue
push si
push cx
mov edi,eax
mov si,8000h
call @LoadBlock ;loading file header
mov di,si
pop cx
pop si
mov ebx,dword ptr @cmp1 ;checking file name
mov edx,dword ptr [di]
cmp ebx,edx
jne @Continue
mov ebx,dword ptr @cmp2
mov edx,dword ptr [di+4]
cmp ebx,edx
jne @Continue
mov al,byte ptr cs:8020h ;checking if is it a file
or al,al
jne @Error
jmp short @Execute
@Continue:
loop @SearchLoop ;looking for next entry
@NotFound:
mov edi,dword ptr @NextBlock ;loading next hot-dir block
or edi,edi
jne @StartSearch
@Fail:
mov si,offset @FailText
jmp @ErrorWrite
@LoadPtr:
dw 0
@Execute:
call @OK
mov si,offset @ExecText
call @WriteStr
mov si,8000h
mov edi,dword ptr [si+22h]
shr si,4
mov ax,si
mov es,ax
mov ds,ax
@LoadLoop:
mov si,bufferoffset
call @LoadBlock
push dword ptr [si]
add si,4
mov cx,127
mov di,word ptr cs:@LoadPtr
rep movsd
movsw
add word ptr cs:@LoadPtr,508
pop edi
or edi,edi
jne @LoadLoop
@AtLast:
call @OK
db 0eah
dw 0,800h
@WriteStr:
@Loop:
lodsb
or al,al
je @Ret
mov ah,0eh
mov bx,7
int 10h
jmp short @Loop
@Ret:
ret
;convertblock
;input: edi block number
;return: ax 0201h (read 1-sector code)
; cx track no & sector start
; dl 0 (boot drive)
; dh head no
@ConvertBlock:
xor bh,bh
xor ah,ah
xor ch,ch
mov al,byte ptr cs:@Sides
mov bl,byte ptr cs:@Spt
mul bl
mov bx,ax
mov eax,edi
div bx
mov bx,ax
xchg bh,bl
shl bl,6
xor edx,edx
mov cl,byte ptr cs:@Spt
mov eax,edi
dec eax
cwd
idiv cx
inc dx
or bl,dl
xor dx,dx
mov eax,edi
mov cl,byte ptr cs:@Spt
div cx
xor dx,dx
mov cl,byte ptr cs:@Sides
cwd
idiv cx
mov dh,dl ;dx eliminated
xor dl,dl
mov cx,bx ;cx eliminated
mov ax,0201h ;ax eliminated
inc cl
ret
@LoadBlock:
xor bh,bh
mov ax,word ptr cs:@Tracks
mov bl,byte ptr cs:@Sides
mul bl
mov bl,byte ptr cs:@Spt
mul bx
shl eax,16
mov ax,dx
rol eax,16
cmp edi,eax
ja @Error
mov byte ptr cs:@Tries,3
@TryLoop:
call @ConvertBlock
mov bx,si
int 13h
jnc @Ret
dec byte ptr cs:@Tries
jnz @TryLoop
@Error:
mov ax,cs
mov ds,ax
mov si,offset @ErrorText
@ErrorWrite:
call @WriteStr
xor ax,ax
int 16h
int 19h
@OK:
mov ax,cs
mov ds,ax
mov si,offset @OKText
call @WriteStr
ret
@ErrorText:
db 'error',0
@Tries:
db ?
@FailText:
db 'nosys',0
@InitText:
db 'init: ',0
@OKText:
db 'ok'
@NewLine:
db 0dh,0ah,0
@SearchText:
db 'srch: ',0
@ExecText:
db 'exec: ',0
@NextBlock:
dd ?
@cmp1:
dd 2E435345h
@cmp2:
dd 74696E69h
ends cseg
end
@yigitgenc
Copy link

baskanim onunde saygiyla egiliyorum. bootloader yazan adamin onunde egilinir, hem de 1994'te. masallah.

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