8a4fd2092cd72de4fd6ed65db775ccc2.ppt
- Количество слайдов: 43
Assembly Language for Intel-Based Computers Kip R. Irvine Chapter 13: 16 -Bit MS-DOS Programming
MS-DOS and the IBM-PC • • Real-Address Mode MS-DOS Memory Organization MS-DOS Memory Map Software Interrupts INT Instruction Interrupt Vectoring Process Common Interrupts Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 2
Real-Address Mode • Real-address mode (16 -bit mode) programs have the following characteristics: • • Max 1 megabyte addressable RAM Single tasking No memory boundary protection Offsets are 16 bits • IBM PC-DOS: first Real-address OS for IBM-PC • Later renamed to MS-DOS, owned by Microsoft Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 3
MS-DOS Memory Organization • • • Interrupt Vector Table BIOS & DOS data Software BIOS MS-DOS kernel Resident command processor Transient programs Video graphics & text Reserved (device controllers) ROM BIOS Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 4
MS-DOS Memory Map Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 5
INT Instruction • The INT instruction executes a software interrupt. • The code that handles the interrupt is called an interrupt handler. • Syntax: INT number (number = 0. . FFh) The Interrupt Vector Table (IVT) holds a 32 -bit segmentoffset address for each possible interrupt handler. Interrupt Service Routine (ISR) is another name for interrupt handler. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 6
Interrupt Vectoring Process Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 7
Common Interrupts • • • INT 10 h Video Services INT 16 h Keyboard Services INT 17 h Printer Services INT 1 Ah Time of Day INT 1 Ch User Timer Interrupt INT 21 h MS-DOS Services Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 8
MS-DOS Function Calls (INT 21 h) • • ASCII Control Characters Selected Output Functions Selected Input Functions Date/Time Functions Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 9
INT 4 Ch: Terminate Process • Ends the current process (program), returns an optional 8 -bit return code to the calling process. • A return code of 0 usually indicates successful completion. mov ah, 4 Ch mov al, 0 int 21 h ; terminate process ; return code ; Same as: EXIT 0 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 10
Selected Output Functions • • • ASCII control characters 02 h, 06 h - Write character to standard output 05 h - Write character to default printer 09 h - Write string to standard output 40 h - Write string to file or device Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 11
ASCII Control Characters Many INT 21 h functions act upon the following control characters: • • • 08 h - Backspace (moves one column to the left) 09 h - Horizontal tab (skips forward n columns) 0 Ah - Line feed (moves to next output line) 0 Ch - Form feed (moves to next printer page) 0 Dh - Carriage return (moves to leftmost output column) • 1 Bh - Escape character Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 12
INT 21 h Functions 02 h and 06 h: Write Character to Standard Output Write the letter 'A' to standard output: mov ah, 02 h mov dl, ’A’ int 21 h or: mov ah, 2 Write a backspace to standard output: mov ah, 06 h mov dl, 08 h int 21 h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 13
INT 21 h Function 05 h: Write Character to Default Printer Write the letter 'A': mov ah, 05 h mov dl, 65 int 21 h Write a horizontal tab: mov ah, 05 h mov dl, 09 h int 21 h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 14
INT 21 h Function 09 h: Write String to Standard Output • The string must be terminated by a '$' character. • DS must point to the string's segment, and DX must contain the string's offset: . data string db "This is a string$". code mov ah, 9 mov dx, OFFSET string int 21 h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 15
INT 21 h Function 40 h: Write String to File or Device Input: BX = file or device handle (console = 1), CX = number of bytes to write, DS: DX = address of array. data message db "Writing a string to the console" bytes. Written dw ? . code mov mov int mov ah, 40 h bx, 1 cx, 50 dx, OFFSET message 21 h bytes. Written, ax Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 16
Selected Input Functions • 01 h, 06 h - Read character from standard input • 0 Bh - Get status of the standard input buffer • 3 Fh - Read from file or device Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 17
INT 21 h Function 01 h: Read single character from standard input • • Echoes the input character Waits for input if the buffer is empty Checks for Ctrl-Break (^C) Acts on control codes such as horizontal Tab. data char db ? . code mov ah, 01 h int 21 h mov char, al Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 18
INT 21 h Function 06 h: Read character from standard input without waiting • Does not echo the input character • Does not wait for input (use the Zero flag to check for an input character) • Example: repeats loop until a character is pressed. . data char db. code L 1: mov int jz mov ? ah, 06 h dl, 0 FFh 21 h L 1 char, al ; keyboard input ; don't wait for input ; no character? repeat loop ; character pressed: save it Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 19
INT 21 h Function 0 Bh: Get status of standard input buffer • Can be interrupted by Ctrl-Break (^C) • Example: loop until a key is pressed. Save the key in a variable: L 1: mov int cmp je mov int mov ah, 0 Bh 21 h al, 0 L 1 ah, 1 21 h char, al ; get buffer status ; buffer empty? ; yes: loop again ; no: input the key ; and save it Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 20
INT 21 h Function 3 Fh: Read from file or device • Reads a block of bytes. • Can be interrupted by Ctrl-Break (^C) • Example: Read string from keyboard: . data input. Buffer BYTE 127 dup(0) bytes. Read WORD ? . code mov ah, 3 Fh mov bx, 0 mov cx, 127 mov dx, OFFSET input. Buffer int 21 h mov bytes. Read, ax Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. ; keyboard handle ; max bytes to read ; target location ; save character count Web site Examples 21
Date/Time Functions • • 2 Ah - Get system date 2 Bh - Set system date 2 Ch - Get system time 2 Dh - Set system time Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 22
INT 21 h Function 2 Ah: Get system date • Returns year in CX, month in DH, day in DL, and day of week in AL mov int mov mov ah, 2 Ah 21 h year, cx month, dh day, dl day. Of. Week, al Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 23
INT 21 h Function 2 Bh: Set system date • Sets the system date. AL = 0 if the function was not successful in modifying the date. mov mov int cmp jne ah, 2 Bh cx, year dh, month dl, day 21 h al, 0 failed Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 24
INT 21 h Function 2 Ch: Get system time • Returns hours (0 -23) in CH, minutes (0 -59) in CL, and seconds (0 -59) in DH, and hundredths (0 -99) in DL. mov int mov mov ah, 2 Ch 21 h hours, ch minutes, cl seconds, dh Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 25
INT 21 h Function 2 Dh: Set system time • Sets the system date. AL = 0 if the function was successful in modifying the time. mov mov int cmp jne ah, 2 Dh ch, hours cl, minutes dh, seconds 21 h al, 0 failed Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 26
Example 1: Time Display. model small 386. . data. code m: mov ax, @data mov ds, ax mov ah, 2 Ch int 21 h mov bh, ch call display 1 call print. Dot proc pusha mov ah, 2 mov dl, ": " int 21 h popa ret print. Dot endp mov bh, cl call display 1 call print. Dot mov bh, dh call display 1 mov ah, 4 ch int 21 h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 27
Example 1: Time Display display 1 proc L 1: mov ah, 2 add dl, 30 h int 21 h pusha mov cx, 2 mov dl, bh shr dl, 4 L 2: and bh, 00001111 b mov dl, bh begin: cmp dl, 10 jb L 1 sub dl, 10 add dl, 41 h mov ah, 2 int 21 h jmp L 2 loop begin popa ret display 1 endp end m Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 28
Example 2: Password. model small 386. . data password db "password"$ invalid db "invalid password"$ valid db "correct password"$ val db 32 dup('$'). code m: mov ax, @data mov ds, ax mov si, 0 mov cx, 32 L 1: mov ah, 8 int 21 h cmp al, 0 dh je fin. Loop mov val]si], al inc si mov ah, 2 mov dl'*', int 21 h fin. Loop: mov di, 0 mov cx, 32 L 2: mov al, val[di] cmp al, password[di] jne notequal cmp al, '$' je correct inc di loop L 2 correct: mov dx, offset valid mov ah, 9 int 21 h jmp fin notequal: mov dx, offset invalid mov ah, 9 int 21 h fin: mov ah, 4 ch int 21 h end m loop L 1 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 29
What's Next • MS-DOS and the IBM-PC • MS-DOS Function Calls (INT 21 h) • Standard MS-DOS File I/O Services Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 30
Standard MS-DOS File I/O Services • • • 716 Ch - Create or open file 3 Eh - Close file handle 42 h - Move file pointer Example: Read and Copy a Text File Reading the MS-DOS Command Tail Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 31
INT 21 h Function 716 Ch: Create or open file • AX = 716 Ch • BX = access mode (0 = read, 1 = write, 2 = read/write) • CX = attributes (0 = normal, 1 = read only, 2 = hidden, 3 = system, 20 h = archive) • DX = action (1 = open, 2 = truncate, 10 h = create) • DS: SI = segment/offset of filename Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 32
Example: Create a New File mov mov mov int jc mov ax, 716 Ch bx, 2 cx, 0 dx, 10 h si, OFFSET Filename 21 h failed handle, ax action. Taken, cx Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. ; ; extended open/create read-write normal attribute action: create + truncate ; file handle ; action taken to open file Web site Examples 33
Example: Open an Existing File mov mov mov int jc mov ax, 716 Ch bx, 0 cx, 0 dx, 1 si, OFFSET Filename 21 h failed handle, ax action. Taken, cx Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. ; ; extended open/create read-only normal attribute open existing file ; file handle ; action taken to open file Web site Examples 34
INT 21 h Function 3 Eh: Close file handle • Use the same file handle that was returned by INT 21 h when the file was opened. • Example: . data filehandle WORD ? . code mov ah, 3 Eh mov bx, filehandle int 21 h jc failed Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 35
INT 21 h Function 42 h: Move file pointer Permits random access to a file (text or binary). mov mov mov int ah, 42 h al, 0 bx, handle cx, offset. Hi dx, offset. Lo 21 h ; offset from beginning AL indicates how the pointer's offset is calculated: 0: Offset from the beginning of the file 1: Offset from the current pointer location 2: Offset from the end of the file Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 36
Example: Read and Copy a Text File • The Readfile. asm program demonstrates several INT 21 h functions: • • Function 716 Ch: Create new file or open existing file Function 3 Fh: Read from file or device Function 40 h: Write to file or device Function 3 Eh: Close file handle Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 37
Example: Read and Copy a Text File. model small. data Buf. Size = infile outfile in. Handle out. Handle buffer bytes. Read. code a: mov 5000 db "Readfile. asm", 0 db "my_output_file. asm", 0 dw? db Buf. Size DUP(? ) dw? ax, @data ds, ax ; Open the input file mov ax, 716 Ch ; extended create or open mov bx, 0 ; mode = read-only mov cx, 0 ; normal attribute mov dx, 1 ; action: open mov si, OFFSET infile int 21 h ; call MS-DOS jc quit ; quit if error mov in. Handle, ax Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 38
Example: Read and Copy a Text File ; Read the input file mov ah, 3 Fh ; read file or device mov bx, in. Handle ; file handle mov cx, Buf. Size ; max bytes to read mov dx, OFFSET buffer ; buffer pointer int 21 h jc quit ; quit if error mov bytes. Read, ax ; Display the buffer mov ah, 40 h ; write file or device mov bx, 1 ; console output handle mov cx, bytes. Read ; number of bytes mov dx, OFFSET buffer ; buffer pointer int 21 h jc quit ; quit if error ; Close the file mov ah, 3 Eh ; mov bx, in. Handle ; int 21 h ; jc quit ; function : close file input file handle call MS-DOS quit if error Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 39
Example: Read and Copy a Text File ; Create the output file mov ax, 716 Ch ; extended create or open mov bx, 1 ; mode = write-only mov cx, 0 ; normal attribute mov dx, 12 h ; action: create/truncate mov si, OFFSET outfile int 21 h ; call MS-DOS jc quit ; quit if error mov out. Handle, ax ; save handle ; Write buffer to new file mov ah, 40 h ; write file or device mov bx, out. Handle ; output file handle mov cx, bytes. Read ; number of bytes mov dx, OFFSET buffer ; buffer pointer int 21 h jc quit ; quit if error ; Close the file mov ah, 3 Eh ; function: close file mov bx, out. Handle ; output file handle int 21 h ; call MS-DOS quit: mov ah, 4 ch int 21 h END a Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 40
Reading the MS-DOS Command Tail • When a program runs, any additional text on its command line is automatically stored in the 128 -byte MS-DOS command tail area, at offset 80 h in the program segment prefix (PSP). • Example: run a program named attr. exe and pass it "FILE 1. DOC" as the command tail: Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 41
Reading the MS-DOS Command Tail. model small 386. . data buffer db 129 dup('$'). code a: mov ax, @data mov ds, ax mov dx, offset buffer call Get_Commandtail mov ah, 9 int 21 h mov ah, 4 ch int 21 h Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 42
Reading the MS-DOS Command Tail Get_Commandtail PROC push es pusha ; save general registers mov ah, 62 h int mov 21 h es, bx mov si, dx di, 81 h L 1 : ; point to buffer ; PSP offset of command ; tail mov cx, 0 ; byte count mov cl, es]: di-1] ; get length byte cmp cx, 0 ; is the tail empty? je L 2 ; yes: exit cld ; no: scan forward mov al, 20 h ; space character repz scasb ; scan for non space jz L 2 ; all spaces found dec di ; non space found inc cx Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. [si], al inc loop clc jmp L 2: L 3: al, es: [di[ mov ; get PSP segment ; address ; returned in BX ; copied to ES mov si di L 1 L 3 stc mov byte ptr [si’$’, [ popa pop es ret Get_Commandtail ENDP End a Web site Examples 43
8a4fd2092cd72de4fd6ed65db775ccc2.ppt