Floppy Disk-Microprocessor and Assembly Language Programming-Lecture Notes, Study notes of Microprocessor and Assembly Language Programming

This lecture handout was provided at Quaid-i-Azam University for Microprocessor and Assembly Language Programming course by Prof. Saleem Raza. Its main points are: Floppy, Disk, Track, Bios, Dos, Extension, Int, Push, Pop, Memory, Execute, Program

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A floppy disk is a circular plate with a fine coating of magnetic material
over it. The plate is enclosed in a plastic jacket which has a cover that can
slide to expose the magnetic surface. The drive motor attaches itself to the
central piece and rotates the plate. Two heads on both sides can read the
magnetically encoded data on the disk.
If the head is fixed and the motor rotates the disk the readable area on the
disk surface forms a circle called a track. Head moved to the next step forms
another track and so on. In hard disks the same structure is extended to a
larger number of tracks and plates. The tracks are further cut vertically into
sectors. This is a logical division of the area on the tracks. Each sector holds
512 bytes of data. A standard floppy disk has 80 tracks and 18 sectors per
track with two heads, one on each side totallying to 2880 sectors or 1440 KB
of data. Hard disks have varying number of heads and tracks pertaining to
their different capacities.
BIOS sees the disks as a combination of sectors, tracks, and heads, as a
raw storage device without concern to whether it is reading a file or directory.
BIOS provides the simplest and most powerful interface to the storage
medium. However this raw storage is meaningless to the user who needs to
store his files and organize them into directories. DOS builds a logical
structure on this raw storage space to provide these abstractions. This
logical formation is read and interpreted by DOS. If another file system is
build on the same storage medium the interpretations change. Main units of
the DOS structure are the boot sector in head 0, track 0, and sector 1, the
first FAT starting from head 0, track 0, sector 2, the second copy of FAT
starting from head 0, track 0, sector 11, and the root directory starting from
head 1, track 0, sector 2. The area from head 0, track 1, sector 16 to head 1,
track 79, sector 18 is used for storing the data of the files. Among this we will
be exploring the directory structure further. The 32 sectors reserved for the
root directory contain 512 directory entries. The format of a 32 byte directory
entry is shown below.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Floppy Disk-Microprocessor and Assembly Language Programming-Lecture Notes and more Study notes Microprocessor and Assembly Language Programming in PDF only on Docsity!

A floppy disk is a circular plate with a fine coating of magnetic material

over it. The plate is enclosed in a plastic jacket which has a cover that can

slide to expose the magnetic surface. The drive motor attaches itself to the

central piece and rotates the plate. Two heads on both sides can read the

magnetically encoded data on the disk.

If the head is fixed and the motor rotates the disk the readable area on the

disk surface forms a circle called a track. Head moved to the next step forms

another track and so on. In hard disks the same structure is extended to a

larger number of tracks and plates. The tracks are further cut vertically into

sectors. This is a logical division of the area on the tracks. Each sector holds

512 bytes of data. A standard floppy disk has 80 tracks and 18 sectors per

track with two heads, one on each side totallying to 2880 sectors or 1440 KB

of data. Hard disks have varying number of heads and tracks pertaining to

their different capacities.

BIOS sees the disks as a combination of sectors, tracks, and heads, as a

raw storage device without concern to whether it is reading a file or directory.

BIOS provides the simplest and most powerful interface to the storage

medium. However this raw storage is meaningless to the user who needs to

store his files and organize them into directories. DOS builds a logical

structure on this raw storage space to provide these abstractions. This

logical formation is read and interpreted by DOS. If another file system is

build on the same storage medium the interpretations change. Main units of

the DOS structure are the boot sector in head 0, track 0, and sector 1, the

first FAT starting from head 0, track 0, sector 2, the second copy of FAT

starting from head 0, track 0, sector 11, and the root directory starting from

head 1, track 0, sector 2. The area from head 0, track 1, sector 16 to head 1,

track 79, sector 18 is used for storing the data of the files. Among this we will

be exploring the directory structure further. The 32 sectors reserved for the

root directory contain 512 directory entries. The format of a 32 byte directory

entry is shown below.

