• 沒有找到結果。

Chapter 13: 16-Bit MS-DOS Programming

N/A
N/A
Protected

Academic year: 2022

Share "Chapter 13: 16-Bit MS-DOS Programming"

Copied!
32
0
0

加載中.... (立即查看全文)

全文

(1)

Assembly Language for Intel

Assembly Language for Intel - - Based Based Computers, 4

Computers, 4

thth

Edition Edition

Chapter 13: 16-Bit MS-DOS Programming

Kip R. Irvine

(2)

Chapter Overview Chapter Overview

• MS-DOS and the IBM-PC

• MS-DOS Function Calls (INT 21h)

(3)

MS MS - - DOS and the IBM DOS and the IBM - - PC PC

• Real-Address Mode

• MS-DOS Memory Organization

• MS-DOS Memory Map

• Redirecting Input-Output

• Software Interrupts

• INT Instruction

• Interrupt Vectoring Process

• Common Interrupts

(4)

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

(5)

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

(6)

MS MS - - DOS Memory Map DOS Memory Map

ROM BIOS Reserved

Video Text & Graphics

Video Graphics

Resident Command Processor DOS Kernel, Device Drivers Software BIOS

BIOS & DOS Data FFFFF

00400 A0000 B8000 C0000 F0000 Address

640K RAM Transient Program Area

(available for application programs) Transient Command Processor

VRAM

(7)

Redirecting Input

Redirecting Input - - Output Output

(1 of 2)(1 of 2)

• Input-output devices and files are interchangeable

• Three primary types of I/O:

• Standard input (console, keyboard)

• Standard output (console, display)

• Symbols borrowed from Unix:

• < symbol: get input from

• > symbol: send output to

• sort < myfile.txt > outfile.txt

• | symbol: pipe output from one process to another

• dir | sort > prn

• Predefined device names:

• PRN, CON, LPT1, LPT2, NUL, COM1, COM2

(8)

Redirecting Input

Redirecting Input - - Output Output

(2 of 2)(2 of 2)

• Standard input, standard output can both be redirected

• Suppose we have created a program named

myprog.exe that reads from standard input and writes to standard output. Following are MS-DOS commands that demonstrate various types of redirection.

myprog < infile.txt myprog > outfile.txt

myprog < infile.txt > outfile.txt

(9)

INT Instruction INT Instruction

• A software interrupt is a call to an operating system procedure.

• The INT instruction executes a software interrupt.

• INT pushes the CPU flags on the stack and calls an interrupt handler.

• 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.

(10)

Interrupt Vectoring Interrupt Vectoring

• Interrupt Vector Table is a table of addresses in the lowest 1,024 bytes of memory.

Each entry in this table is a 32-bit segment-offset address that points to an interrupt handler.

• Steps when the INT instruction is invoked.

1. With the number following the INT mnemonic, the CPU locates the entry of interrupt vector table.

2. The CPU pushes the flag on the stack, disables

hardware interrupt, and executes a call to the address stored in the interrupt vector table.

3. The interrupt handler begins execution and finishes when the IRET instruction is reached.

4. The IRET instruction causes the program to resume execution at the next instruction in the calling program.

(11)

Interrupt Vectoring Process Interrupt Vectoring Process

mov...

int 10h add...

F000:F065

3069 F000:AB62

F000:F065 F066 F067 F068 . .

sti cld

push es .

. IRET

1 2

3

Calling program

(entry for INT 10)

Interrupt Vector Table

Interrupt Handler

4

(12)

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

(13)

INT 4Ch: Terminate Process 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

(14)

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

(15)

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

(16)

INT 21h Functions 02h and 06h:

INT 21h Functions 02h and 06h:

Write Character to Standard Output 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

(17)

INT 21h Function 05h:

INT 21h Function 05h:

Write Character to Default Printer 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

(18)

INT 21h Function 09h:

INT 21h Function 09h:

Write String to Standard Output Write String to Standard Output

.data

string BYTE "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:

(19)

INT 21h Function 40h:

INT 21h Function 40h:

Write String to File or Device Write String to File or Device

.data

message "Writing a string to the console"

