Help! did I load something?

Programming, for all ages and all languages.
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Help! did I load something?

Post by Tjaalie »

Dear thread reader,

I'm working on my little toy os. And now I'm trying to load a few sectors after the first sector on my boot disk containing a little bootfs (custom format). The idea is that this way I can write different boot loaders for different media and just drop this data after the boot loader (or maybe even in the fs itself) and then let the second stage (in the bootfs) just parse the ram and load the kernel, this way it doesn't have to touch any drives and can be totally independent of the media it is loaded from. After the kernel is loaded it can load his disk drivers and access the drive as a normal drive. Now back to my question, I write a little piece of asm code just to load a single sector into memory. But now I have no idea if it worked and I got the feeling it didn't work (because the first four bytes should be 'xefs' but when I try to print those chars to the screen nothing shows up). Now I'm horrible when it comes to asm so the code might be totally wrong. What I try to achieve is this. AX contains the sector to load, next I try to extract the head, track and sector number from this and then load this sector at the desired location 0x1000:0x0. Here is the code:

Code: Select all

;;rest floppy drive
fdReset:
	mov ah, 0
	mov dl, 0
	
	int 0x13
	jc fdReset
	
	ret
	
;;read a sector from the disk
;;es:bx address to read to
;;ax sector to read
fdRead:

	;;select head
	mov dh, 0
	cmp ax, 1440
	jl fdRead_headset
	
	sub ax, 1440
	mov dh, 1
	
fdRead_headset:

	mov cx, ax
	mov al, 80
	div cx
	mov ch, al
	mov cl, ah
	inc ch
	inc cl

	mov ah, 0x02
	mov al, 0
	mov dl, 0
	
	int 0x13
	
	ret


And here is the code that calls that code

Code: Select all

;;rest floppy drive
call fdReset
	
;;read xefs headers
mov ax, 0x1000
mov es, ax
mov bx, 0
mov ax, 1
call fdRead
If I'm correct this should load the second sector from my floppy disk to memory?

Thanks in advance
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Help! did I load something?

Post by Troy Martin »

Interrupt 13h will set the carry flag on error, you can use that to determine if it loaded or not.
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
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

Thanks

So this will show a message if it fails (printStr prints a 0 terminated string that is pointed to by si)

jc ERROR

ERROR:
mov si, strError
call printStr

I want to thank this forum, it only took a few minutes before some with knowledge answered :D

EDIT:

Yes it gives me a error (if the above code works:P), so anyone has an idea on what is going wrong?
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

Can someone with some knowledge of assembler look at my math instructions?
Because I'm not sure div does what I think it does. And I can't seem to find any good info on how to use those instructions. Google gives me all kind of hits, mostly code that works but I can't figure out what it actually does.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Help! did I load something?

Post by 01000101 »

I'm very unsure of what you're trying to accomplish with the DIV instruction.

Currently you load AX with 1, and then set AL (lower byte) to 80 which results in 0x0050. Then you divide that by what was in AX to begin with (1) to result in AL = 0x50 (as 0x50/1 is 0x50) and AH = 0 (remainder).

At the end of this, you end up with CH (low 8 bits of the cylinder) with 0x51 and the sector number (CL) as 1.

DIV info: http://www.intel.com/Assets/PDF/manual/253666.pdf and http://www.laynetworks.com/assembly%20t ... .htm#arith
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

Let my explain what I was doing instead of just showing my code witch is so not working:P
If you don't mind I will use C/C++/pseudocode to explain this because that's what I'm most familiar with.

Code: Select all

linearsector = 1; //we want the second sector

//find out witch head to use
head = 1
if (linearsector >= 1440 )  //1440 sector per head right?
{
    head = 2;
    linearsector -= 1440;
}

//find track to use
track  = linearsector/80; // 80 sectors per track right?
track++; //starting at track 1
sector = linearsector%80; //starting at 0 right?

//then int 0x13 with
head = 1
track = 1
sector = 1
I'm puzzled with why the head and track count start at 1 and the sector count start at 0? Or is this not right.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Help! did I load something?

Post by 01000101 »

you should check out this if you haven't already.