+00 Filename (8 bytes) +08 Extension (3 bytes) +0B Flag Byte (1 byte) +0C Reserved (1 byte) +0D Creation Date/Time (5 bytes) +12 Last Accessed Data (2 bytes) +14 Starting Cluster High Word (2 bytes) for FAT +16 Time (2 bytes) +18 Date (2 bytes) +1A Starting Cluster Low Word (2 bytes) +1C File Size (4 bytes)

STORAGE ACCESS USING BIOS

We will be using BIOS disk services to directly see the data stored in the

directory entries by DOS. For this purpose we will be using the BIOS disk

services.

INT 13 - DISK - RESET DISK SYSTEM

AH = 00h DL = drive Return: CF = error flag AH = error code INT 13 - DISK - READ SECTOR(S) INTO MEMORY AH = 02h AL = number of sectors to read (must be nonzero) CH = low eight bits of cylinder number CL = sector number 1-63 (bits 0-5) high two bits of cylinder (bits 6-7, hard disk only) DH = head number DL = drive number (bit 7 set for hard disk) ES:BX -> data buffer Return: CF = error flag AH = error code AL = number of sectors transferred INT 13 - DISK - WRITE DISK SECTOR(S) AH = 03h AL = number of sectors to write (must be nonzero) CH = low eight bits of cylinder number CL = sector number 1-63 (bits 0-5) high two bits of cylinder (bits 6-7, hard disk only) DH = head number DL = drive number (bit 7 set for hard disk) ES:BX -> data buffer Return: CF = error flag AH = error code AL = number of sectors transferred INT 13 - DISK - GET DRIVE PARAMETERS AH = 08h DL = drive (bit 7 set for hard disk) Return: CF = error flag AH = error code CH = low eight bits of maximum cylinder number CL = maximum sector number (bits 5-0)

DL = drive number DS:SI -> disk address packet Return: CF = error flag AH = error code disk address packet's block count field set to number of blocks successfully transferred

The format of the disk address packet used above is as follows.

Offset Size Description 00h BYTE size of packet = 10h 01h BYTE reserved (0) 02h WORD number of blocks to transfer 04h DWORD -> transfer buffer 08h QWORD starting absolute block number

Hard disks have a different formation from floppy disks in that there is a

partition table at the start that allows several logical disks to be maintained

within a single physical disk. The physical sector 0 holds the master boot

record and a partition table towards the end. The first 446 bytes contain

MBR, then there are 4 16 byte partition entries and then there is a 2 byte

signature. A partition table entry has the following format.

Byte 0 – 0x80 for active 0x00 for inactive Byte 1-3 – Starting CHS Byte 4 – Partition Type Byte 5-7 – Ending CHS Byte 8-B – Starting LBA Byte C-F – Size of Partition

Some important partition types are listed below.

00 Unused Entry 01 FAT 05 Extended Partition 06 FAT 0b FAT 0c FAT32 LBA 0e FAT16 LBA 0f Extended LBA 07 NTFS

Extended partition type signals that the specified area is treated as a

complete hard disk with its own partition table and partitions. Therefore

extended partitions allow a recursion in partitioning and consequently an

infinite number of partitions are possible. The following program reads the

partition tables (primary and extended) using recursion and displays in an

indented form all partitions present on the first hard disk in the system.

Example

; a program to display the partition table [org 0x0100] jmp start

dap: db 0x10, 0 ; disk address packet dw 1 dd 0, 0, 0

msg: times 17 db ' ' db 10, 13, '$' fat12: db 'FAT12...$' fat16: db 'FAT16...$' fat32: db 'FAT32...$' ntfs: db 'NTFS....$' extended: db 'EXTEND..$' unknown: db 'UNKNOWN.$'

partypes: dw 0x1, fat12 ; table of known partition types dw 0x5, extended dw 0x6, fat dw 0xe, fat dw 0xb, fat dw 0xc, fat dw 0x7, ntfs dw 0xf, extended dw 0x0, unknown

; subroutine to print a number in a string as hex ; takes address of string and a 16bit number as parameter printnum: push bp mov bp, sp push ax push bx push cx push dx push di

mov di, [bp+6] ; string to store the number add di, 3

