汇编程序:显示时间中响应键盘中断

来源:互联网 发布:会展中心 网络 编辑:程序博客网 时间:2024/05/16 19:05

【任务】
  在屏幕的左上角动态显示时间,期间,按下Home键后,能显示”Home”,按下End键后,退出程序。

【参考解答】

assume cs:codestack segment     db 128 dup (0)stack endsdata segment     dw 0,0home db 'Home',0dh,0ah,'$'data endscode segmentstart:      mov ax,stack      mov ss,ax      mov sp,128      mov ax,data      mov ds,ax      ; 改中断例程入口地址      mov ax,0      mov es,ax      push es:[9*4]      pop ds:[0]      push es:[9*4+2]      pop ds:[2]      mov word ptr es:[9*4],offset int9      mov es:[9*4+2],cs      ; 显示时间show: mov al,2  ;分      out 70h,al      in al,71h      mov ah,al      mov cl,4      shr ah,cl      and al,00001111b      add ah,30h      add al,30h      mov bx,0b800h      mov es,bx      mov byte ptr es:[0],ah      mov byte ptr es:[1],01001111b      mov byte ptr es:[2],al      mov byte ptr es:[3],01001111b      mov byte ptr es:[4],':'      mov byte ptr es:[5],01001111b      mov al,0    ;秒      out 70h,al      in al,71h      mov ah,al      mov cl,4      shr ah,cl      and al,00001111b      add ah,30h      add al,30h      mov bx,0b800h      mov es,bx      mov byte ptr es:[6],ah      mov byte ptr es:[7],01001111b      mov byte ptr es:[8],al      mov byte ptr es:[9],01001111b      jmp show    ; 定义中断例程int9:      push ax      push bx      push dx      push es      in al,60h      pushf      pushf      pop bx      and bh,11111100b      push bx      popf      call dword ptr ds:[0]      mov bl, al ;保存al      cmp al,47h ; 47h是HOME键的扫描码      jne ifend      ;处理HOME      lea dx, home      mov ah,9      int 21h      jmp int9ret ifend: cmp bl, 4Fh ;4Fh是end键的扫描码      jne int9ret      ;处理END,使程序结束,注意在此要恢复中断向量      mov ax,0      mov es,ax      push ds:[0]      pop es:[9*4]      push ds:[2]      pop es:[9*4+2]      mov ax,4c00h      int 21hint9ret:pop es      pop dx      pop bx      pop ax      iretcode endsend start

【说明】
  本程序是汇编程序:显示时间(分秒)的扩充,实现了显示时间过程中,键盘中断做一些处理,这已经有了一点实用的模型,可以再自行扩充,例如用于战斗类游戏设计中,按下方向键,实现开火、加速等,这些都好控制了。
  还可以在主程序中完成一定的功能,将显示时间也由定时器控制,用中断机制完成,这个可以作为时一步学习的方向。

原创粉丝点击