首页 > 解决方案 > 如何在汇编8086中显示中断向量表?

问题描述

我想在程序集 8086 中的代码中显示中断向量表,然后我希望它在第一个空闲向量处停止。
问题是:查看中断向量表并确定第一个空闲向量。

我知道表的第一个向量的地址是0000h,所以我尝试将cs段寄存器设置为它,我做不到?我试过了:mov cs,0mov bx,0 mov cs,bx没有奏效。
然后我又试call cs:offset 0000h了一次还是不行。那么我该怎么做呢?

标签: assemblyx86-16emu8086

解决方案


问题是:查看中断向量表并确定第一个表空闲向量

这是一个双重问题。

要显示所涉及的数字,您可以阅读使用 DOS 显示数字

要找到包含 0:0 的中断向量表 (IVT) 中的第一个插槽,您可以使用以下代码:

  xor si, si    ; Set DS:SI to the start of the IVT
  mov ds, si
  cld           ; Have LODSW increment (by 2) the SI register
Again:
  lodsw         ; The offset part of a vector
  mov dx, ax
  lodsw         ; The segment part of a vector
  or  ax, dx
  jz  Found     ; If both parts are zero, then their OR will set ZF=1 (Zero Flag)
  cmp si, 1024
  jb  Again     ; Repeat until the end of the IVT which is at address 1024
NotFound:
  ...
  jmp ..
Found:
  sub si, 4     ; -> DS:SI is the address of the first slot containing 0:0

包含 0:0 的 IVT-slot 肯定是空闲的,但它是否是第一个空闲的时隙并不一定正确。在Find a free interrupt slot中@Margaret Bloom 的回答中阅读更多相关信息。

[编辑]

一种更优雅的解决方案,它也更短但速度稍慢,并且减少了一个寄存器(DX不使用寄存器):

  xor si, si    ; Set DS:SI to the start of the IVT
  mov ds, si
Again:
  mov ax, [si]  ; The offset part of a vector
  or  ax, [si+2]; The segment part of a vector
  jz  Found     ; If both parts are zero, then their OR will set ZF=1 (Zero Flag)
  add si, 4
  cmp si, 1024
  jb  Again     ; Repeat until the end of the IVT which is at address 1024
NotFound:
  ...
  jmp ..
Found:
                ; -> DS:SI is the address of the first slot containing 0:0

这就是@Peter Cordes 在评论中提出的想法。它在循环中慢了 1 个时钟,但如果我们用 and 替换 and 指令,我们可以减少2add si, 2sub si, 2字节:inc si inc sidec si dec si

  xor si, si    ; Set DS:SI to the start of the IVT
  mov ds, si
  cld
Again:
  lodsw         ; The offset part of a vector
  or  ax, [si]  ; The segment part of a vector
  jz  Found     ; If both parts are zero, then their OR will set ZF=1 (Zero Flag)
  add si, 2
  cmp si, 1024
  jb  Again     ; Repeat until the end of the IVT which is at address 1024
NotFound:
  ...
  jmp ..
Found:
  sub si, 2     ; -> DS:SI is the address of the first slot containing 0:0

推荐阅读