CHS and LBA Confusion
Posted: Mon Aug 08, 2011 11:26 am
Hello I've been building a bootloader and I've looked for code showing how to convert LBA(Liogical Block Addressing) to CHS(Cylinder, Head, Sector) and CHS to LBA(BTW I'm using FAT12). I found this code and I just don't understand the way they did this.
The formula to convert CHS to LBA:
Shouldn't the code be look this:
The formula to convert CHS to LBA:
Code: Select all
CHS to LBA
Formula
LBA = (cluster - 2 ) * sectors per cluster
And here the code:
sub ax, 0x0002 ; subtract 2 from cluster number
xor cx, cx
mov cl, BYTE [bpbSectorsPerCluster] ; get sectors per cluster
mul cx ;Why multiply by cx? Why not ax?
LBA to CHS
Formula:
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)
Code:
LBACHS:
xor dx, dx
div WORD [bpbSectorsPerTrack]
inc dl
mov BYTE [absoluteSector], dl
xor dx, dx
div WORD [bpbHeadsPerCylinder]
mov BYTE [absoluteHead], dl
mov BYTE [absoluteTrack], al
ret
Code: Select all
xor dx, dx
div WORD [bpbSectorsPerTrack]
push dx
inc dx
mov BYTE [absoluteSector]
pop dx
;Then somehow get the MOD
mov BYTE[absoluteHead], dx
mov dx, WORD [bpbSectorPerTrack]
mul BYTES [bpbNumberOfHeads]
xor ax, ax
div dx