Assembly Language for Intel-Based Computers Chapter 13: 16-Bit MS-DOS Programming Kip R. Irvine.

Slides:



Advertisements
Similar presentations
DOS and BIOS Interrupts DOS and BIOS interrupts are used to perform some very useful functions, such as displaying data to the monitor, reading data from.
Advertisements

Writing and reading files. Creating a file on a disk Get a file handle from system Use INT 21H function 3C to create a directory entry for the file Use.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
MS DOS File IO Text chapter 13. DateTime C:\MASM615>make16 datetime Assembling: datetime.asm Volume in drive C has no label. Volume Serial Number is 07D
Video systems (continue). Practice Modify the program to get a string from a keyboard to display the input string on the middle of the screen with reverse.
Direct video practice and Keyboard Operations
Assembly Language for Intel-Based Computers Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You may modify and.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Symbolic Constants Equal-Sign Directive Calculating.
Assembly Language for Intel-Based Computers
Chapter 7 Programming with DOS and BIOS Function Calls Objectives: The use of DOS and BIOS function call How to read the PC’s keyboard How to send text.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Assembly Language for Intel-Based Computers
MSDOS 16-bit programming
Kip Irvine: Assembly Language for Intel-Based Computers
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 13: 16-Bit MS-DOS Programming (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers Chapter 16: Expert MS-DOS Programming (c) Pearson Education, All rights reserved. You may modify.
Overview Chapter 13: 16-bit MS-DOS Programming MS-DOS and the IBM-PC
8.4 Instruction Execution Times TOBIN PROC FAR SUB AX,AX MOV DX,AX MOV CX,4 NEXTD: PUSH CX SUB BP,BP MOV CX,4 GETNUM: RCL BX,1 RCL BP,1 LOOP GETNUM.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You.
Chapter 15 BIOS-Level Programming
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 13: 16-Bit MS-DOS Programming (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7)
Lecture 11 Last notes on interrupts and exam review Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
BIOS1 Basic Input Output System BIOS BIOS refers to a set of procedures or functions that enable the programmer have access to the hardware of the computer.
ECE291 Computer Engineering II Lecture 13 Dr. Zbigniew Kalbarczyk University of Illinois at Urbana- Champaign.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 13: 16-Bit MS-DOS Programming (c) Pearson Education, All rights reserved.
Assembly Language for x86 Processors 6th Edition
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
1 Screen and Keyboard Operations Suthida Chaichomchuen
Chapter 3 Examining Computer Memory and Executing Instructions.
1 Chapter 5: Procedures and Interrupts Assembly Language for Intel-Based Computers, Kip R. Irvine 3rd edition 3/17/2000.
Chapter 2 Instruction Addressing and Execution. Lesson plan Review some concepts in the first week First assembly program with EMU8086 Related concepts.
Assembly Language for Intel-Based Computers Chapter 13: 16-Bit MS-DOS Programming Kip R. Irvine.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
File I/O MS-DOS Interrupt 21h has many functions dealing with file and directory I/O services. Both MS-DOS and MS_Windows use 16- bit integers called HANDLES.
Video systems. Lesson plan Review the code for the previous exercise Video systems Review for midterm exam.
8086 Microprocessor Interrupts By: Vijay Kumar. K Reference From Slide Share.
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
Writing and Reading Files Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
String Instructions String instructions were designed to operate on large data structures. The SI and DI registers are used as pointers to the data structures.
Microprocessors used in Personal Computers. The Memory Map of a Personal Computers Transient Program Area (TPA): Holds the operating system (interrupt.
Chapter Bit MS-DOS Programming Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,
Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 13: 16-Bit MS-DOS Programming Interrupts (c) Pearson Education, All rights reserved.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA 1 Assembly Language Programming.
Chapter 5: Procedures and Interrupts
Format of Assembly language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Symbolic Instruction and Addressing
A Hexadecimal Calculator
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Symbolic Instruction and Addressing
Unit:08 Software Interrupts
UNIT-II Assembly Language Programs Involving Logical
Chapter 6 –Symbolic Instruction and Addressing
By Nasser Halasa Assembly Language.
Presentation transcript:

Assembly Language for Intel-Based Computers Chapter 13: 16-Bit MS-DOS Programming Kip R. Irvine

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, MS-DOS Memory Map

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 segment- offset address for each possible interrupt handler. Interrupt Service Routine (ISR) is another name for interrupt handler.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Interrupt Vectoring Process

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Common Interrupts INT 10h Video Services INT 16h Keyboard Services INT 17h Printer Services INT 1Ah Time of Day INT 1Ch User Timer Interrupt INT 21h MS-DOS Services

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, MS-DOS Function Calls (INT 21h) ASCII Control Characters Selected Output Functions Selected Input Functions Date/Time Functions

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 4Ch: 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,4Ch; terminate process mov al,0; return code int 21h ; Same as: EXIT 0

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Selected Output Functions ASCII control characters 02h, 06h - Write character to standard output 05h - Write character to default printer 09h - Write string to standard output 40h - Write string to file or device

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, ASCII Control Characters 08h - Backspace (moves one column to the left) 09h - Horizontal tab (skips forward n columns) 0Ah - Line feed (moves to next output line) 0Ch - Form feed (moves to next printer page) 0Dh - Carriage return (moves to leftmost output column) 1Bh - Escape character Many INT 21h functions act upon the following control characters:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Functions 02h and 06h: Write Character to Standard Output Write the letter 'A' to standard output: mov ah,02h mov dl,’A’ int 21h Write a backspace to standard output: mov ah,06h mov dl,08h int 21h or: mov ah,2

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 05h: Write Character to Default Printer Write the letter 'A': mov ah,05h mov dl,65 int 21h Write a horizontal tab: mov ah,05h mov dl,09h int 21h

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 09h: Write String to Standard Output.data string db "This is a string$".code mov ah,9 mov dx,OFFSET string int 21h The string must be terminated by a '$' character. DS must point to the string's segment, and DX must contain the string's offset:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 40h: Write String to File or Device.data message db "Writing a string to the console" bytesWritten dw ?.code mov ah,40h mov bx,1 mov cx,50 mov dx,OFFSET message int 21h mov bytesWritten,ax Input: BX = file or device handle (console = 1), CX = number of bytes to write, DS:DX = address of array

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Selected Input Functions 01h, 06h - Read character from standard input 0Bh - Get status of the standard input buffer 3Fh - Read from file or device

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 01h: Read single character from standard input.data char db ?.code mov ah,01h int 21h mov char,al 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 06h: Read character from standard input without waiting.data char db ?.code L1:mov ah,06h; keyboard input mov dl,0FFh; don't wait for input int 21h jz L1; no character? repeat loop mov char,al; character pressed: save it 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 0Bh: Get status of standard input buffer L1:mov ah,0Bh; get buffer status int 21h cmp al,0; buffer empty? je L1; yes: loop again mov ah,1; no: input the key int 21h mov char,al; and save it Can be interrupted by Ctrl-Break (^C) Example: loop until a key is pressed. Save the key in a variable:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 3Fh: Read from file or device.data inputBuffer BYTE 127 dup(0) bytesRead WORD ?.code mov ah,3Fh mov bx,0; keyboard handle mov cx,127; max bytes to read mov dx,OFFSET inputBuffer; target location int 21h mov bytesRead,ax; save character count Reads a block of bytes. Can be interrupted by Ctrl-Break (^C) Example: Read string from keyboard:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Date/Time Functions 2Ah - Get system date 2Bh - Set system date 2Ch - Get system time 2Dh - Set system time

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 2Ah: Get system date mov ah,2Ah int 21h mov year,cx mov month,dh mov day,dl mov dayOfWeek,al Returns year in CX, month in DH, day in DL, and day of week in AL

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 2Bh: Set system date mov ah,2Bh mov cx,year mov dh,month mov dl,day int 21h cmp al,0 jne failed Sets the system date. AL = 0 if the function was not successful in modifying the date.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 2Ch: Get system time mov ah,2Ch int 21h mov hours,ch mov minutes,cl mov seconds,dh Returns hours (0-23) in CH, minutes (0-59) in CL, and seconds (0-59) in DH, and hundredths (0-99) in DL.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 2Dh: Set system time mov ah,2Dh mov ch,hours mov cl,minutes mov dh,seconds int 21h cmp al,0 jne failed Sets the system date. AL = 0 if the function was successful in modifying the time.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example 1: Time Display.model small.386.data.code m: mov mov ds,ax mov ah,2Ch int 21h mov bh,ch call display1 call printDot mov bh,cl call display1 call printDot mov bh,dh call display1 mov ah,4ch int 21h printDot proc pusha mov ah,2 mov dl,":" int 21h popa ret printDot endp

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example 1: Time Display display1 proc pusha mov cx,2 mov dl,bh shr dl,4 begin: cmp dl,10 jb L1 sub dl,10 add dl,41h mov ah,2 int 21h jmp L2 L1: mov ah,2 add dl,30h int 21h L2: and bh, b mov dl,bh loop begin popa ret display1 endp end m

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 mov ds,ax mov si,0 mov cx,32 L1: mov ah,8 int 21h cmp al,0dh je finLoop mov val[si],al inc si mov ah,2 mov dl,'*' int 21h loop L1 finLoop: mov di,0 mov cx,32 L2: mov al,val[di] cmp al,password[di] jne notequal cmp al,'$' je correct inc di loop L2 correct: mov dx,offset valid mov ah,9 int 21h jmp fin notequal: mov dx,offset invalid mov ah,9 int 21h fin: mov ah,4ch int 21h end m

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, What's Next MS-DOS and the IBM-PC MS-DOS Function Calls (INT 21h) Standard MS-DOS File I/O Services

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Standard MS-DOS File I/O Services 716Ch - Create or open file 3Eh - Close file handle 42h - Move file pointer Example: Read and Copy a Text File Reading the MS-DOS Command Tail

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 716Ch: Create or open file AX = 716Ch BX = access mode (0 = read, 1 = write, 2 = read/write) CX = attributes (0 = normal, 1 = read only, 2 = hidden, 3 = system, 20h = archive) DX = action (1 = open, 2 = truncate, 10h = create) DS:SI = segment/offset of filename

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Create a New File mov ax,716Ch; extended open/create mov bx,2; read-write mov cx,0 ; normal attribute mov dx,10h ; action: create + truncate mov si,OFFSET Filename int 21h jc failed mov handle,ax ; file handle mov actionTaken,cx ; action taken to open file

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Open an Existing File mov ax,716Ch ; extended open/create mov bx,0 ; read-only mov cx,0 ; normal attribute mov dx,1 ; open existing file mov si,OFFSET Filename int 21h jc failed mov handle,ax ; file handle mov actionTaken,cx ; action taken to open file

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 3Eh: Close file handle.data filehandle WORD ?.code mov ah,3Eh mov bx,filehandle int 21h jc failed Use the same file handle that was returned by INT 21h when the file was opened. Example:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, INT 21h Function 42h: Move file pointer mov ah,42h mov al,0 ; offset from beginning mov bx,handle mov cx,offsetHi mov dx,offsetLo int 21h 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 Permits random access to a file (text or binary).

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Read and Copy a Text File The Readfile.asm program demonstrates several INT 21h functions: Function 716Ch: Create new file or open existing file Function 3Fh: Read from file or device Function 40h: Write to file or device Function 3Eh: Close file handle

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Read and Copy a Text File.model small.data BufSize = 5000 infile db "Readfile.asm",0 outfile db "my_output_file.asm",0 inHandle dw ? outHandle dw ? buffer db BufSize DUP(?) bytesRead dw ?.code a: mov mov ds,ax ; Open the input file mov ax,716Ch ; 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 21h ; call MS-DOS jc quit; quit if error mov inHandle,ax

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Read and Copy a Text File ; Read the input file mov ah,3Fh; read file or device mov bx,inHandle; file handle mov cx,BufSize; max bytes to read mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error mov bytesRead,ax ; Display the buffer mov ah,40h; write file or device mov bx,1; console output handle mov cx,bytesRead; number of bytes mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error ; Close the file mov ah,3Eh ; function: close file mov bx,inHandle; input file handle int 21h ; call MS-DOS jc quit; quit if error

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Read and Copy a Text File ; Create the output file mov ax,716Ch ; extended create or open mov bx,1 ; mode = write-only mov cx,0; normal attribute mov dx,12h; action: create/truncate mov si,OFFSET outfile int 21h ; call MS-DOS jc quit; quit if error mov outHandle,ax; save handle ; Write buffer to new file mov ah,40h; write file or device mov bx,outHandle; output file handle mov cx,bytesRead; number of bytes mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error ; Close the file mov ah,3Eh ; function: close file mov bx,outHandle; output file handle int 21h ; call MS-DOS quit: mov ah,4ch int 21h END a

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 80h in the program segment prefix (PSP). Example: run a program named attr.exe and pass it "FILE1.DOC" as the command tail:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Reading the MS-DOS Command Tail.model small.386.data buffer db 129 dup('$').code a: mov mov ds,ax mov dx,offset buffer call Get_Commandtail mov ah,9 int 21h mov ah,4ch int 21h

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Reading the MS-DOS Command Tail Get_Commandtail PROC push es pusha; save general registers mov ah,62h; get PSP segment ;address int 21h; returned in BX mov es,bx; copied to ES mov si,dx; point to buffer mov di,81h; 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 L2; yes: exit cld; no: scan forward mov al,20h; space character repz scasb; scan for non space jz L2; all spaces found dec di; non space found inc cx L1: mov al,es:[di] mov [si],al inc si inc di loop L1 clc jmp L3 L2:stc L3:mov byte ptr [si],’$’ popa pop es ret Get_Commandtail ENDP End a