# How can I calculate CHS from the LBA?

I'm writing a bootsector, and for it I want to access a particular logical block address on a floppy disk. I have all the parameters of the disk, such as total sectors, sectors per track, head count, etc. And I know the formula to go from CHS to LBA, but I'm in need of a formula to calculate the Cylinder, Head, and Sector from a given LBA address.

I actually found such a function in the MikeOS source code:

``````l2hts:          ; Calculate head, track and sector settings for int 13h
; IN: logical sector in AX, OUT: correct registers for int 13h
push bx
push ax

mov bx, ax          ; Save logical sector

mov dx, 0           ; First the sector
div word [SectorsPerTrack]
add dl, 01h         ; Physical sectors start at 1
mov cl, dl          ; Sectors belong in CL for int 13h
mov ax, bx

mov dx, 0           ; Now calculate the head
div word [SectorsPerTrack]
mov dx, 0
div word [Sides]
mov ch, al          ; Track

pop ax
pop bx

mov dl, byte [bootdev]      ; Set correct device

ret
``````

But I can't work out how this works. Take the Sector for example, from what I can work out the formula for the Sector here is `(logical sector % sectors per track) + 1`. Is this correct? It doesn't make much sense to me, for some reason. Perhaps I just don't fully understand how logical sectors are layed out.

``````    C = LBA ÷ (HPC × SPT)