How do all sectors of a disk repeat in an assembly?

In the process of learning how to build, I write the OS. I successfully wrote the code needed to add a second 512-byte sector to the initial 512-byte bootloader:

%define KBDINT  0x16
%define VIDINT  0x10
%define DISKINT 0x13
%define TTYOUT  0x0E
%define VIDMODE 0x0000
%define NUL 0x00
%define CR  0x0D
%define LF  0x0A
%define START   0x7C00

%macro  PRINT   1
    mov si, %1
    call    print
%endmacro

    bits    16          ; 16 bit real mode
    org START           ; loader start in memory
start:  jmp main

print:  jmp .init
.loop:  mov bx, VIDMODE
    mov ah, TTYOUT
    int VIDINT
    inc si
.init:  mov al, [si]
    cmp al, NUL
    jne .loop
    ret

main:   cli
    xor ax, ax
    mov ds, ax
    mov es, ax
    sti
    PRINT   welcome

    mov ah, NUL
    int DISKINT
    mov al, 0x01        ; sector count
    mov ah, 0x02        ; read function
    mov bx, kernel
    mov cl, 0x02
    mov ch, 0x00        ; cylinder number
    mov dh, 0x00        ; head number
    int DISKINT
    jc  fail
    jmp kernel

fail:   PRINT   failure
;   jmp halt

halt:   PRINT   halting
    cli
    hlt
    PRINT   imprbbl
    jmp halt

    welcome db "moose os", CR, LF, NUL
    failure db "failed disk load", CR, LF, NUL
    halting db "halting", CR, LF, NUL
    imprbbl db "but that impossible!", CR, LF, NUL

times 0x0200 - ($ - $$) - 2 db 0x00
    end dw 0xAA55

kernel: PRINT   yay
    yay db "kernel", CR, LF, NUL
    jmp halt

times 0xFFFF db 0x00

I will compile the file with nasm -f bin -o boot.bin boot.asm && qemu boot.bin::

binary execution

I am curious how heads and cylinders are used:

  • How are sector iterations performed?
  • How is the iteration between emulation and direct execution different?
+4
source share
1 answer

• How are sector iterations performed?

, CHS CylinderHeadSector, . BIOS 08h on int 13h, , , .

CL 1 63.
DH 0 255, 255 .
CL 0 1023. , 2 10- 6 7 CL!

CHS, - , C - S - .
, .
, S, , reset (1) , H.
, H, , reset (0) , C.
, C, , reset (0). . SectorCount , , .

; INPUT:  DL=Drive
;         CH=Cylinder
;         DH=Head
;         CL=Sector
;         AX=SectorCount
;         ES:BX=Buffer
; OUTPUT: CF=0 AH       = 0
;              CH,DH,CL = CHS of following sector
;         CF=1 AH       = Error status
;              CH,DH,CL = CHS of problem sector
ReadDiskSectors:
  push    es
  push    di
  push    bp
  mov     bp,sp           ;Local variables:
  push    ax              ;[bp-2]=SectorCount
  push    cx              ;[bp-4]=MaxSector
  push    dx              ;[bp-6]=MaxHead
  push    bx              ;[bp-8]=MaxCylinder

  push    es
  mov     ah,08h
  int     13h             ;ReturnDiskDriveParameters
  pop     es
  jc      NOK
  mov     bx,cx           ;10-bit cylinder info -> BX
  xchg    bl,bh
  shr     bh,6
  xchg    [bp-8],bx       ;Store MaxCylinder and get input BX back
  movzx   dx,dh           ;8-bit head info -> DX
  xchg    [bp-6],dx       ;Store MaxHead and get input DX back
  and     cx,003Fh        ;6-bit sector info -> CX
  xchg    [bp-4],cx       ;Store MaxSector and get input CX back

ReadNext:
  mov     di,5            ;Max 5 tries per sector
ReadAgain:
  mov     ax,0201h        ;Read 1 sector
  int     13h             ;ReadDiskSectors
  jnc     OK
  push    ax              ;Save error status byte in AH
  mov     ah,00h
  int     13h             ;ResetDiskSystem
  pop     ax
  dec     di
  jnz     ReadAgain
  stc
  jmp     NOK
OK:
  dec     word ptr [bp-2] ;SectorCount
  jz      Ready
  call    NextCHS
  mov     ax,es           ;Move buffer 512 bytes up
  add     ax,512/16
  mov     es,ax
  jmp     ReadNext

Ready:
  call    NextCHS         ;Return useful CHS values to support reads
  xor     ah,ah           ; -> CF=0 ... that are longer than memory
NOK:
  mov     sp,bp
  pop     bp
  pop     di
  pop     es
  ret

NextCHS:
  mov     al,cl            ;Calculate the 6-bit sector number
  and     al,00111111b
  cmp     al,[bp-4]        ;MaxSector
  jb      NextSector
  cmp     dh,[bp-6]        ;MaxHead
  jb      NextHead
  mov     ax,cx            ;Calculate the 10-bit cylinder number
  xchg    al,ah
  shr     ah,6
  cmp     ax,[bp-8]        ;MaxCylinder
  jb      NextCylinder
DiskWrap:
  mov     cx,1             ;Wraparound to very first sector on disk
  mov     dh,0
  ret
NextCylinder:
  inc     ax
  shl     ah,6             ;Split 10-bit cylinder number over CL and CH
  xchg    al,ah
  mov     cx,ax
  mov     dh,0
  inc     cl
  ret
NextHead:
  inc     dh
  and     cl,11000000b
NextSector:
  inc     cl
  ret

, 512 , , . , 512- .
, 4- DisketteParameterTable, ES: DI BIOS ReturnDiskDriveParameters.

• ?

, .
BIOS CHS, ... , !
, , , . , @Jester, : " ?" , times 0xFFFF db 0x00

. ​​ 7C00h, SS: SP 0000h: 7C00h .

main:
  cli
  xor ax, ax
  mov ds, ax
  mov es, ax
  mov ss, ax
  mov sp, 7C00h
  sti
  PRINT   welcome

@Fifoernik, jmp halt yay db "kernel", CR, LF, NUL, !

kernel:
  PRINT   yay
  jmp halt
  yay db "kernel", CR, LF, NUL
+9

All Articles