I am referring BrokenThorn's OS development tutorial, and currently reading the part on developing a complete first stage bootloader that loads the second stage - Bootloaders 4.
In the part of converting Logical Block Address (LBA) to Cylinder-Head-Sector (CHS) format, this is the code that is used -
LBACHS:
xor dx, dx ; prepare dx:ax for operation
div WORD [bpbSectorsPerTrack] ; divide by sectors per track
inc dl ; add 1 (obsolute sector formula)
mov BYTE [absoluteSector], dl
xor dx, dx ; prepare dx:ax for operation
div WORD [bpbHeadsPerCylinder] ; mod by number of heads (Absolue head formula)
mov BYTE [absoluteHead], dl ; everything else was already done from the first formula
mov BYTE [absoluteTrack], al ; not much else to do :)
ret
I am not able to understand the logic behind this conversion. I tried using a few sample values to walk through it and see how it works, but that got me even more confused. Can someone explain how this conversion works and the logic used ?
I am guessing that your LBA value is being stored in
AXas you are performing division on some value.As some pre-information for you, the
absoluteSectoris the CHS sector number,absoluteHeadis the CHS head number, andabsoluteTrackis the CHS cylinder number. Cylinders and tracks are the exact same thing, just a different name.Also, the
DIVoperation for your code in 16-bit will take whatever is in theDX:AXregister combination and divide it by some value. The remainder of the division will be in theDXregister while the actual result will be in theAXregister.Next, the
*Xregisters are 16-bit registers, where*is one ofABCD. They are made up of a low and high component, both referred to as*Hand*Lfor high and low, respectively. For example, theDXregister hasDHfor the upper 8 bits andDLfor the lower 8 bits.Finally, as the
BYTEandWORDmodifiers simply state the size of the data that will be used/transferred.The first value you must extract is the sector number, which is obtained by diving the LBA value by the number of sectors per track. The
DLregister will then contain the sector number minus one. This is because counting sectors starts at 1, which is different than most values, which start at zero. To fix this, we add one to theDLregister get the correct sector value. This value is stored in memory atabsoluteSector.The next value you must extract is the head number, which is obtained by dividing the result of the last
DIVoperation by the number of heads per cylinder. TheDLregister will then contain the head number, which we store atabsoluteHead.Finally, we get the track number. With the last division we already obtained the value, which is in the
ALregister. We then store this value atabsoluteTrack.Hope this cleared things up a little bit.
-Adrian