









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 16
This page cannot be seen from the preview
Don't miss anything!










+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)
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
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
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
00 Unused Entry 01 FAT 05 Extended Partition 06 FAT 0b FAT 0c FAT32 LBA 0e FAT16 LBA 0f Extended LBA 07 NTFS
; 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
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
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
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
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
; 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
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
; 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