Chapter 9 Using Disks and Files Objectives: The difference between Floppy and Hard Disks What the Boot Sector is for What the File Allocation Table is for How to access disk sectors trough BIOS INT 13H How to navigate trough DOS’s directory structure Using DOS INT 21H functions to access disk files EE314 Microprocessor Systems Based on "An Introduction to the Intel Family of Microprocessors" by James L. Antonakos
9.2 Organization of Floppy and Hard Disks sector 1 sector 2 sector 3 sector 4 sector 5 sector 6 sector 7 sector 8 sector 9 track 0 track 1 track 2 track 79 side (head) 1 side (head) 0 Bytes/sector = 512 Examples of Floppy Disks: Typical Hard-Drive construction Read/Write Heads head 0 head 1 Bytes/sector = 512 Examples of Hard Disks: Cylinder = all the tracks accessed at same time by all the heads. Capacity = heads X cylinders X sectors/track X bytes/sector. Spindle Platters
debug -l d 100 1AA5:0100 EB 3C 90 4D F E <.MSDOS AA5: A0 05 F P AA5: E 4F 20 4E )VfF.NO NA 1AA5:0130 4D FA 33 ME FAT12.3 1AA5:0140 C0 8E D0 BC 00 7C BB C5 37 1E |...x.6.7.V 1AA5: BF 3E 7C B9 0B 00-FC F3 A4 06 1F C6 45 FE.S.>| E. 1AA5:0160 0F 8B 0E 18 7C 88 4D F C7 07 3E 7C FB....|.M..G...>|. 1AA5:0170 CD C C B 0E 13 7C..ry3.9..|t....| 9.2 Organization of Floppy and Hard Disks The Boot Sector (sector 0) load at address 100h from drive 0, starting at sector 0, 1 sector dump from address 100h
debug -l d 100 1AA5: F RAR EXE.).O 1AA5:0110 9E 24 9E B-AC F $.$..`[....h.. 1AA5:0120 E F SDOS SYS AA5: E 1B BE 03 FA > AA5:0140 E5 45 5A A EZA ZIP.... 1AA5: BD 8C-2B 23 6E 03 CE D #n AA5:0160 E5 42 4C E BLSPACEBIN AA5: E 1B F6 FA > Organization of Floppy and Hard Disks The Directory (sector 7...) load at address 100h from drive 0, starting at sector 7 (directory), 1 sector
debug -l d 100 1ff 1AA5:0100 F0 FF FF A0 00 1AA5:0110 C0 00 0D E0 00 0F AA5: A0 01 1B-C0 01 1D E0 01 1F AA5: A0 02 2B 1AA5:0140 C0 02 2D E0 02 2F AA5: FF 0F AA5: AA5: Organization of Floppy and Hard Disks The FAT (File Allocation Table) load at address 100h from drive 0, starting at sector 1, 1 sector dump from address 100h bytes 2 FAT entries FAT entry for cluster 2 FAT entry for cluster 3 FAT entry for Floppy disk = 12 bit (HD 16 bit) 000: cluster available 002-FEF: number of next cluster in file or directory FF0-FF6: reserved FF7: Bad sector in cluster FF8-FFF: Last cluster in file or directory
9.3 Reading and Writing Disk Sectors BIOS INT 13, function 00H: Reset Disk System Specification: resets the specified disk drive. Relocates the disk head to track 0. Input: AH = 00 (function code), DL = the drive to be reset (0 =>A, 1 =>B,...) Output: CF = 0 if succeed, CF = 1 if failed, AH = error code: 00h No error 01h Invalid function request 02h Address mark not found 03h Write protect error 04h Sector not found 06h Disk change line active 08h DMA overrun operation 09h Data boundary error 0Ch Media type not found 10h Uncorrectable ECC or CRC error 20h General controller failure 40h Seek operation failed 80h Timeout BIOS INT 13, function 01H: Read Disk Status Specification: returns the specified disk drive status. Input and Output similar to function 00 (but AH = 01 (function code). BIOS INT 13, function 02H: Read Disk Sectors Specification: copies the content of specified sectors into DTA (Disk Transfer memory Area). BIOS INT 13, function 03H: Write Disk Sectors Specification: copies the content of DTA (Disk Transfer memory Area) into specified sectors. Input and Output similar to function 02 (but AH = 03 (function code). Constrain: does not update the FAT (higher level DOS INTs perform FAT housekeeping). Input: AH = 02 (function code), AL = Number of sectors to be read, CH = Track number, CL = First sector to be read, DH = Head number, DL = Drive number, BX pointer to DTA, ES = DTA’s segment Output: CF = 0 if succeed, CF = 1 if failed, AH = error code, AL = number of sectors read.
9.4 Directory Functions DOS INT 21, function 47H: Get current Directory Specification: returns the path of the current directory at DS:SI. Input: AH = 47h, DL = drive ID (0 => current drive, 1 =>A (!!!), 2 =>B,...), DS:SI = memory buffer address. Output: CF = 0 if succeed, CF = 1 if failed, AX = error code (see below), memory(DS:SI) = the ASCIIZ string representing the current directory path (an ASCIIZ string use the byte “0” as terminator (similar with “$” for display string function). 00h No error 01h Invalid function number02h File not found 03h Path not found 04h No more handles available05h Access denied 06h Invalid Handle 07h Bad memory control blocks08h Insufficient memory 09h Invalid memory block address0Ah Invalid environment 0Bh Invalid format 0Ch Invalid access code0Dh Invalid data 0Eh Reserved 0Fh Invalid drive specification10h Removing current directory 11h Not same device 12h Not more files to be found13h Disk is write protected 14h Unknown disk 15h Drive is not ready16h Unknown command 17h Data error (CRC) 18h Bad request length 19h Seek error 1Ah Unknown media type 1Bh Sector not found1Ch Printer out-of-paper 1Dh Write fault 1Eh Read fault1Fh General failure Example: MOV AH,47h MOV DL,0 MOV SI, 200h INT 21h 1C3F: C 50 5C Drive not specified, the current one is considered. String terminatorASCIIZ string
9.4 Directory Functions DOS INT 21, function 3BH: Set (Change) Current Directory Specification: sets the current directory to the path found in memory at address DS:DX. Input: AH = 3Bh (function code), DS:DX = pointer to ASCIIZ string, memory(DS:SI) = the ASCIIZ string representing the new directory path. DOS INT 21, function 39H: Create Subdirectory Specification: Creates a new directory, heaving the path found in memory at address DS:DX. Output: CF = 0 if succeed, CF = 1 if failed, AX = error code (similar to function 47h). DOS INT 21, function 3AH: Delete Subdirectory Specification: Deletes the directory heaving the path found in memory at address DS:DX. DOS INT 21, function 19H: Get Current Drive Specification: returns the ID for the current drive (0 =>A (!!!), 1 =>B,...(similar to INT13H) ). Note: The rules for path specifying are the known DOS rules. Ex: - A path beginning with a letter followed by ”:” specifies a new drive to be used. - A path beginning with ”\” is considered relative to the root directory. Input and output: Similar to function 3B (but AH = 39h). Input and output: Similar to function 3B (but AH = 3Ah). Input: AH = 19h (function code) Output: AL = The current drive ID. - A path beginning otherwise is considered relative to the current directory. Constrain: The directory must be empty to complete successfully.
9.4 Directory Functions ;Program PATHNAME.ASM: ;Display current directory path..MODEL SMALL.DATA PMSG DB ' Path for current directory: $ ' DRIVE DB ? DB ':\$' PATH DB 128 DUP(?) CRLF DB 0DH,0AH,'$' EMSG DB ' Error getting pathname. ',0DH,0AH,'$'.CODE.STARTUP MOV AH,19H ;get current drive function INT 21H ;DOS call ADD AL,41H ;add alpha bias MOV DRIVE,AL ;save for later LEA SI,PATH ;set pointer to pathname buffer SUB DL,DL ;current drive MOV AH,47H ;get current directory INT 21H ;DOS call JC EROR ;error if carry set LEA DX,PMSG ;set up pointer to dir message MOV AH,9 ;display string function INT 21H ;DOS call LEA DX,DRIVE ;set pointer to drive string MOV AH,9 ;display string function INT 21H ;DOS call LEA SI,PATH ;set up pointer to path SHPA: MOV DL,[SI] ;get path character OR DL,DL ;is it zero? JZ FIN MOV AH,2 ;display character function INT 21H ;DOS call INC SI ;advance to next position JMP SHPA FIN: LEA DX,CRLF ;set pointer to newline string MOV AH,9 ;display string function INT 21H ;DOS call JMP EXIT EROR: LEA DX,EMSG ;set pointer to error message MOV AH,9 ;display string function INT 21H ;DOS call EXIT:.EXIT END DL = 0=> the drive is not specified => the current drive is used. Function 09h could not be used because the string doesn’t ends with “$”.