Quaxton Hale
Quaxton Hale

Reputation: 2520

Assembly division and modulo

LBACHS:
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [bpbSectorsPerTrack]           ; calculate (AX / WORD [bpbSectorsPerTrack]
          inc     dl                                  ; adjust for sector 0
          mov     BYTE [absoluteSector], dl
          xor     dx, dx                              ; prepare dx:ax for operation
          div     WORD [bpbHeadsPerCylinder]          ; calculate
          mov     BYTE [absoluteHead], dl             ;
          mov     BYTE [absoluteTrack], al            ;Quotient is returned in AL
          ret

I have two questions regarding this piece of code.

I thought DIV stores the results in AX not DL? Why would I increase the DL register?

How is modulos calculated? With DL?

Upvotes: 0

Views: 171

Answers (1)

Brendan
Brendan

Reputation: 37232

The 16-bit DIV instruction divides the 32-bit value in DX:AX by a 16-bit number (from [bpbSectorsPerTrack] in your case); then stores the quotient in AX and the remainder in DX.

For your specific case, the value in DX:AX before the division is an LBA sector number. After the division, value in AX is LBA / sectors_per_track and the value in DX is LBA % sectors_per_track = CHS_sector - 1. Note: For CHS the first sector is sector number 1 and not sector number 0, which is why there's an inc involved.

For the second division, the value in DX:AX beforehand is LBA / sectors_per_track. After the division, value in AX is (LBA / sectors_per_track) / heads_per_cylinder = cylinder and the value in DX is (LBA / sectors_per_track) % heads_per_cylinder = head.

Upvotes: 1

Related Questions