首页 文章

来自中断服务程序的奇怪行为

提问于
浏览
3

我正在编写一个中断服务程序,它应该使用int 70h和IRQ8来处理由RTC引起的中断,以便与某个定时器一起使用 . 不幸的是,我遇到了很多问题,因此我决定将问题分成几个较小的问题,然后独立解决每个问题 . 首先,我放弃了硬件部分并决定首先在软件中实现中断 .

现在,我正在使用NASM和DosBox .

这是ISR代码:

segment .code
; ----------------------------------------------
; writes a message on the screen
; every time interrupt is called
; ----------------------------------------------

INT_CODE equ 070h

my_int:
    pusha           ;saves all registers on stack so they get preserved

    ;EDIT1
    xor ax, ax      ;sets ax to zero
    mov es, ax      ;puts zero into extra segment register
    mov bx, [es:INT_CODE*4+2] ;this should give us the sergment of the ISR
    mov ds, bx      ;the segment is now in ds
    ;END OF EDIT1

    ;mov ax, 0      ;cleans any garbage in ax

    ;mov ah, 09h    ;preparing to call DOS system call, remove later
    mov ax, string_s
    mov si, ax
    call _print_string


    ;int 021h       ;this should hopefully work

    mov al, 0Ch     ; Accessing RTC
    out 070h, al    ; register C should be read
    in al, 071h     ;or there won't be any new interrupts (or so it's supposed to be)

    ;mov ax, 0      ; again we clear anything left in ax, just in case
    ;mov ah, 09h    ; preparing to write string
    mov ax, string_e
    mov si, ax
    call _print_string
    ;int 021h       ; this should work

    mov al, 20h     ;we're letting PICs know the interrupt ended
    out 0A0h, al    ;notifying second PIC
    out 020h, al    ;notifying first PIC

    popa            ;application gets its registers back

    iret

_inst_70:
    cli             ;hardware interrupts are now stopped
    xor     ax, ax
    mov     es, ax
    mov     bx, [es:INT_CODE*4]
    mov     [old_int70_off], bx
    mov     bx, [es:INT_CODE*4+2]
    mov     [old_int70_seg], bx

; adding our routine to interrupt vector table
    mov     dx, my_int
    mov     [es:INT_CODE*4], dx
    mov     ax, cs
    mov     [es:INT_CODE*4+2], ax
    sti

    ;mov ah, 09h

    mov ax, string_inst
    mov si, ax
    call _print_string
    ;int 021h

    ret

; -----------------------------------------------------------------------------
; return old int 70 h

_uninst_70:
    cli
    xor     ax, ax
    mov     es, ax
    mov     ax, [old_int70_seg]
    mov     [es:INT_CODE*4+2], ax
    mov     dx, [old_int70_off]
    mov     [es:INT_CODE*4], dx
    sti
    ret

_print_string:

    pusha
    mov     ah, 0Eh                     ; BIOS INT 10h teletype (TTY) function
.Repeat:
    lodsb                               ; takes one character from a string
    cmp     al, 0
    je     .End                         ; If it's zero, end of string
    int     10h                         ; if not, call BIOS
    jmp    .Repeat                      ; and go to next character
.End:
    popa
    ret

segment .data

string_s: db 'We're in ISR',0
string_e: db 'It's working',0
string_inst: db 'Installed',0

old_int70_seg: dw 0
old_int70_off: dw 0

我正在使用以下程序测试此中断:

;myint
org 100h;installs the interrupt
segment .code

main:
    call   _inst_70

    ;call   _uninst_70 ; THIS IS ON PURPOSE!
    ret

%include "myint.asm"

;int70h
org 100h            ;calls the interrupt
segment .code
    mov ah, 09h     ; getting ready to print string
    mov dx, string1
    int 21h

    ;mov ax, 0      ;getting rid of the last message
    ;mov dx, 0

    int 070h        ;calling the interrupt

    mov ah, 09h
    mov dx, string2;
    int 21h

    ret

segment .data
string1: db 'Testing!',0
string2: db 'int 70h working',0
_print_string:

        pusha
        mov     ah, 0Eh         ; BIOS INT 10h teletype (TTY) function
.Repeat:
        lodsb                   ; takes one character from a string
        cmp     al, 0
        je     .End             ; If it's zero, end of string
        int     10h             ; if not, call BIOS
        jmp    .Repeat          ; and go to next character
.End:
        popa
        ret

Now we're getting to the interesting part.

当我调用安装程序时,我收到安装中断的消息,程序似乎结束了 .

当我打电话给INT70H.COM时,我得到了一个似乎是内存区域的转储 . 唯一可读的东西是: Testing!Testing!int 70h workingC:\NASM-DOS\NASM.EXE .

当我取消注释INT70H中的 mov ax, 0mov dx, 0 行时,我得到 Testing! 并且DosBox挂起并且有时会崩溃 . VMware和VirtualBox也是如此 .

当我注释掉正在读取RTC的寄存器C的行时,INT70H的两个mov被评论,我得到 Testing!Testing!int 70h working 并且DosBox挂起 . VirtualBox和VMware也发生了同样的事情 . 如果取消注释INT70H中的两个mov,我会得到 Testing! 并挂起 .

这让我相信它可能是一些DOS系统调用(我不应该在最终产品中使用)可能做坏事,但即使他们注释掉,当我运行INT70H时,计算机挂起 .

我的主要问题是,现在我完全不知道如何开始解决这个问题 .

3 回答

  • 1

    中断服务程序必须在执行任何依赖于它们的操作之前设置段寄存器 . 调用中断时,它可以具有系统中绝对任何内容的上下文 . 打印字符串的调用特别成问题,因为它们依赖 ds:dx 作为字符串地址,但未设置 ds .

    除此之外,它表面看起来很好 . 看看设置 ds 是否解决了挂起问题 . 如果没有,请跟进 .

  • 0

    中断服务程序(ISR)必须保存它使用的任何寄存器并将其恢复(以便被中断的软件不会随机删除寄存器) . 这包括段寄存器(例如DS和ES) . 你需要“ push ds " and " push es " near the start of your ISR and the corresponding " pop " instructions before the " iret” .

    没有任何BIOS功能是可重入的,因此它在主代码和ISR中用于中断主代码 . 如果它只是用于测试;尝试直接写入显示内存(例如,对于文本模式,“ mov ax,0xB800; mov es,ax; inc word [es:0] ”) .

    对于测试OS代码,通常更容易测试OS代码 . 例如,如果这是在故意没有返回(锁定)的引导扇区中实现的;那么你就不必费心去保存/恢复以前的IVT条目了,或者担心DOS和/或任何TSR在后台做什么可能会干扰你的测试;并且您可以在Bochs之类的东西中调试它,而无需先在虚拟机中安装DOS / FreeDOS . 作为额外的奖励,您可以在目标操作模式(例如,可能是32位保护模式)下执行此操作,而不是稍后重写16位实模式代码 .

  • 1

    这是一个非常奇怪的错误 . 非常感谢所有帮助过的人,但最后,事实证明,当我设置RTC时,我没有在写入之前将输出寄存器设置为B.现在它工作正常 .

相关问题