Bootloader doesn't get to halt instruction

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
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Bootloader doesn't get to halt instruction

Post by nekros »

Ok, So I've been working on my bootloader lately. I tested it on qemu for the first time, and it did absolutely nothing.
I have this code at the beginning:

Code: Select all

begin:
mov sp,stackstart
push cs; THANKS TROY!
pop ds
push cs
pop es    ;done setting segments
mov BYTE[drive],dl     ; store the boot drive
;;;;;;;;;;;;;;;Load next 3 Sectors To complete stage1;;;;;;;;;;;;;;;;;;
mov ah,0x08
mov dl,BYTE [drive] 
int 0x13        ; get drive parameters
jc FAIL     ;did this faiL?
;else store info for LBA TO CHS
Really nothing here that should hold anything up. And FAIL is supposed to print a string and then hlt. (I haven't used my readsector code yet).
After that is this section of code:

Code: Select all

mov BYTE [HPC], dh ;//store heads per cylinder 

mov ax,0 ;; start counter for all the bits

testandset: ; tests and sets all the sector bits in cl
bt cx,ax     ; test bit
jc set       ; is the bit set? if yes set it in SPT
inc ax    ; if no go to next bit and test set
cmp ax,6
je continue ;if we are finished, continue with the rest of  the code
jmp testandset


set:
bts WORD [SPT],ax
inc ax
cmp ax,6
je continue ;if done setting, continue
jmp testandset ;we are not done
Continue does the same thing as FAIL, I think their might be something wrong with the loop above and the loop never ends. Should I use test instead of cmp?

EDIT:
I tried using test, it doesn't change anything.

EDIT2:
I bet I'll be facepalming soon.
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Bootloader doesn't get to halt instruction

Post by Love4Boobies »

Erm, why the heck don't you

Code: Select all

PUSH CS
POP DS
MOV ES,DS
instead of pushing CS twice?

And HEADS PER CYLINDER? ROFL! Dude, I'm not sure OSDev is the right thing for you :shock:
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Bootloader doesn't get to halt instruction

Post by Troy Martin »

Heh, I was thinking quickly at the time (remembered the opcode, wasn't thinking "optimizations" xD)
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Re: Bootloader doesn't get to halt instruction

Post by nekros »

LBA to CHS formula I got from wikipedia. I'm going to bonafide to check if they are different...
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Re: Bootloader doesn't get to halt instruction

Post by nekros »

Wikipedia:

CYL = LBA / (HPC * SPT)
TEMP = LBA % (HPC * SPT)
HEAD = TEMP / SPT
SECT = TEMP % SPT + 1
Bonafide:
Sector = (LBA/SectorsPerTrack) Remainder value + 1
Cylinder = (LBA/SectorsPerTrack)/NumHeads (Take Remainder value)
Head = (LBA/SectorsPerTrack)/NumHeads (Take quotient value)
\

That still doesn't explain why I don't get to the print and then halt instructions.
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Re: Bootloader doesn't get to halt instruction

Post by nekros »

I'm starting to think that there is a problem with qemu. Since I switched the print string call to the start and it only prints one character.... The code for printing a string is from the babystep2 tutorial. I've used that little bit of code in the past and it worked so somethings not right here...

EDIT:
Wrong again, I tested it on real hardware...
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Re: Bootloader doesn't get to halt instruction

Post by nekros »

I now know that something is causing a halt. I tried executing the print string function twice and I only got one character. It's not the assembler (I tried nasm), what would cause a halt then?
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Re: Bootloader doesn't get to halt instruction

Post by nekros »

I found the bug, it was setting sp to stackstart. I changed it to (0x7c00-1) and it worked. I don't understand why though, so if anyone has an answer please tell me...
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Bootloader doesn't get to halt instruction

Post by JohnnyTheDon »

You always need to set the stack pointer to the end of the stack, not the start.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Bootloader doesn't get to halt instruction

Post by JohnnyTheDon »

Love4Boobies wrote:Erm, why the heck don't you

Code: Select all

PUSH CS
POP DS
MOV ES,DS
instead of pushing CS twice?

And HEADS PER CYLINDER? ROFL! Dude, I'm not sure OSDev is the right thing for you :shock:
That code results in:

Code: Select all

[john@john-arch ~]$ yasm -f bin test.s
test.s:5: error: invalid combination of opcode and operands
So unless yasm sucks that bad, I don't think you get to tell him to stop doing os dev.
User avatar
nekros
Member
Member
Posts: 391
Joined: Wed Mar 05, 2008 9:10 pm
Contact:

Re: Bootloader doesn't get to halt instruction

Post by nekros »

I officially facepalm myself for missing that...
Oh well, back to happily coding until another facepalm...
Last edited by nekros on Tue Mar 03, 2009 8:54 pm, edited 1 time in total.
Working On:Bootloader, RWFS Image Program
Leviathan: http://leviathanv.googlecode.com
Kernel:Working on Design Doc
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Bootloader doesn't get to halt instruction

Post by JohnnyTheDon »

Don't worry, every time I start a bootloader I make that mistake :P
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Bootloader doesn't get to halt instruction

Post by Love4Boobies »

I was referring to the thing with the heads per cylinder :?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Bootloader doesn't get to halt instruction

Post by xenos »

Love4Boobies wrote:Erm, why the heck don't you

Code: Select all

PUSH CS
POP DS
MOV ES,DS
MOV ES, DS not a valid instruction. You can't move the content of one segment register into another. But this should work:

Code: Select all

MOV AX, CS
MOV DS, AX
MOV ES, AX
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Bootloader doesn't get to halt instruction

Post by Love4Boobies »

berkus wrote:
Love4Boobies wrote:I was referring to the thing with the heads per cylinder :?
The number of heads is indeed counted per cylinder. Think for a minute what a cylinder is.
No, it's actually the other way around.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply