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

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

    21-Dec-2015
  • Category

    Documents

  • view

    238
  • download

    0

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

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

Assembly Language for Intel-Based Assembly Language for Intel-Based ComputersComputers

Chapter 13: 16-Bit MS-DOS Programming

Kip R. Irvine

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 2Web site Examples

MS-DOS and the IBM-PCMS-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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 3Web site Examples

Real-Address ModeReal-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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 4Web site Examples

MS-DOS Memory OrganizationMS-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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 5Web site Examples

MS-DOS Memory MapMS-DOS Memory Map

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 6Web site Examples

INT InstructionINT 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.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 7Web site Examples

Interrupt Vectoring ProcessInterrupt Vectoring Process

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 8Web site Examples

Common InterruptsCommon 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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 9Web site Examples

MS-DOS Function Calls (INT 21h)MS-DOS Function Calls (INT 21h)

• ASCII Control Characters• Selected Output Functions• Selected Input Functions• Date/Time Functions

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 10Web site Examples

INT 4Ch: Terminate ProcessINT 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 processmov al,0 ; return codeint 21h

; Same as:

EXIT 0

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 11Web site Examples

Selected Output FunctionsSelected 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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 12Web site Examples

ASCII Control CharactersASCII 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:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 13Web site Examples

INT 21h Functions 02h and 06h: INT 21h Functions 02h and 06h: Write Character to Standard OutputWrite Character to Standard Output

Write the letter 'A' to standard output:

mov ah,02hmov dl,’A’int 21h

Write a backspace to standard output:

mov ah,06hmov dl,08hint 21h

or: mov ah,2

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 14Web site Examples

INT 21h Function 05h: INT 21h Function 05h: Write Character to Default PrinterWrite Character to Default Printer

Write the letter 'A':

mov ah,05hmov dl,65int 21h

Write a horizontal tab:

mov ah,05hmov dl,09hint 21h

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 15Web site Examples

INT 21h Function 09h: INT 21h Function 09h: Write String to Standard OutputWrite String to Standard Output

.datastring db "This is a string$"

.codemov ah,9mov dx,OFFSET stringint 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:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 16Web site Examples

INT 21h Function 40h: INT 21h Function 40h: Write String to File or DeviceWrite String to File or Device

.datamessage db "Writing a string to the console"bytesWritten dw ?

.codemov ah,40hmov bx,1mov cx,50mov dx,OFFSET messageint 21hmov bytesWritten,ax

Input: BX = file or device handle (console = 1), CX = number of bytes to write, DS:DX = address of array

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 17Web site Examples

Selected Input FunctionsSelected Input Functions

• 01h, 06h - Read character from standard input• 0Bh - Get status of the standard input buffer• 3Fh - Read from file or device

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 18Web site Examples

INT 21h Function 01h: INT 21h Function 01h: Read single character from standard inputRead single character from standard input

.datachar db ?.codemov ah,01hint 21hmov 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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 19Web site Examples

INT 21h Function 06h: INT 21h Function 06h: Read character from standard input without waitingRead character from standard input without waiting

.datachar db ?.codeL1: mov ah,06h ; keyboard input

mov dl,0FFh ; don't wait for inputint 21h

jz L1 ; no character? repeat loopmov 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.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 20Web site Examples

INT 21h Function 0Bh: INT 21h Function 0Bh: Get status of standard input bufferGet status of standard input buffer

L1: mov ah,0Bh ; get buffer statusint 21h

cmp al,0 ; buffer empty? je L1 ; yes: loop again

mov ah,1 ; no: input the keyint 21hmov 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:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 21Web site Examples

INT 21h Function 3Fh: INT 21h Function 3Fh: Read from file or deviceRead from file or device

.datainputBuffer BYTE 127 dup(0)bytesRead WORD ?.codemov ah,3Fhmov bx,0 ; keyboard handlemov cx,127 ; max bytes to readmov dx,OFFSET inputBuffer ; target locationint 21hmov bytesRead,ax ; save character count

• Reads a block of bytes.• Can be interrupted by Ctrl-Break (^C)• Example: Read string from keyboard:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 22Web site Examples

Date/Time FunctionsDate/Time Functions

• 2Ah - Get system date• 2Bh - Set system date • 2Ch - Get system time• 2Dh - Set system time

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 23Web site Examples

INT 21h Function 2Ah: INT 21h Function 2Ah: Get system dateGet system date

mov ah,2Ahint 21hmov year,cxmov month,dhmov day,dlmov dayOfWeek,al

• Returns year in CX, month in DH, day in DL, and day of week in AL

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 24Web site Examples

INT 21h Function 2Bh: INT 21h Function 2Bh: Set system dateSet system date

mov ah,2Bhmov cx,yearmov dh,monthmov dl,dayint 21hcmp al,0jne failed

• Sets the system date. AL = 0 if the function was not successful in modifying the date.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 25Web site Examples

INT 21h Function 2Ch: INT 21h Function 2Ch: Get system timeGet system time

mov ah,2Chint 21hmov hours,chmov minutes,clmov 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.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 26Web site Examples

INT 21h Function 2Dh: INT 21h Function 2Dh: Set system timeSet system time

mov ah,2Dhmov ch,hoursmov cl,minutesmov dh,secondsint 21hcmp al,0jne failed

• Sets the system date. AL = 0 if the function was successful in modifying the time.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 27Web site Examples

Example 1: Time DisplayExample 1: Time Display

.model small

.386

.data

.codem:

mov ax,@datamov ds,ax

mov ah,2Chint 21hmov bh,chcall display1call printDot

mov bh,clcall display1call printDot

mov bh,dhcall display1

mov ah,4chint 21h

printDot proc

pusha

