組合語言 如何改寫中斷例程(例項 射擊遊戲)

2021-08-21 20:11:31 字數 2554 閱讀 3272

編寫程式乙個「射擊遊戲」,用上、下、左、右鍵控制跳上、跳下、裝子彈、射擊的動作,按esc鍵退出遊戲。

執行效果如圖所示:

編寫的程式如下:

assume cs:code, ss:stack, ds: data

stack segment

db 256 dup (0)

stack ends

data segment

dw 0,0

run db 'game is runing...',0dh,0ah,'$'

up db 'jump up...',0dh,0ah,'$'

down db 'jump down...',0dh,0ah,'$'

right db 'shoot...',0dh,0ah,'$'

left db 'get bullet...',0dh,0ah,'$'

over db 'byebye...',0dh,0ah,'$'

data ends

code segment

start:

mov ax,stack

mov ss,ax

mov sp,128

mov ax,data

mov ds,ax

mov si,0

mov dx,0

; 改中斷例程入口位址

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

play: lea dx, run

mov ah,9

int21h

call delay

jmp play

delay:push ax ; 延時

push dx

mov dx,10h

mov ax,0

s1:sub ax,1

sbb dx,0

cmp ax,0

jne s1

cmp dx,0

jne s1

pop dx

pop ax

ret; 定義中斷例程

int9: push ax

push bx

push es

push dx

in al,60h

pushf

pushf

pop bx

and bh,11111100b

push bx

popf

call dword ptr ds:[0]

shang: cmp al,48h; ↑的掃瞄碼48

jne xia

lea dx, up

jmp show

xia:cmp al,50h;↓的掃瞄碼50

jne zuo

lea dx,down

jmp show

zuo:cmp al,4bh;←的掃瞄碼4b

jne you

lea dx, left

jmp show

you:cmp al,4dh;→的掃瞄碼4d

jne fanhui

lea dx, right

show:mov ax,9

int21h

jmp int9ret

fanhui: cmp al,01h;esc的掃瞄碼01

jne int9ret

lea dx, over

mov ah,9

int21h

mov ax,0

mov es,ax

; 恢復原來的位址

push ds:[0]

pop es:[9

*4] push ds:[2]

pop es:[9

*4+2]

mov ax,4c00h

int21hint9ret:

pop dx

pop es

pop bx

pop ax

iret

code ends

end start

執行結果截圖:

其實最後從執行上來講還是存在一些問題的,就算改了上下左右四個鍵的中斷歷程,在執行時按這幾個鍵還是會崩潰(估計是改的中斷有問題),不過改的其他幾個鍵的中斷沒有這種問題,可惜我實在找不到問題所在,期待哪位朋友偶然看到這裡,肯給指點一下迷津。晚輩先在這裡謝過!!!

組合語言 安裝中斷例程

1 assume cs code23 stack segment 4 db 128 dup 0 5 stack ends67 code segment 8start movax,stack 9mov ss,ax 10mov sp,128 1112 push cs13 popds 1415 mov a...

組合語言 BIOS和DOS中斷例程

強烈推薦人工智慧學習 bios和dos提供的中斷例程是如何安裝到記憶體中的呢?1 開機後,cpu一加電,初始化 cs 0ffffh,ip 0,自動從ffff 0單元開始執行程式。ffff 0處有一條轉跳指令,cpu執行該指令後,轉去執行bios中的硬體系統檢測和初始化程式。2 初始化程式將建立bio...

組合語言 內中斷

內中斷 在執行完當前正在執行的指令之後,檢測到從cpu內部發過來的或內部產生的一種特殊資訊,並且可以立即對所接收到的資訊進行處理 中斷資訊可以 於cpu外部和內部 內中斷的產生 1除法錯誤 0 2單步執行 1 3執行into指令 4 into 溢位中斷 4執行int指令,該指令格式 int n 指令...