mov ax, [bp+4] ; load number in ax mov bx, 16 ; use base 16 for division mov cx, 4

nextdigit: mov dx, 0 div bx ; divide by 16 add dl, 0x30 ; convert into ascii value cmp dl, 0x jbe skipalpha

add dl, 7

skipalpha: mov [di], dl ; update char in string dec di loop nextdigit

pop di pop dx pop cx pop bx pop ax pop bp ret 4

; subroutine to print the start and end of a partition ; takes the segment and offset of the partition table entry printpart: push bp mov bp, sp push es push ax push di

les di, [bp+4] ; point es:di to dap

mov ax, msg push ax push word [es:di+0xA] call printnum ; print first half of start

add ax, 4 push ax push word [es:di+0x8] call printnum ; print second half of start

int 0x21 ; dos services

push ss mov ax, bp add ax, si push ax ; pass partition entry address call printpart ; print start and end from it

cmp byte [bp+si+4], 5 ; is it an extended partition je recurse ; yes, make a recursive call

cmp byte [bp+si+4], 0xf ; is it an extended partition jne exit ; yes, make a recursive call

recurse: mov ax, [bp+8] add ax, 2 ; increase indentation level push ax push word [bp+si+0xA] ; push partition type address push word [bp+si+0x8] call readpart ; recursive call

exit: add si, 16 ; point to next partition entry cmp si, -2 ; gone past last entry jne nextpart ; no, read this entry

failed: pop si pop dx pop bx pop cx pop ax mov sp, bp pop bp ret 6

start: xor ax, ax push ax ; start from zero indentation push ax ; main partition table at 0 push ax call readpart ; read and print it

mov ax, 0x4c00 ; terminate program int 0x

STORAGE ACCESS USING DOS

BIOS provides raw access to the storage medium while DOS gives a more

logical view and more cooked services. Everything is a file. A directory is a

specially organized file that is interpreted by the operating system itself. A

list of important DOS services for file manipulation is given below.

INT 21 - CREATE OR TRUNCATE FILE

AH = 3Ch CX = file attributes DS:DX -> ASCIZ filename Return: CF = error flag AX = file handle or error code INT 21 - OPEN EXISTING FILE AH = 3Dh AL = access and sharing modes DS:DX -> ASCIZ filename CL = attribute mask of files to look for (server call only) Return: CF = error flag AX = file handle or error code INT 21 - CLOSE FILE AH = 3Eh

BX = file handle Return: CF = error flag AX = error code INT 21 - READ FROM FILE AH = 3Fh BX = file handle CX = number of bytes to read DS:DX -> buffer for data Return: CF = error flag AX = number of bytes actually read or error code INT 21 - WRITE TO FILE AH = 40h BX = file handle CX = number of bytes to write DS:DX -> data to write Return: CF = error flag AX = number of bytes actually written or error code INT 21 - DELETE FILE AH = 41h DS:DX -> ASCIZ filename (no wildcards, but see notes) Return: CF = error flag AX = error code INT 21 - SET CURRENT FILE POSITION AH = 42h AL = origin of move BX = file handle CX:DX = offset from origin of new file position Return: CF = error flag DX:AX = new file position in bytes from start of file AX = error code in case of error INT 21 - GET FILE ATTRIBUTES AX = 4300h DS:DX -> ASCIZ filename Return: CF = error flag CX = file attributes AX = error code INT 21 - SET FILE ATTRIBUTES AX = 4301h CX = new file attributes DS:DX -> ASCIZ filename Return: CF = error flag AX = error code

We will use some of these services to find that two files are same in

contents or different. We will read the file names from the command prompt.

The command string is passed to the program in the program segment prefix

located at offset 0 in the current segment. The area from 0-7F contains

information for DOS, while the command tail length is stored at 80. From 81

to FF, the actual command tail is stored terminated by a CR (Carriage

Retrun).

mov bx, [handle2] ; handle for file to read mov cx, 4096 ; number of bytes to read mov dx, buffer2 ; buffer to read in int 0x21 ; dos services jnc check ; if no error, proceed mov dx, readfailed2 ; else, select error message jmp error ; proceed to error printing

