a simple bootloader + loading a program

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
zdebel
Posts: 4
Joined: Sun May 27, 2007 11:45 am

a simple bootloader + loading a program

Post by zdebel »

Hey there, I wrote a simple boot loader which looks like this:

Code: Select all

org 7C00h
start:
mov ah, 2
mov al, 2
mov ch, 0
mov cl, 2
mov dh, 0
mov bx, 0800h
mov es, bx
xor bx, bx
int 13h
jmp 0800h:0000h
times 510 - ($ - start) db 0
dw 0AA55h
I write it to the MBR of the floppy which I boot and the bootloader tries to run this code:

Code: Select all

;a test program for zdio_b.asm and zdgfx_b.asm xD
segment .data
welcome db "UZ-DOS Version 0.01",13,10,0
copyright db "Copyright (c) UZ-Developement 2006, 2006-2007",13,10,0 ;it just looks cool xD
testmsg db 13,10,"intget/intprint test, input a number < 4294967296",13,10,13,10,0
br db 13,10,0
prompt db "Z:\HELL>",0
text db "You have input this number: ",0
segment .bss
stringzor resb 128
segment .text
code:
call clrscreen
mov si,welcome
call strprint
mov si,copyright
call strprint
mov si,testmsg
call strprint
mov si,prompt
call strprint
call intget
mov eax,ecx
mov si,br
call strprint
mov si,text
call strprint
call intprint
%include "zdio_b.asm"
%include "zdgfx_b.asm"
Now the thing is, it reads a string which is then converted to a real number, and then it's again converted to a string and printed out, I'm just trying to test my functions on a real machine. The read/print mechanism works, the number gets displayed, but there's a problem with those strings, they are just garbage, like they weren't loaded into the memory. If someone could tell me what I'm doing wrong or what I'm missing I'd be very grateful.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

well, i see several potential problems:

1) your not checking to see if an error occurred you just assume it loaded correctly and jump to where it was supposed to be loaded to

Code: Select all

xor bx, bx
int 13h
jmp 0800h:0000h 
fix:
if there was a problem with loading the sector, the carry flag will be set, so test the carry flag after your 'int 13h'

2) it is quite common for a read request (from int 0X13) to fail, it is usually recommended that you try to read it at least 3 times before giving up

fix:
fix problem 1 first, then add code to retry if it fails the first 3-4 times, also, try reseting first (that is int 13, AH==0)

3) your 'org' statement implys that you assume your bootsector is loaded at 0:7C00, while this will be true most of the time, it wont be on some computers (and DS doesnt even necessarily point to the bootsector code at all)

fix:
not important since you dont actually do anything (no near JMP/CALL, and no data references) -- but make sure you understand this issue, also see problem #4

4) you never set DS to anything (in the code you posted -- see problem #5), on bootup, DS isnt required to be set to anything sane, and there is no way to know what your offsets need to be to read it, you are setting ES, but not DS

fix:
set DS to something (preferably, in your bootsector, set it to 0 or 7C00, and in your loaded sector, right at the beginning, reset it to 0X800)

5) you didnt post the code that actually does the work thats not working, this is more of a problem for us, since we cant help you with code you havent given us access to...

fix:
although this time i think i covered your problem, keep in mind for next time, that we cant help you with code you havent given us access to





i hope this helps you
zdebel
Posts: 4
Joined: Sun May 27, 2007 11:45 am

Post by zdebel »

I'll sure try to do what you've suggested, but I'm certain that at the current state it loads just fine, as the CODE is executed as expected, only the declared strings are a mess, like if they weren't loaded into memory, maybe it's related to me not setting DS... but I'll need to read up on that :oops:
-------
whoa, push cs pop ds fixed the strings, but I still need to understand this... :oops:
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

i really suspect its DS thats the problem

DS probably is set to 0 on many systems (but it really could be anything), i dont see an 'org' on your loaded sector, so im assuming there isnt one (which normally defaults to org 0 unless your linker is told another address, but you didnt specify that...), so:

DS.base = 0
org = start = 0
welcome = offset from start = 0

when it tries to read welcome message from memory it reads it at:
DS.base+start+welcome
or
0+0+0 = 0

so its printing your IDT instead, you really should set DS

all data references (with a couple exceptions, or when an override is used), use DS to point to the base, from which it takes an offset

so the assembler will give it an offset, but that is from the segment base, rather than an absolute address, so your DS needs to point to the begining of the area you are using (or another value which includes the data, but then you need to tell your assembler what the starting offset is -- in assembly this is usually done with a org statement)



when you push CS pop DS, your pushing the 0x800 which you loaded into CS with your JMP 0x800:0, onto the stack, then taking it off the stack into the DS register -- this will work fine as long as you know what its doing and why, and you know what CS is -- and only when your in RMode (in PMode you need a different selector)

this will not work in your bootsector though, because you dont know what CS is (unless you start that with a JMP FAR -- some people do), as i said though, you currently dont use DS in your bootsector so it doesnt matter, but if you ever reference any data in your bootsector, you will have the same problem, and PUSH CS/POP DS will not fix it there (since you have an org 7C00, you need your DS.base to be start-7C00 -- for your bootsector this is 0, so it might work on some computers and not on others)


hope this helped you to understand, if you still have questions, ask something more specific and ill try to answer if i can
zdebel
Posts: 4
Joined: Sun May 27, 2007 11:45 am

Post by zdebel »

thank you for such a nice explanation, I corrected it and manually set ds using mov and ax :)
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

if you dont have the intel manuals, you should get them, as they will be invaluable especially when you get to the more advanced stuff and setting up PMode and paging and such

"Intel® 64 and IA-32 Architectures Software Developer's Manual" volumes 1 - 3 -- which is 5 books (2 and 3 are 2 parts each)

get all 5 books, there is a link in my signature to the intel site to download the PDF versions, and on that page, there is also a link to get hard copies -- which i definitely recommend -- intel will ship all 5 books to you free of charge


edit:
oh and welcome to OSdev.org!
zdebel
Posts: 4
Joined: Sun May 27, 2007 11:45 am

Post by zdebel »

Thank you very much for this info :D
Post Reply