16位MS - DOS编程及相关知识详解
1 16位MS - DOS编程基础
1.1 MS - DOS和IBM - PC相关问题解答
| 问题 | 答案 |
|---|---|
| 某个地址值 | 9FFFFh |
| 特定表 | Interrupt vector table |
| 特定位置 | 00400h |
| 负责某功能的系统 | The BIOS |
| 将程序输出重定向到默认打印机的命令(假设程序名为myProg.exe) | myProg > prn |
| 默认打印机设备名 | LPT1 |
| 中断服务程序的定义 | An interrupt service routine (also called an interrupt handler) is an operating system procedure that (1) provides basic services to application programs, and (2) handles hardware events. |
| 中断发生时的操作 | Push the flags on the stack |
| 中断处理的步骤 | 见相关文档的四个步骤 |
| 中断处理结束的操作 | The interrupt handler executes an IRET instruction |
| 特定中断号 | 10h |
| 特定中断号 | 1Ah |
| 中断号21h对应的向量表地址 | 21h * 4 = 0084h |
1.2 MS - DOS函数调用(INT 21h)
|问题|答案|
|函数调用参数寄存器|AH|
|退出程序的函数|Function 4Ch|
|写单个字符的函数|Functions 2 and 6|
|显示字符串的函数|Function 9|
|写数据块的函数|Function 40h|
|读单个字符的函数|Functions 1 and 6|
|读文件的函数|Function 3Fh|
|获取和设置日期时间的函数|Functions 2Ah and 2Bh(显示时间可调用WriteDec过程)|
|设置系统日期和时间的函数|Functions 2Bh (set system date) and 2Dh (set system time)|
|某特定功能函数|Function 6|
1.3 标准MS - DOS文件I/O服务
1.3.1 设备句柄说明
| 设备 | 句柄值 |
|---|---|
| 键盘(标准输入) | 0 |
| 控制台(标准输出) | 1 |
| 错误输出 | 2 |
| 辅助设备(异步) | 3 |
| 打印机 | 4 |
1.3.2 功能函数716Ch参数
AX = 716Ch
BX = access mode (0 = read, 1 = write, 2 = read/write)
CX = attributes (0 = normal, 1 = read only, 2 = hidden, 3 = system, 8 = volume ID, 20h = archive)
DX = action (1 = open, 2 = truncate, 10h = create)
DS:SI = segment/offset of filename
DI = alias hint (optional)
1.3.3 打开现有文件用于输入的代码示例
.data
infile BYTE "myfile.txt",0
inHandle WORD ?
.code
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
1.3.4 读取二进制数组的函数及参数
使用INT 21h Function 3Fh,参数如下:
AH = 3Fh
BX = open file handle
CX = maximum bytes to read
DS:DX = address of input buffer
1.3.5 判断文件结束的方法
调用INT 21h后,比较AX中的返回值和调用函数前CX中的值。如果AX较小,则表示已到达文件末尾。
1.3.6 从键盘和文件读取的区别
唯一区别是BX的值。从键盘读取时,BX设置为键盘句柄(0);从文件读取时,BX设置为打开文件的句柄。
1.3.7 移动文件指针的函数及代码示例
函数为Function 42h,代码示例(BX已包含文件句柄):
mov ah,42h ; move file pointer
mov al,0 ; method: offset from beginning
mov cx,0 ; offset Hi
mov dx,50 ; offset Lo
int 21h
2 磁盘基础知识
2.1 磁盘存储系统
| 问题 | 答案 |
|---|---|
| 某陈述判断 | True |
| 某陈述判断 | False |
| 磁盘结构术语 | Cylinder |
| 某陈述判断 | True |
| 扇区字节数 | 512 |
| 相邻柱面存储数据的原因 | For faster access because the closer are the cylinders, the smaller is the distance that the read/write heads must travel |
| 柱面间隔大的缺点 | The read/write heads must jump over other cylinders, wasting time and increasing the probability that errors will occur |
| 磁盘特定概念 | Volume |
| 寻道时间定义 | The average amount of time required to move the read/write heads between tracks |
| 磁盘格式化操作 | The marking of physical sectors on the disk surfaces |
| 主引导记录内容 | The master boot record contains the disk partition table and a program that locates a single partition’s boot sector and runs another program that loads the operating system |
| 磁盘分区数量 | One partition |
| 磁盘分区类型 | System |
2.2 文件系统
|问题|答案|
|某陈述判断|True|
|文件位置|No. It is in the disk directory|
|某陈述判断|False (all systems, including NTFS, require at least one cluster to store a file)|
|某陈述判断|False|
|某陈述判断|False|
|最大文件系统容量|4 GByte|
|常用文件系统|FAT32 and NTFS|
|某些特性更优的文件系统|NTFS|
|文件系统组成部分|Boot record, file allocation table, root directory, and the data area|
|特定信息位置|This information is at offset 0Dh in the boot record|
|存储文件所需簇及浪费字节数|Two 8 KByte clusters would be required, for a total of 16,384 bytes. The number of wasted bytes would be (16,384 - 8,200), or 8,184 bytes|
2.3 磁盘目录
|问题|答案|
|某陈述判断|True|
|某名称判断|False (it is called the root directory)|
|目录项内容判断|False (it contains the starting cluster number)|
|某陈述判断|True|
|目录项字节数|32 bytes|
|目录项包含信息|Filename, extension, attribute, time stamp, date stamp, starting cluster number, and file size|
|状态字节描述|The status bytes and their descriptions are listed in Table 15 - 5|
|时间位表示|Bits 0 to 4 表示 seconds; bits 5 to 10 表示 minutes; and bits 11 to 15 表示 hours|
|长文件名条目首字节|The first byte of the entry is 4xh, where x indicates the number of long filename entries to be used for the file|
|长文件名条目数量|Two filename entries|
|新字段|There are three new fields: last access date, create date, and create time|
|文件分配表链接|File allocation table links|
2.4 读写磁盘扇区
|问题|答案|
|某陈述判断|True|
|某功能运行模式判断|False (the function runs in real - address mode)|
|函数参数|AX: 7305h
DS:BX: Segment/offset of a DISKIO structure variable
CX: 0FFFFh
DL: Drive number (0 = default, 1 = A, 2 = B, 3 = C, etc.)
SI: Read/write flag|
|INT 10h功能|INT 10h displays special ASCII graphics characters without trying to interpret them as control codes (such as Tab and Carriage return)|
|错误判断及处理|The Carry flag is set if function 7305h cannot read the requested sector, and the program displays an error message. (Remember that you cannot test this program under Windows NT, 2000, XP, and beyond)|
2.5 系统级文件函数
| 功能 | 函数 |
|---|---|
| 某功能 | Function 7303h |
| 某功能 | Function 7303h |
| 创建子目录和设置当前目录 | Function 39h (create subdirectory) and Function 3Bh (set current directory) |
| 获取和设置文件属性 | Function 7143h |
3 BIOS级编程
3.1 键盘输入(INT 16h)
| 问题 | 答案 |
|---|---|
| 最佳键盘输入中断 | INT 16h is best |
| 键盘预输入缓冲区位置 | In the keyboard typeahead buffer, at location 0040:001E |
| INT 9h功能 | INT 9h reads the keyboard input port, retrieves the keyboard scan code, and produces the corresponding ASCII code. It inserts both in the keyboard typeahead buffer |
| 特定功能函数 | Function 05h |
| 特定功能函数 | Function 10h |
| 检查按键等待函数 | Function 11h examines the buffer and lets you know which key, if any, is waiting |
| 某陈述判断 | No |
| 特定功能函数 | Function 12h |
| 特定按键位 | Bit 4 |
| 检查Ctrl键按下的代码示例 |
L1:
mov ah,12h ; get keyboard flags
int 16h
test al,100h ; Ctrl key down?
jz L1 ; no: repeat the loop
; At this point, the Ctrl key has been pressed
|检查其他按键的代码示例|
L1:
cmp ah,1 ; ESC key's scan code?
je quit ; yes: quit
cmp ah,3Bh ; F1 function key?
je quit ; yes: quit
cmp ah,47h ; Home key?
je quit ; yes: quit
jmp L1 ; no: check buffer again
3.2 视频编程(INT 10h)
3.2.1 编程级别和模式切换
- 编程级别有MS - DOS level, BIOS level, and Direct video level。
-
在MS - Windows中切换到全屏模式的方法:
1. Create a shortcut to the program’s EXE file. Then open the Properties dialog for the shortcut, select the Screen properties, and select Full - screen mode.
2. Open a Command window from the Start menu, then press Alt - Enter to switch to full - screen mode. Using the CD (change directory) command, navigate to your EXE file’s directory, and run the program by typing its name. Alt - Enter is a toggle, so if you press it again, it will return the program to Window mode.
3.2.2 视频模式和相关参数
| 问题 | 答案 |
|---|---|
| 常用视频模式 | Mode 3 (color, 80 × 25) |
| 显示单元信息 | ASCII code and attribute (2 bytes) |
| 颜色组成 | Red, green, blue, and intensity |
| 背景和前景位 | Background: bits 4 to 7. Foreground: bits 0 to 3 |
| 设置光标位置函数 | Function 02h |
| 滚动屏幕函数 | Function 06h |
| 显示字符函数 | Function 09h |
| 设置光标形状函数 | Function 01h |
| 设置视频模式函数 | Function 00h |
| 设置光标位置参数 | AH = 2, DH = row, DL = column, and BH = video page |
| 隐藏光标方法 | There are two ways: (1) use INT 10h Function 01h to set the cursor’s top line to an illegal value, or (2) use INT 10h Function 02h to position the cursor outside the displayable range of rows and columns |
| 滚动屏幕参数 | AH = 6, AL = number of lines to scroll, BH = attribute of scrolled lines, CH & CL = upper left window corner, and DH & DL = lower right window corner |
| 显示字符参数 | AH = 9, AL = ASCII code of character, BH = video page, BL = attribute, and CX = repetition count |
| 获取光标位置函数 | Function 10h, Subfunction 03h (set AH to 10h and AL to 03h) |
| 清屏参数 | AH = 06h, and AL = 0 |
3.3 绘制图形(INT 10h)
| 问题 | 答案 |
|---|---|
| 绘制像素函数 | Function 0Ch |
| 绘制像素参数 | AH = 0Ch, AL = pixel value, BH = video page, CX = X - coordinate, and DX = Y - coordinate |
| 该方法缺点 | It’s very slow |
| 设置视频模式代码示例 |
mov ah,0 ; set video mode
mov al,11h ; to mode 11h
int 10h ; call the BIOS
|特定视频模式|Mode 6Ah|
|坐标转换公式|sx = (sOrigX + X)|
|坐标示例|a. (350,150) b. (375,225) c. (150,400)|
3.4 内存映射图形
| 问题 | 答案 |
|---|---|
| 某陈述判断 | False (each byte corresponds to 1 pixel) |
| 某陈述判断 | True |
| 模式13h特性 | Mode 13h maps each pixel’s integer value into a table of colors called a palette |
| 像素颜色索引作用 | The color index of a pixel identifies which color in the palette is to be used when drawing the pixel on the screen |
| 调色板条目组成 | Each entry in the palette consists of three separate integer values (0 to 63) known as RGB (red, green, blue). Entry 0 in the color palette controls the screen’s background color |
| 颜色示例 | (20,20,20), (63,63,63), (63,0,0) |
| 设置屏幕背景为亮绿色代码示例 |
; Set screen background color to bright green.
mov dx,3c8h ; video paletter port
mov al,0 ; index 0 (background color)
out dx,al
mov dx,3c9h ; colors go to port 3C9h
mov al,0 ; red
out dx,al
mov al,63 ; green (intensity = 63)
out dx,al
mov al,0 ; blue
out dx,al
|设置屏幕背景为白色代码示例|
; Set screen background color to white
mov dx,3c8h ; video paletter port
mov al,0 ; index 0 (background color)
out dx,al
mov dx,3c9h ; colors go to port 3C9h
mov al,63 ; red = 63
out dx,al
mov al,63 ; green = 63
out dx,al
mov al,63 ; blue = 63
out dx,al
3.5 鼠标编程
| 问题 | 答案 |
|---|---|
| 初始化鼠标函数 | Function 0 |
| 初始化鼠标代码示例 |
mov ax,0 ; reset mouse
int 33h ; call the BIOS
cmp ax,0 ; mouse not available?
je MouseNotAvailable ; yes: show error message
|隐藏和显示鼠标指针函数|Functions 1 and 2|
|隐藏鼠标指针代码示例|
mov ax,2 ; hide mouse pointer
int 33h
|获取鼠标位置和状态函数|Function 3|
|获取鼠标位置和状态代码示例|
mov ax,3 ; get mouse position and status
int 33h
mov mouseX,cx
mov mouseY,dx
|设置鼠标位置函数|Function 4|
|设置鼠标位置代码示例|
mov ax,4 ; set mouse position
mov cx,100 ; X - value
mov dx,400 ; Y - value
int 33h
|获取鼠标按钮按下信息函数|Function 5|
|获取鼠标按钮按下信息代码示例|
mov ax,5 ; get button press information
mov bx,0 ; button ID for left button
int 33h
test ax,1 ; left button currently down?
jne Button1 ; yes: jump to label
|获取鼠标按钮释放信息函数|Function 6|
|获取鼠标按钮释放信息代码示例|
mov ax,6 ; get button release information
mov bx,1 ; button ID
int 33h
test ax,2 ; right button released?
jz skip ; no - skip
mov mouseX,cx ; yes: save coordinates
mov mouseY,dx
skip:
|设置鼠标垂直限制函数|Function 8|
|设置鼠标垂直限制代码示例|
mov ax,8 ; set vertical limits
mov cx,200 ; lower limit
mov dx,400 ; upper limit
int 33h
|设置鼠标水平限制函数|Function 7|
|设置鼠标水平限制代码示例|
mov ax,7 ; set horizontal limits
mov cx,300 ; lower limit
mov dx,600 ; upper limit
int 33h
|鼠标坐标转换示例|
- 假设字符单元格为8像素×8像素,单元格位置为(8 * 20), (8 * 10),即160, 80。
- 单元格左上角为(8 * 22), (8 * 15),鼠标移到单元格中心为180, 124。
|鼠标发明信息|The mouse was invented by Douglas Engelbart in 1963 at the Stanford Research Institute. (Source: http://en.wikipedia.org/wiki/Computer_mouse)|
4 高级MS - DOS编程
4.1 定义段
| 问题 | 答案 |
|---|---|
| 段开始和结束声明 | SEGMENT declares the beginning of a segment, and ENDS declares the end of a segment |
| 获取段地址 | Returns the segment address of a data label or code label |
| ASSUME指令作用 | The ASSUME directive makes it possible for the assembler to calculate the offsets of labels and variables at assembly time |
| 段类型 | BYTE, WORD, DWORD, PARA, and PAGE |
| 组合类型 | PRIVATE, PUBLIC, MEMORY, STACK, COMMON, and AT |
| 特定段类型 | DWORD |
| 组合类型作用 | The combine type tells the linker how to combine segments having the same name |
| 使用AT组合类型定义段示例 |
bios SEGMENT AT 40h
|段类类型作用|A segment’s class type provides another way of combining segments, in particular, those with different names|
|代码示例|
mov al,es:[di]
|段地址示例|The third segment will also begin at address 1A060h|
4.2 运行时程序结构
| 问题 | 答案 |
|---|---|
| 程序执行检查流程 | The command processor checks to see if there is filename with extension COM in the current directory. If a file is found, it is executed. If a matching file is not found, see relevant description for the subsequent steps |
| 某陈述判断 | No |
| 应用程序内存特性 | Application programs loaded into the lowest 640K of memory. They are transient because when they finish executing, they are automatically unloaded from memory |
| 程序段前缀 | Program segment prefix |
| 特定信息位置 | At offset 2Ch inside the program segment prefix area |
| COM程序特点 | A COM program is a single - segment MS - DOS program. When stored on disk as a COM file, it is simply a binary image of the program when loaded into memory |
| 程序类型 | Tiny |
| 编译选项 | /T |
| COM程序内存大小 | 64 KByte |
| COM程序效率 | Not efficient because even the smallest COM program uses an entire 64K memory segment |
| 段数量 | One |
| 段寄存器设置 | All segment registers are set to offset zero within the program. The program, in turn, is loaded into memory at the first available segment location following other programs still in memory |
| ORG指令作用 | The ORG directive assigns a specific offset to the very next label or instruction following the directive |
| 加载模块 | Load module |
| 段寄存器指向 | DS and ES point to the program segment prefix area of the program |
| 内存分配 | MS - DOS automatically allocates all of available memory to a program when it is first loaded, unless the program’s EXE header specifically limits its maximum memory allocation size |
| 内存统计和修改程序 | The EXEMOD program displays statistics about a program’s memory usage, and also permits many settings in the EXE header to be modified |
| 查看重定位项数量方法 | Run the EXEMOD program, passing it the name of the EXE file. The last line of the display will show the number of relocation entries |
4.3 中断处理
| 问题 | 答案 |
|---|---|
| 特定中断处理操作 | It displays a message on the screen “Abort, retry, or ignore?” and terminates the current program |
| 中断向量内容 | A 32 - bit segment/offset address pointing to an interrupt handler |
| 特定中断向量地址 | At address 0000:0040h because 0040h equals 10h * 4 |
| 中断控制器芯片 | The 8259 Programmable Interrupt Controller chip |
| 清除中断标志指令 | The CLI (clear interrupt flag) instruction |
| 设置中断标志指令 | The STI (set interrupt flag) instruction |
| 最高优先级中断 | IRQ 0 has highest priority |
| 中断优先级示例 | Before the file has been created because the keyboard (IRQ 1) is at a higher priority than the disk drive (IRQ 14) |
| 键盘中断 | INT 9h |
| 中断处理返回操作 | An IRET instruction at the end of the interrupt handler returns control to the code that was running when the interrupt occurred |
| 设置和获取中断向量函数 | Functions 25h and 35h |
| 中断处理程序和内存驻留程序区别 | An interrupt handler is any procedure that takes over processing an interrupt. A memory - resident program remains in memory even after the program that installed it has ended. A memory - resident program does not necessarily have to be an interrupt handler |
| 终止并驻留程序 | A terminate and stay resident (TSR) program leaves part of itself in the memory when it exits. This is accomplished by calling INT 21h function 31h |
| 移除TSR程序方法 | The computer can be rebooted, or a special utility program can remove the TSR |
| 替代IRET的操作 | Rather than executing an IRET instruction when it finishes, it can instead execute a JMP to the address that was previously stored in the interrupt vector |
| 特定程序类型 | A terminate and stay resident (TSR) program |
| 特定重启组合键 | Ctrl + Alt + Right shift + Del |
超级会员免费看
2

被折叠的 条评论
为什么被折叠?



