正如标题所示,我正在尝试用我自己的处理程序替换 DOS 中定时器中断的现有处理程序。
在广泛搜索各种解决方案之后,我找到了一些完全可以做到这一点的汇编代码,我什至成功地编译和测试了它,并发现它可以工作。
现在的问题是我找到的代码(见下文)是为 TASM 编写的,我希望将它与我正在编写的一些 C 代码一起使用,我用 GCC 编译这些代码。
我尝试将代码转换为 GAS(GNU 汇编器)语法,但我似乎无法让它工作(在我的多次尝试中,我大多经历过一种或另一种崩溃)。
如果有人能启发我一个解决方案(无论是 GAS 可以编译的汇编代码的工作版本,还是用 C 语言完成整个事情的方法——“中断”关键字不起作用,我将非常感激)也没有“属性((interrupt))”之类的——甚至是 TASM 和 GCC 之间的桥梁)。
我还应该提到,我使用的 DOS 系统实际上是一个 OracleVM VirtualBox Manager,运行一个安装了 FreeDOS 的虚拟机,并且我使用的 C 编译器是 DJGPP 开发环境提供的 GCC 。
这是我的工作 TASM 代码(取自http://www.programmersheaven.com/mb/x86_asm/276128/276185/re-redefining-the-timer-interrupt-handler/):
_stack SEGMENT STACK
db 32 DUP ('STACK ')
_stack ENDS
_code SEGMENT PARA 'CODE'
ASSUME CS:_code, SS:_stack
Lstart LABEL NEAR
JMP Linstall
;+---------------------------------------------
;| My New 1Ch INT
;| Print 'random' chars to the first video line
new_Int PROC FAR
DEC BYTE PTR CS:Counter
CLD
PUSH AX
MOV AX, 0B800h
MOV ES,AX ; ES = b800h
MOV DI,000h ; DI = 0000h
MOV AH,CS:Counter ; set foreground and background color
MOV AL,CS:Counter ; set char
MOV CX,80
REP STOSW ; From AX to ES:DI
POP AX
STI
IRET
new_Int ENDP
Counter DB 0Fh
;+-----------------------------------------
;| Store old INT and Install the new one
;|
Linstall LABEL NEAR
old_INT DD 00000000h
MOV AL,01Ch ;+-
MOV AH,35h ;| Save old_INT
INT 21h ;|
MOV WORD PTR [old_INT],BX
MOV WORD PTR [old_INT][2],ES
CLI ;+-
PUSH CS ;| Install
POP DS ;|
LEA DX,new_INT
MOV AL,1Ch
MOV AH,25h
INT 21h
MOV AH,0 ;+-
INT 16H ;| Wait for a keypress
;+-----------------------------------------
;| Disinstall and exit
CLI
PUSH DS
LDS DX,CS:[old_INT] ;+-
MOV AL,1Ch ;| Disinstall int
MOV AH,25h ;|
INT 21h ;|
POP DS
STI
MOV AL,0 ;+-
MOV AH,4Ch ;| Exit
INT 21h ;|
_code ENDS
END Lstart
它在我的机器上完全可以工作。我启动程序,看到控制台的整个第一行都被不断变化的彩色字符所取代。
这是我尝试将上面的代码转换为 GAS 语法:
.file "ttv2.s"
# Define a variable for "randomizing" characters and colors
.globl _MyVar
.section .bss
_MyVar:
.space 1
.section .text
# Define a variable for storing the address of the current ISR
.globl _OldInt
.section .bss
.p2align 2
_OldInt:
.space 4
.section .text
# Program entry point
.text
.globl start
start:
jmp _Install
# This is the new Interrupt Service Routine that is going to be installed
.globl _NewInt
_NewInt:
movb _MyVar, %al
decb %al # Decrement our variable
movb %al, _MyVar
cld
pushw %ax
movw $0xB800, %ax
movw %ax, %es # ES = 0xB800
movw $0, %di # DI = 0
movb _MyVar, %ah # Set the foreground and background colors
movb _MyVar, %al # Set the charater to be displayed
movw $80, %cx # The screen is 80 characters wide
rep stosw # Start copying from AX to AS:DI
popw %ax
sti
iret
.globl _Install
_Install:
# Save old ISR address
movb $0x1C, %al # Set the code for the Timer interrupt
movb $0x35, %ah # 0x35 is the code for getting the current ISR
int $0x21 # 0x21 is the interrupt fot s/getting ISRs
movw %es, %dx #
shll $16, %edx # Save the address of the
movw %bx, %dx # old interrupt handler
movl %edx, _OldInt #
# Install the new ISR
cli
pushw %cs
popw %ds
lea _NewInt, %dx # Set the address of the ISR we're installing
movb $0x1C, %al # Set the code for the Timer interrupt
movb $0x25, %ah # 0x25 is the code for setting a new ISR
int $0x21 # 0x21 is the interrupt fot s/getting ISRs
# Wait for a key press
movl $0, %eax
int $0x16
.globl _Uninstall
_Uninstall:
cli
pushw %ds
lds %cs:_OldInt, %dx # Install the address of the old ISR
movb $0x1C, %al # Set the code for the Timer interrupt
movb $0x25, %ah # 0x25 is the code for setting a new ISR
int $0x21 # 0x21 is the interrupt fot s/getting ISRs
popw %ds
sti
.globl _End
_End:
# Exit
movb $0, %al
movb $0x4C, %ah # 0x4C is the code for program exit in DOS
int $0x21
.ident "GCC: (GNU) 4.5.2"
我使用以下命令编译我的文件(称为“ttv2.s”):
as -o ttv2.o ttv2.s
ld -o ttv2.exe ttv2.o
当我运行生成的 EXE 文件时(在汇编和链接过程中没有警告或错误),程序崩溃并出现错误“Exception 0D in Ring 0”(以及大量寄存器值)。
然而,TASM 版本可以顺利运行!
所以我猜测我转换代码的方式或者我构建最终 EXE 的方式有问题。或两者。
一些额外的信息,如果有任何帮助的话:
- If I remove安装命令(
int $0x21
),没有崩溃,程序等待我按键然后退出。
- If I keep安装命令,但是remove等待按键命令(
int $0x16
),程序立即退出,没有crash。
- If I keep安装命令,以及replace带有活动延迟循环(40 亿次迭代的简单循环)的 wait-for-key 命令,程序崩溃的方式与 wait-for-key 命令到位时的情况相同,但在几秒钟后,而不是比立即。
- 在这两种崩溃的情况下(通过按键或延迟),即使我只删除两个安装命令之一,程序也会崩溃。
预先感谢您提供的所有帮助,并对冗长的帖子表示歉意......