mov ah,2

mov dl,":"

int 21h

popa

ret

printDot endp

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 28Web site Examples

Example 1: Time DisplayExample 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,00001111b

mov dl,bh

loop begin

popa

ret

display1 endp

end m

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 29Web site Examples

Example 2: PasswordExample 2: Password.model small.386.datapassword db "password$"invalid db "invalid password$"valid db "correct password$"val db 32 dup('$').codem:

mov ax,@datamov ds,ax

mov si,0mov cx,32

L1:mov ah,8int 21hcmp al,0dhje finLoopmov val[si],alinc simov ah,2mov dl,'*'int 21hloop L1

finLoop:mov di,0mov cx,32L2:

mov al,val[di]cmp al,password[di]jne notequalcmp al,'$'je correctinc diloop L2

correct:mov dx,offset validmov ah,9int 21hjmp fin

notequal:mov dx,offset invalidmov ah,9int 21h

fin:mov ah,4chint 21h

end m

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 30Web site Examples

What's NextWhat's Next

• MS-DOS and the IBM-PC• MS-DOS Function Calls (INT 21h)• Standard MS-DOS File I/O Services

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 31Web site Examples

Standard MS-DOS File I/O ServicesStandard 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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 32Web site Examples

INT 21h Function 716Ch: INT 21h Function 716Ch: Create or open fileCreate 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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 33Web site Examples

Example: Create a New FileExample: Create a New File

mov ax,716Ch ; extended open/createmov bx,2 ; read-writemov cx,0 ; normal attributemov dx,10h ; action: create + truncatemov si,OFFSET Filenameint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 34Web site Examples

Example: Open an Existing FileExample: Open an Existing File

mov ax,716Ch ; extended open/createmov bx,0 ; read-onlymov cx,0 ; normal attributemov dx,1 ; open existing filemov si,OFFSET Filenameint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 35Web site Examples

INT 21h Function 3Eh: INT 21h Function 3Eh: Close file handleClose file handle

.datafilehandle WORD ?.code

mov ah,3Ehmov bx,filehandle int 21hjc failed

• Use the same file handle that was returned by INT 21h when the file was opened.

• Example:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 36Web site Examples

INT 21h Function 42h: INT 21h Function 42h: Move file pointerMove file pointer

mov ah,42hmov al,0 ; offset from beginningmov bx,handlemov cx,offsetHimov dx,offsetLoint 21h

AL indicates how the pointer's offset is calculated:0: Offset from the beginning of the file1: Offset from the current pointer location 2: Offset from the end of the file

Permits random access to a file (text or binary).

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 37Web site Examples

Example: Read and Copy a Text FileExample: 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

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 38Web site Examples

Example: Read and Copy a Text FileExample: Read and Copy a Text File

.model small

.data

BufSize = 5000infile db "Readfile.asm",0outfile db "my_output_file.asm",0inHandle dw ?outHandle dw ?buffer db BufSize DUP(?)bytesRead dw ?

.codea: mov ax,@data mov ds,ax

; Open the input filemov ax,716Ch ; extended create or openmov bx,0 ; mode = read-onlymov cx,0 ; normal attributemov dx,1 ; action: openmov si,OFFSET infileint 21h ; call MS-DOSjc quit ; quit if errormov inHandle,ax

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 39Web site Examples

Example: Read and Copy a Text FileExample: Read and Copy a Text File

; Read the input filemov ah,3Fh ; read file or devicemov bx,inHandle ; file handlemov cx,BufSize ; max bytes to readmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if errormov bytesRead,ax

; Display the buffermov ah,40h ; write file or devicemov bx,1 ; console output handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error

; Close the filemov ah,3Eh ; function: close filemov bx,inHandle ; input file handleint 21h ; call MS-DOSjc quit ; quit if error

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 40Web site Examples

Example: Read and Copy a Text FileExample: Read and Copy a Text File

; Create the output filemov ax,716Ch ; extended create or openmov bx,1 ; mode = write-onlymov cx,0 ; normal attributemov dx,12h; action: create/truncatemov si,OFFSET outfileint 21h ; call MS-DOSjc quit ; quit if errormov outHandle,ax ; save handle

; Write buffer to new filemov ah,40h; write file or devicemov bx,outHandle ; output file handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer; buffer pointerint 21hjc quit ; quit if error

; Close the filemov ah,3Eh ; function: close filemov bx,outHandle ; output file handleint 21h ; call MS-DOS

quit:mov ah,4chint 21h

END a

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 41Web site Examples

Reading the MS-DOS Command TailReading 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:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 42Web site Examples

Reading the MS-DOS Command TailReading the MS-DOS Command Tail

.model small

.386

.databuffer db 129 dup('$').codea:

mov ax,@datamov ds,ax

mov dx,offset buffercall Get_Commandtail

mov ah,9int 21h

mov ah,4chint 21h

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. 43Web site Examples

Reading the MS-DOS Command TailReading the MS-DOS Command Tail

Get_Commandtail PROCpush espusha ; save general registers

mov ah,62h ; get PSP segment ;address

int 21h ; returned in BXmov es,bx ; copied to ES

mov si,dx ; point to buffermov di,81h ; PSP offset of command ;tailmov cx,0 ; byte countmov cl,es:[di-1] ; get length bytecmp cx,0 ; is the tail empty?je L2 ; yes: exitcld ; no: scan forwardmov al,20h ; space characterrepz scasb ; scan for non spacejz L2 ; all spaces founddec di ; non space foundinc cx

L1: mov al,es:[di]

mov [si],al

inc siinc diloop L1clcjmp L3

L2: stcL3: mov byte ptr [si],’$’

popapop esret

Get_Commandtail ENDP

End a