鱼C论坛

 找回密码
 立即注册
查看: 2399|回复: 0

[学习笔记] 第十五章 外中断

[复制链接]
发表于 2017-12-5 16:24:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 若余相思 于 2017-12-5 16:24 编辑

这章将学习外中断,外中断就是CPU对于外部中断的处理,比如:键盘的输入

要及时处理外设的输入,显然需要解决两个问题:
(1)外设的输入输出随时可能发生,CPU如何得知?
(2)CPU如何得到外设的输入

15.1节   接口芯片和端口

外设的输入不直接送入内存和CPU,而是送入相关的接口芯片的端口中,
CPU向外设的输出也不是直接送入外设,而是先送入端口,再有相关的芯片送到外设

可见,CPU通过端口和外部设备进行联系

15.2节   外中断信息

外中断源一共分位两种
1、可屏蔽中断
2、不可屏蔽中断

在可屏蔽中断中,CPU可以不响应中断,响不响应主要看IF位,IF位为1
则CPU要响应这个可屏蔽中断,IF为0可以不响应

如下指令设置IF的值
sti        设置IF = 1
cli        设置IF = 0

在不可屏蔽中断中,CPU必须立即响应该中断,不可屏蔽中断的类型码固定为2,
所在中断过程中可以不取中断类型码

不可屏蔽中断是系统在有必要处理的紧急情况发生时来通知CPU的中断信心,在课程中主要讲解可屏蔽中断

15.3节   PC机键盘处理过程

1、键盘输入
当你在键盘上按下一个字符时,就会产生扫描码(通码),扫描码被送入寄存器,该寄存器的端口为60H;你在松开一个键盘字符时也会产生扫描码(断码),也是被送入60H端口。

断码= 通码+ 80H
如:g键的通码为22H,则断码为a2h。
下面列出键盘部分扫描码:


2、引发int9中断
键盘的输入到达了60h端口时,相关芯片就会向CPU发出类型码为9的可屏蔽中断,如果IF = 1,则响应中断。


15.4节   编写int9中断例程

编程:在屏幕一次显示"a" ~ "z",并让人看清楚,在显示过程中,按下esc键后,改变显示的颜色

来先看看完整的代码:
  1. assume cs: code

  2. stack segment

  3.         db 128 dup (0)
  4. stack ends

  5. data segment
  6.         dw 0, 0
  7. data ends

  8. code segment
  9. start :                        mov ax, stack
  10.                                 mov ss, ax
  11.                                 mov sp, 128
  12.                                
  13.                                 mov ax, data
  14.                                 mov ds, ax
  15.                                
  16.                                 mov ax, 0
  17.                                 mov es, ax
  18.                                
  19.                                 push es:[9*4]
  20.                                 pop ds:[0]
  21.                                 push es:[9 * 4 + 2]
  22.                                 pop ds:[2]
  23.                                
  24.                                 mov word ptr es:[9 * 4 ], offset int9
  25.                                 mov word ptr es:[9 * 4 + 2], cs
  26.                                
  27.                                
  28.                                 mov ax, 0b800h
  29.                                 mov es, ax
  30.                                 mov ah, 'a'
  31.                         s:        mov es:[160 * 12 + 80 ], ah
  32.                                 call delay
  33.                                 inc ah
  34.                                 cmp ah, 'z'
  35.                                 jna s
  36.                                
  37.                                 mov ax, 4c00H
  38.                                 int 21H
  39.                                
  40.                                 mov ax, 0
  41.                                 mov es, ax
  42.                                
  43.                                 push ds:[0]
  44.                                 pop es:[9 * 4]
  45.                                 push ds:[2]
  46.                                 pop es:[9 * 4 + 2]
  47.                                
  48.                                 mov ax, 4c00H
  49.                                 int 21H
  50.                                
  51.                                
  52.         delay:                push ax
  53.                                 push dx
  54.                                 mov ax, 0
  55.                                 mov dx, 1000H
  56.                                
  57.                 s1:                sub ax, 1
  58.                                 sbb dx, 0
  59.                                 cmp ax, 0
  60.                                 jne s1
  61.                                 cmp dx, 0
  62.                                 jne s1
  63.                                
  64.                                 pop dx
  65.                                 pop ax
  66.                                 ret
  67.                                
  68.                 int9:                push ax
  69.                                 push bx
  70.                                 push es
  71.                
  72.                                 in al, 60H
  73.                                
  74.                                 pushf
  75.                                
  76.                                 pushf
  77.                                 pop bx
  78.                                 and bh, 11111100b
  79.                                 push bx
  80.                                 popf
  81.                                
  82.                                 call dword ptr ds:[0]
  83.                                
  84.                                 cmp al, 1
  85.                                 jne int9ret
  86.                                
  87.                                 mov ax, 0b800h
  88.                                 mov es, ax
  89.                                 inc byte ptr es:[12*160 + 80 + 1]
  90.                                
  91. int9ret:                        pop es
  92.                                 pop bx
  93.                                 pop ax
  94.                                 iret
  95.                                
  96.                                
  97.                                
  98. code ends
  99. end start
  100.                                

  101.                                
  102.                                
复制代码


写虽然困难,但是理解了就觉得挺简单的



在接着15.5节的编程

代码:
  1. assume cs:code

  2. stack segment
  3.         db 128 dup (0)
  4. stack ends

  5. code segment
  6. start:       
  7.         mov ax,stack
  8.         mov ss,ax
  9.         mov sp,128

  10.         push cs
  11.         pop ds

  12.         mov ax,0
  13.         mov es,ax

  14.         mov si,offset int9                                        ;设置ds:si指向源地址
  15.         mov di,204h                                                        ;设置es:di指向目的地址
  16.         mov cx,offset int9end - offset int9        ;设置cx为传输长度
  17.         cld                                                                        ;设置传输方向为正
  18.         rep movsb       

  19.         push es:[9*4]
  20.         pop es:[200h]
  21.         push es:[9*4+2]
  22.         pop es:[202h]

  23.         cli
  24.         mov word ptr es:[9*4],204h
  25.     mov word ptr es:[9*4+2],0
  26.         sti
  27.        
  28.         mov ax,4c00h
  29.         int 21h

  30. int9:       
  31.         push ax
  32.         push bx
  33.         push cx
  34.         push es
  35.         
  36.         in al,60h

  37.         pushf        
  38.     call dword ptr cs:[200h]                 ;当此中断例程执行时(CS)=0

  39.         cmp al,3bh                                                ;F1的扫描码为3bh
  40.         jne int9ret

  41.         mov ax,0b800h
  42.         mov es,ax
  43.         mov bx,1
  44.         mov cx,2000
  45.    
  46. s:       
  47.         inc byte ptr es:[bx]
  48.         add bx,2
  49.         loop s

  50. int9ret:
  51.         pop es
  52.         pop cx
  53.         pop bx
  54.         pop ax
  55.         iret

  56. int9end:
  57.         nop

  58. code ends

  59. end start
复制代码



本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 18:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表