bytesWritten WORD ? .code

mov ah,40h mov bx,1

mov cx,LENGTHOF message 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

(20)

Selected Input Functions Selected Input Functions

• 01h, 06h - Read character from standard input

• 0Ah - Read array of buffered characters from standard input

• 0Bh - Get status of the standard input buffer

• 3Fh - Read from file or device

(21)

INT 21h Function 01h:

INT 21h Function 01h:

Read single character from standard input Read single character from standard input

.data

char BYTE ? .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

(22)

INT 21h Function 06h:

INT 21h Function 06h:

Read character from standard input without waiting Read character from standard input without waiting

.data

char BYTE ? .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 call DumpRegs ; display registers

• 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.

(23)

INT 21h Function 0Ah:

INT 21h Function 0Ah:

Read buffered array from standard input (1 of 2) Read buffered array from standard input (1 of 2)

count = 80

KEYBOARD STRUCT

maxInput BYTE count ; max chars to input inputCount BYTE ? ; actual input count buffer BYTE count DUP(?) ; holds input chars KEYBOARD ENDS

• Requires a predefined structure to be set up that describes the maximum input size and holds the input characters.

• Example:

(24)

INT 21h Function 0Ah

INT 21h Function 0Ah

(2 of 2)(2 of 2)

.data

kybdData KEYBOARD <>

.code

mov ah,0Ah

mov dx,OFFSET kybdData int 21h

Executing the interrupt:

(25)

INT 21h Function 0Bh:

INT 21h Function 0Bh:

Get status of standard input buffer 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

• Example: loop until a key is pressed. Save the key in a variable:

(26)

Example: String Encryption Example: String Encryption

XORVAL = 239 ; any value between 0-255 .code

main PROC

mov ax,@data mov ds,ax

L1: mov ah,6 ; direct console input

mov dl,0FFh ; don't wait for character int 21h ; AL = character

jz L2 ; quit if ZF = 1 (EOF) xor al,XORVAL

mov ah,6 ; write to output mov dl,al

int 21h

jmp L1 ; repeat the loop

Reads from standard input, encrypts each byte, writes to standard output. (encrypt < infile.txt > outfile.txt)

(27)

INT 21h Function 3Fh:

INT 21h Function 3Fh:

Read from file or device 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

• Read a block of bytes.

• Can be interrupted by Ctrl-Break (^C)

• Example: Read string from keyboard:

(28)

Date/Time Functions Date/Time Functions

• 2Ah - Get system date

• 2Bh - Set system date

• 2Ch - Get system time

• 2Dh - Set system time

(29)

INT 21h Function 2Ah:

INT 21h Function 2Ah:

Get system date 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

(30)

INT 21h Function 2Bh:

INT 21h Function 2Bh:

Set system date 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.

(31)

INT 21h Function 2Ch:

INT 21h Function 2Ch:

Get system time 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.

(32)

INT 21h Function 2Dh:

INT 21h Function 2Dh:

Set system time 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 not successful in modifying the time.

參考文獻

相關文件

➢The input code determines the generator output. ➢Understand the meaning of each dimension to control

• A function is a piece of program code that accepts input arguments from the caller, and then returns output arguments to the caller.. • In MATLAB, the syntax of functions is

In the work of Qian and Sejnowski a window of 13 secondary structure predictions is used as input to a fully connected structure-structure network with 40 hidden units.. Thus,

• BP can not correct the latent error neurons by adjusting their succeeding layers.. • AIR tree can trace the errors in a latent layer that near the front

Keyboard, mouse, and other pointing devices; touch screens, pen input, other input for smart phones, game controllers, digital cameras, voice input, video input,. scanners

ReadInt - Reads 32-bit signed decimal integer from keyboard ReadKey – Reads character from keyboard input buffer.. ReadString - Reads string from standard input, terminated

ReadInt - Reads a 32-bit signed decimal integer from standard input, terminated by the Enter key. ReadString - Reads a string from standard input, terminated by the

•The running time depends on the input: an already sorted sequence is easier to sort. •Parameterize the running time by the size of the input, since short sequences are easier