There are normally 18 sectors per head on a standard 1.44M floppy. So, linear 0 is at head(0)sector(1) and linear 18 is at head(1)sector(1). Then, there are usually 2 heads per cylinder, so linear 18 is at cylinder(0)head(1)sector(1) and linear 36 is at cylinder(1)head(0)sector(1).
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

Oke, and now you lost me. I was under the impression that there where 80 tracks and that tracks and cylinders where the same thing but different words. And 2 heads, the top and bottom of the magnetic circle in the disk. So if there would only be 18 sectors on a head that would result in 18*512 = 9kB of memory?
User avatar
xDDunce
Member
Member
Posts: 173
Joined: Tue Aug 12, 2008 4:04 pm
Contact:

Re: Help! did I load something?

Post by xDDunce »

can i point you to the brokenthorn bootloader tutorials? this one in particular

also, a small programming tip: if you can't get it to work in code, write out the algorithm in plain english. it's much easier to understand a primary language and translate into a programming language, than it is in reverse. besides the fact that algorithms are a necessity when it comes to implementing bigger projects, in order to understand everything that is going on.

lastly, can i just point out to E that if AX=0x0001 followed by AL=0x50. DIV AX, AL gives 1 (0x0050/0x0050) so cylinder will be 1. and this will occur until AH is given a value other than 0x00. therefore, only when you want cylinder number 256 will the outcome be any different (and then wrong anyway).

@OP: re-think all of your code. read int 0x13 of ralf brown's interrupt list for more detail on that interrupt if your unsure, and check the link above for information on disk reading/layout.

Cheers,
James.
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

Oke I think I found something. Is the brokenthorn tutorial right? Because when I try to load a sector using this code:

Code: Select all

	mov		ah, 0x02				; read floppy sector function
	mov		al, 1					; read 1 sector
	mov		ch, 1					; we are reading the second sector past us, so its still on track 1
	mov		cl, 1					; sector to read (The first sector)
	mov		dh, 1					; head number
	mov		dl, 0					; drive number. Remember Drive 0 is floppy drive.
	int		0x13
Now when I jump to the address 0x1000:0x0 nothing happens (it is loaded at that address).
Now when I change the track and head number to 0 it restarts my bootloader (so it loads the first sector at the address). But when I change the sector to read to 2 it loads nothing:S. Does anyone have a idea whats wrong here?

EDIT:

When I search the net I seem to find tutorials doing both, using 0 as the first track/cylinder or using 1 as the first one. They all seem to use 1 as the first sector, its just the track/cylinder index that seems to be different. What's wrong with my code? I mean it's not that I'm trying to do something rockedscienceish here? Most of the people on this forum must have written code like this that works?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Help! did I load something?

Post by Troy Martin »

Google CHS to LBA for the formulas you need to convert an absolute sector number to cylinder/head/sector. Use those formulas to write a simple call for said formulas. It should be about ten or twelve lines of code.
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
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

Yeah I found that, but that's my problem until I can actually load something:P. Since I'm only able to load the first sector on disk somehow the second sector won't load. Right now I'm going to see if what AL contains after reading (since the carry flag is not set) if it's zero there is something really wrong.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Help! did I load something?

Post by Troy Martin »

For god sakes, find a hex editor or use MS-DOS DEBUG.EXE to make sure there actually is crap in the second sector :|

EDIT: Or partcopy:

Code: Select all

partcopy -f0 200 200 sector2.bin
For a disk image, replace -f0 with the image name.
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
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help! did I load something?

Post by Tjaalie »

I did that already, there is crap in the second sector. Now it seems that it won't even load the first sector. It's just that bochs sets all memory to 0 so it will nop all over it until it reaches the boot loader.

I'm using linux btw, not that it matters. And I know how to work with 'dd'.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Help! did I load something?

Post by Troy Martin »

Uhh, 00 isn't a nop, 90 is. 00 00 is "add byte [bx+si],al". You need to actually grasp assembly 'fore attempting to write an OS.

And the BIOS starts at F000:FFF0, not 0. you'd have to nop the entire memory, including the IVT, BDA, and video memory, not to mention other shizz just to spend all the time switching CS and stuff to get to the reset vector.
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
Post Reply