## LBA to CHS

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

nikoj
Posts: 8
Joined: Sat Oct 04, 2008 11:59 pm

### LBA to CHS

http://www.brokenthorn.com/Resources/OSDev11.html

this tutorial confused me about tracks.in the tutorial the track is calculated with the formula:
absolute track = logical sector / (sectors per track * number of heads)
and it is written that 20-th sector "live" in sector 2, head 1, track 2.

if we use the formula, then the 20-th sector should be in sector 2, head 1, track 0.

is it a typo or i m wrong?

thanks

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:
Hi,

You seem to have some of your terminology confused.

Cylinder and Track mean almost the same thing, apart from the fact that Track refers to the current disk you are on whereas cylinder refers to all the 'same' tracks across all the disks (only applicable for Hard Disks which have more than 1 disk - ignore this for floppy drives)

Other than that, I would agree with you that this is probably a typo

Andrew

Mike
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:
if we use the formula, then the 20-th sector should be in sector 2, head 1, track 0
How did you get that?

Code: Select all

absolute track = logical sector / (sectors per track * number of heads)

absolute track = 20 / (18 * 1)
absolute track = 20 / 18
absolute track = 1.1111111111111111111111111111111
...or, absolute track==1
and it is written that 20-th sector "live" in sector 2, head 1, track 2.
That looks like a typo track should be 1 not 2. I'll see if I can fix that...

nikoj
Posts: 8
Joined: Sat Oct 04, 2008 11:59 pm
another typo

absolute track = 20 / (18 * 2)
absolute track = 20 / 36
absolute track = 0.555555555555555555555555555556
so track = 0
we can test this formula for correctness:

First we read track 0 head 0 sector 1 until we reach sector 18.
Next we read track 0 head 1 sector 1 until we reach sector 18, and so on...

here is the table

So our 20th sector should be in track 0 head 1 sector 2
Track 0 Head 0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Track 0 Head 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Track 1 Head 0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
......
i guess now i m right

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:
Seconded - I agree with nikoj

Mike
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:
You are correct - Ill see if I can fix those when I have the time I actually want to update that tutorial as it has been skipped in the last revision as well.

nikoj
Posts: 8
Joined: Sat Oct 04, 2008 11:59 pm
huh, another dilemma

what if the logical sector is 36?
absolute sector = (logical sector / sectors per track) + 1

Code: Select all

LBA_to_CHS:

xor dx, dx
div word [SectorsPerTrack]  ;36/18 = AX =2 DX =0
push ax
mov cl, dl
pop ax
xor dx,dx
div word [Sides]
mov dh, dl
mov ch, al
mov dl, byte [drive_number]

ret
at the end... sector = 1 track=1 head = 0

and according the above table, this is wrong...