check: pop cx ; number of bytes read of file 1 cmp ax, cx ; are number of byte same je check2 ; yes, proceed to compare them mov dx, different ; no, files are different jmp error ; proceed to message printing

check2: test ax, ax ; are zero bytes read jnz compare ; no, compare them mov dx, same ; yes, files are same jmp error ; proceed to message printing

compare: mov si, buffer1 ; point si to file 1 buffer mov di, buffer2 ; point di to file 2 buffer repe cmpsb ; compare the two buffers je check3 ; if equal, proceed mov dx, different ; else, files are different jmp error ; proceed to message printing

check3: cmp ax, 4096 ; were 4096 bytes read je readloop ; yes, try to read more mov dx, same ; no, files are same

error: mov ah, 9 ; service 9 – output message int 0x21 ; dos services

mov ah, 0x3e ; service 3e – close file mov bx, [handle1] ; handle of file to close int 0x21 ; dos services

mov ah, 0x3e ; service 3e – close file mov bx, [handle2] ; handle of file to close int 0x21 ; dos services

mov ax, 0x4c00 ; terminate program int 0x

Another interesting service that DOS provides regarding files is executing

them. An important point to understand here is that whenever a program is

executed in DOS all available memory is allocated to it. No memory is

available to execute any new programs. Therefore memory must be freed

using explicit calls to DOS for this purpose before a program is executed.

Important services in this regard are listed below.

INT 21 - ALLOCATE MEMORY

AH = 48h BX = number of paragraphs to allocate Return: CF = error flag AX = segment of allocated block or error code in case of error BX = size of largest available block in case of error INT 21 - FREE MEMORY AH = 49h ES = segment of block to free Return: CF = error flag AX = error code INT 21 - RESIZE MEMORY BLOCK AH = 4Ah BX = new size in paragraphs ES = segment of block to resize

Return: CF = error flag AX = error code BX = maximum paragraphs available for specified memory block INT 21 - LOAD AND/OR EXECUTE PROGRAM AH = 4Bh AL = type of load (0 = load and execute) DS:DX -> ASCIZ program name (must include extension) ES:BX -> parameter block Return: CF = error flag AX = error code

The format of parameter block is as follows.

