简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM