# OSDev.org

The Place to Start for Operating System Developers
 It is currently Sat Jan 20, 2018 1:28 am

 All times are UTC - 6 hours

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: [help]LBA to CHS functionPosted: Wed Jan 12, 2011 10:11 pm

Joined: Mon May 24, 2010 10:28 am
Posts: 10
I try to load my loader on my boot loader 32 bit with FAT32. But my LBACHS function do not work properly. LBA to CHS formula is:
absolute sector = (logical sector / sectors per track) + 1
absolute head = (logical sector / sectors per track) MOD number of heads
absolute track = logical sector / (sectors per track * number of heads)

I try to load my root directory which is locate on LBA sector 0x2000 to memory 0:0x8000.
On BPB my sector per track is 0x3f and number of head is 0xffh.

From the formula it must be C=0 H=0x82 and S=3, but my function show C=0x82 H=3 S=0x82

here is my LBACHS code
Code:
; PROCEDURE LBACHS
; absolute sector = (logical sector / sectors per track) + 1
; absolute head   = (logical sector / sectors per track) MOD number of heads
; absolute track  = logical sector / (sectors per track * number of heads)
;*************************************************************************
LBACHS:
xor     dx, dx                              ; prepare dx:ax for operation
div     WORD [A_BF_BPB_SectorsPerTrack]              ; calculate
inc     dl                                  ; adjust for sector 0
mov     BYTE [absoluteSector], dl
xor     dx, dx                              ; prepare dx:ax for operation
mov     BYTE [absoluteTrack], al
ret

Can someone tell me what's wrong on the function?

Thanks, best regards

_________________
http://osdeverdiary.blogspot.com

Top

 Post subject: Re: [help]LBA to CHS functionPosted: Thu Jan 13, 2011 2:36 am
 Member

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8201
Location: At his keyboard!
Hi,

a07859 wrote:
I try to load my loader on my boot loader 32 bit with FAT32. But my LBACHS function do not work properly. LBA to CHS formula is:
absolute sector = (logical sector / sectors per track) + 1
absolute head = (logical sector / sectors per track) MOD number of heads
absolute track = logical sector / (sectors per track * number of heads)

That should be "absolute sector = (logical sector MOD sectors per track) + 1" (which is what your code is actually doing).

From the code in your previous post, we're working with 63 sectors per track, 255 heads and a total of 0x3C3F00 sectors. It's obvious that the LBA address won't fit in AX alone, and your function should be converting a 32-bit LBA address (in either EAX or "DX:AX") " into CHS because if you use AX alone it will only support 65536 sectors (or about 32 MiB).

Also note that for the BIOS "read sectors into memory" function the cylinder number is a 10-bit number and won't fit in an 8-bit register (and therefore your "absoluteTrack" should be 16-bit).

Of course when the LBA address is 0x2000 none of the problems I mentioned above would matter (it's only when you start using this LBA->CHS conversion code as a general purpose routine used for other things, like when you realise your OS needs to be in a partition and not using the entire device).

a07859 wrote:
Can someone tell me what's wrong on the function?

Sooner or later you're going to need to setup some sort of emulator that allows you to debug your code properly (e.g. Bochs with it's inbuilt debugger, or Qemu with GDB). I'd suggest that now is the perfect opportunity for you to learn how to debug your code with this technique, as the knowledge/experience you gain with the debugger to solve this simple problem now will be extremely useful for fixing all of the (potentially more tricky) bugs you're going to have in future....

Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.

Top

 Post subject: Re: [help]LBA to CHS functionPosted: Thu Jan 13, 2011 3:43 am

Joined: Mon May 24, 2010 10:28 am
Posts: 10
Quote:
Also note that for the BIOS "read sectors into memory" function the cylinder number is a 10-bit number and won't fit in an 8-bit register (and therefore your "absoluteTrack" should be 16-bit).

I think is okay on 8 bit because the CHS of my disk is 0xff,0xff,0x3f and if I use 16 bit, int 13 for read sectors use 8 bit register for store the value of CHS (ch,cl,al)

thx,
Best Regards,

_________________
http://osdeverdiary.blogspot.com

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC - 6 hours

#### Who is online

Users browsing this forum: Bing [Bot], stlw and 15 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Operating System Development    OS Development    OS Design & Theory    Announcements, Test Requests, & Job openings Everything Else    General Programming    General Ramblings    Auto-Delete Forum OSDev.org    OSDev Wiki    About this site