Offset Size Description 00h WORD segment of environment to copy for child process (copy caller's environment if 0000h) 02h DWORD pointer to command tail to be copied into child's PSP 06h DWORD pointer to first FCB to be copied into child's PSP 0Ah DWORD pointer to second FCB to be copied into child's PSP 0Eh DWORD (AL=01h) will hold subprogram's initial SS:SP on return 12h DWORD (AL=01h) will hold entry point (CS:IP) on return

As an example we will use the multitasking kernel client from the

multitasking chapter and modify it such that after running all three threads

it executes a new instance of the command prompt instead of indefinitely

hanging around.

Example

; another multitasking TSR caller [org 0x0100] jmp start

; parameter block layout: ; cs,ip,ds,es,param ; 0, 2, 4, 6, 8

paramblock: times 5 dw 0 ; space for parameters lineno: dw 0 ; line number for next thread chars: db '|/-' ; chracters for rotating bar message: db 'moving hello' ; moving string message2: db ' ' ; to erase previous string messagelen: dw 12 ; length of above strings tail: db ' ', command: db 'COMMAND.COM', 0 execblock: times 11 dw 0

;;;;; COPY LINES 028-071 FROM EXAMPLE 10.1 (printnum) ;;;;; ;;;;; COPY LINES 073-114 FROM EXAMPLE 10.1 (printstr) ;;;;; ;;;;; COPY LINES 103-126 FROM EXAMPLE 11.5 (mytask) ;;;;; ;;;;; COPY LINES 128-146 FROM EXAMPLE 11.5 (mytask2) ;;;;; ;;;;; COPY LINES 148-193 FROM EXAMPLE 11.5 (mytask3) ;;;;;

start: mov [paramblock+0], cs ; code segment parameter mov word [paramblock+2], mytask ; offset parameter mov [paramblock+4], ds ; data segment parameter mov [paramblock+6], es ; extra segment parameter mov word [paramblock+8], 0 ; parameter for thread mov si, paramblock ; address of param block in si int 0x80 ; multitasking kernel interrupt

mov [paramblock+0], cs ; code segment parameter mov word [paramblock+2], mytask2 ; offset parameter mov [paramblock+4], ds ; data segment parameter

RH+22 BYTE Drive number for first unit of this block driver (0=A...) Return from driver RH+13 BYTE Number of units (block devices only) RH+14 DWORD Address of first free memory above driver (break address) RH+18 DWORD BPB pointer array (block devices only) 1 – Media Check RH+13 BYTE Media descriptor byte Return RH+14 BYTE Media change code -1 if disk changed 0 if dont know whether disk changed 1 if disk not changed RH+15 DWORD pointer to previous volume label if device attrib bit 11=1 (open/close/removable media supported) 2 – Build BPB RH+13 BYTE Media descriptor byte RH+14 DWORD buffer address (one sector) Return RH+18 DWORD pointer to new BPB if bit 13 (ibm format) is set buffer is first sector of fat, otherwise scrach space 4 – Read / 8 – Write / 9 – Write with verify RH+13 BYTE Media descriptor byte RH+14 DWORD transfer address RH+18 WORD byte or sector count RH+20 WORD starting sector number (for block devices) Return RH+18 WORD actual byte or sectors transferred RH+22 DWORD pointer to volume label if error 0Fh is returned

The BIOS parameter block discussed above is a structure that provides

parameters about the storage medium. It is stored in the first sector or the

boot sector of the device. Its contents are listed below.

00-01 bytes per sector 02 sectors per allocation unit 03-04 Number of reserved sectors ( 0 based) 05 number of file allocation tables 06-07 max number of root directory entries 08-09 total number of sectors in medium 0A media descriptor byte 0B-0C number of sectors occupied by a single FAT 0D-0E sectors per track (3.0 or later) 0F-10 number of heads (3.0 or later) 11-12 number of hidden sectors (3.0 or later) 13-14 high-order word of number of hidden sectors (4.0) 15-18 IF bytes 8-9 are zero, total number of sectors in medium 19-1E Reserved should be zero

We will be building an example device driver that takes some RAM and

expresses it as a secondary storage device to the operating system. Therefore

a new drive is added and that can be browsed to, filed copied to and from

just like ordinary drives expect that this drive is very fast as it is located in

the RAM. This program cannot be directly executed since it is not a user

program. This must be loaded by adding the line “device=filename.sys” in the

“config.sys” file in the root directory.

Example

; ram disk dos block device driver header: dd -1 ; no next driver dw 0x2000 ; driver attributes: block device dw strategy ; offset of strategy routine dw interrupt ; offset of interrupt routine db 1 ; no of units supported times 7 db 0 ; reserved

request: dd 0 ; space for request header

ramdisk: times 11 db 0 ; initial part of boot sector bpb: dw 512 ; bytes per sector db 1 ; sectors per cluster dw 1 ; reserved sectors db 1 ; fat copies dw 48 ; root dir entries dw 105 ; total sectors db 0xf8 ; media desc byte: fixed disk dw 1 ; sectors per fat times 482 db 0 ; remaining part of boot sector db 0xfe, 0xff, 0xff ; special bytes at start of FAT times 509 db 0 ; remaining FAT entries unused times 103*512 db 0 ; 103 sectors for data bpbptr: dw bpb ; array of bpb pointers

dispatch: dw init ; command 0: init dw mediacheck ; command 1: media check dw getbpb ; command 2: get bpb dw unknown ; command 3: not handled dw input ; command 4: input dw unknown ; command 5: not handled dw unknown ; command 6: not handled dw unknown ; command 7: not handled dw output ; command 8: output dw output ; command 9: output with verify

; device driver strategy routine strategy: mov [cs:request], bx ; save request header offset mov [cs:request+2], es ; save request header segment retf

; device driver interrupt routine interrupt: push ax push bx push cx push dx push si push di push ds push es

push cs pop ds

les di, [request] mov word [es:di+3], 0x mov bl, [es:di+2] mov bh, 0 cmp bx, 9 ja skip shl bx, 1

call [dispatch+bx]

skip: pop es pop ds pop di pop si pop dx pop cx pop bx pop ax retf