second stage bootloader cant access data segments (assembly)
Posted: Tue Jul 16, 2013 11:24 am
Hi, I hope this is the right place to put my question and that my question hasn't already been asked, I'm not really sure how to describe my problem. I have an OS project that I've been planning and researching for, for the last 7 years. I've made a working HDD bootloader, it loads my second stage loader which is just the following 1024 bytes on the hdd (so the second 2 sectors). I'm using bochs emulator, and fasm for compiling, I intend this to be a wholey asm os, the second stage loader runs in 16bit mode and is intended to setup the hardware as desired and load the kernel (which I have yet to make). the second stage program loads fine, and runs.. but in the second stage program it has a db string at the end that just stores "System Loaded" with a zero byte terminator. It's intended to print this after clearing the screen and setting the cursor position (which all work fine) but it doesn't print the string, it prints some binary from somewhere in memory (this is shown in the image attached below) the code I have is as follows.
G:/system files/ring0/boot/level_one_boot(hdd).asm
G:/system files/ring0/boot/level_two_boot.asm
I apologize if this has already been answered, it seems to work except for the print routine not printing what it's supposed to.
My guess is an addressing problem but I can't find it.
As for the screenshot the characters it's printing out it's almost like the computer is showing a confused emoticon... lol
Screenshot:
G:/system files/ring0/boot/level_one_boot(hdd).asm
Code: Select all
use16
org 0x7C00
;setup registers
xor ax,ax
mov ds,ax
mov es,ax
cli
mov ss,ax
mov sp,0x7C00
sti
;main
call ClearScreen
mov bx,00001101b;purple on black
mov [xpos],4d;xpos
mov [ypos],2d;ypos
call SetCursorPos
mov si,bootstrapname
call PrintText
mov [xpos],4d;xpos
mov [ypos],3d;ypos
call SetCursorPos
mov si,loadingmsg
call PrintText
mov [xpos],0
mov [ypos],5
call SetCursorPos
;begin bootloader
call LoadStage2
cmp [errorflag],0
;check if we successfully loaded the second stage program
jg halt
;if successful, cache the drive number we're loading from and jump to the program
mov dl,80h
push dx
jmp 0x2000:0x0000 ;jump to loaded program
;handle boot failure
halt:
mov bx,00001111b;white on black
mov [xpos],4d;xpos
mov [ypos],5d;ypos
call SetCursorPos
mov si,haltexception
call PrintText
mov [xpos],0
mov [ypos],7
call SetCursorPos
hang:
jmp hang
;routines
ClearScreen:
pusha
mov ah,00h
mov al,03h
int 10h
popa
ret
SetCursorPos:;dl = xpos, dh = ypos
pusha
mov ah,02h
mov dl,[xpos]
mov dh,[ypos]
int 10h
popa
ret
PrintText:;si = string start position
pusha
mov ah,09h
mov bh,00h
mov cx,01h
.dochar:
lodsb
or al,al
jz .return
int 10h
add [xpos],1d
call SetCursorPos
jmp .dochar
.return:
popa
ret
LoadStage2:
pusha
mov bx,0x2000 ;segment
mov es,bx
mov bx,0x00 ;offset
mov [counter1],1
.tryread:
mov ah,02h
mov al,2d
mov ch,00h
mov cl,02h
mov dh,00h
mov dl,80h
int 13h
jc .tryagain
mov bx,00000101b;purple on black
mov [xpos],4d;xpos
mov [ypos],3d;ypos
call SetCursorPos
mov si,donemsg
jmp .fin
.tryagain:
add [counter1],1
cmp [counter1],3
jg .failed
jmp .tryread
.failed:
mov bx,00000100b;red on black
mov [xpos],4d;xpos
mov [ypos],3d;ypos
call SetCursorPos
mov si,errormsg
.fin:
call PrintText
mov [xpos],0d;xpos
mov [ypos],5d;ypos
call SetCursorPos
popa
ret
;data section
bootstrapname db "Wolf Bootloader ",0
loadingmsg db "Loading System... ",0
errormsg db "Failed to Load System",0
donemsg db "System Loaded... ",0
haltexception db "Loader Halted! ",0
xpos db 0
ypos db 0
errorflag db 0
counter1 db 0
;bootloader specifics
times 510-($-$$) db 0
dw 0xAA55
Code: Select all
use16
org 0x0000
;retrieve active drive number, assuming the first bootloader cached it for us
pop dx
mov [drivenumber],dl
xor dx,dx
;reset registers
xor ax,ax
mov ds,ax
mov es,ax
cli
mov ss,ax
mov sp,0x2000
sti
;main
call ClearScreen
mov bx,00000101b;purple on black
mov [xpos],4d;xpos
mov [ypos],2d;ypos
call SetCursorPos
mov si,loadedmsg
call PrintText
mov [xpos],0d;xpos
mov [ypos],5d;ypos
call SetCursorPos
halt:
jmp halt
ClearScreen:
pusha
mov ah,00h
mov al,03h
int 10h
popa
ret
SetCursorPos:;dl = xpos, dh = ypos
pusha
mov ah,02h
mov dl,[xpos]
mov dh,[ypos]
int 10h
popa
ret
PrintText:;si = string start position
pusha
mov ah,09h
mov bh,00h
mov cx,01h
.dochar:
lodsb
or al,al
jz .return
int 10h
add [xpos],1d
call SetCursorPos
jmp .dochar
.return:
popa
ret
loadedmsg db "System Loaded",0
xpos db 0
ypos db 0
drivenumber db 80h ;edit: was dw, should be db (an error when pasting)
times 1024-($-$$) db 0
My guess is an addressing problem but I can't find it.
As for the screenshot the characters it's printing out it's almost like the computer is showing a confused emoticon... lol
Screenshot: