汇编实验
任务2 - 射击游戏框架
编写程序一个“射击游戏”(有些太弱了哈),用上、下、左、右键控制跳上、跳下、装子弹、射击的动作,按ESC键退出游戏。
以下结合了汇编的简洁写法,学以致用emm
注释为个人理解,不用太过借鉴
// **借鉴自王爽著《汇编语言》第三版例题新int9中断例程的安装。**
.8086
.MODEL small
.data
db 16 dup(0) ;分配数据段字空间,否则出现显示字符串乱码
str1 db 'Game is running...',0ah,0dh,'$' ;字符串后加10,13实现显示后自动换行
str2 db 'Jump up...',0ah,0dh,'$' ;'$'用于DOS中断09h号功能的结束标志
str3 db 'Jump down...',0ah,0dh,'$'
str4 db 'get bullet...',0ah,0dh,'$'
str5 db 'shoot...',0ah,0dh,'$'
str6 db 'ByeBye...',0ah,0dh,'$'
.stack 100H
.code
start:
mov ax,@data
mov ds,axmov ax,0 ;将原来int9中断例程入口地址存入ds:0,ds:2单元中
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 ;在中断向量表中设置新int9中断入口地址
run:
mov dx,offset str1 ;show game is running
mov ah,09h ;Function 9 output string to standard output device screen
int 21h
call delay
jmp rundelay:
push ax
push dx
mov dx,10h ;每延迟一小段时间自动弹出str1中的字符串
mov ax,0
s1:
sub ax,1
sbb dx,0
cmp ax,0
jne s1
cmp dx,0
jne s1
pop dx
pop ax
retover:
mov ax,0 ;在中断向量表中恢复原中断入口地址
mov es,ax
push ds:[0]
pop es:[9*4]
push ds:[2]
pop es:[9*4+2] mov ah,4ch
int 21h
;-----------以下为新的int9中断例程--------------
int9:
push bx
push ax
push dx
push es
;mov bx,0
;mov dx,0
;mov ax,0
;以下模拟int指令,调用原来int9中断in al,60h ;从端口60h读数据到al寄存器
pushf
pushf
pop bx
and bh,11111100b ;标志寄存器第9位IF,第8位TF
push bx
popf ;IF=0,TF=0call dword ptr ds:[0] ;CS,IP入栈input:
cmp al,48h ;If it is the up key, control the "jump up"
je up
cmp al,50h ;If it is the down key, control the "jump down"
je down
cmp al,4bh ;If it is the left key, control the "get bullet"
je left
cmp al,4dh ;If it is the right key, control the "shoot"
je right
cmp al,01h ;If it is the Esc key, control the "ByeBye"
je quit
jmp int9ret
up:
mov dx,offset str2 ;String initial address
mov ah,09h ;Function 9 output string to standard output device screen
int 21h
jmp int9ret
down:
lea dx,str3 ;the same as above
mov ah,09h
int 21h
jmp int9ret
left:
lea dx,str4 ;the same as above
mov ah,09h
int 21h
jmp int9ret
right:
lea dx,str5 ;the same as above
mov ah,09h
int 21h
jmp int9retquit:
lea dx,str6 ;the same as above
mov ah,09h
int 21h
jmp overint9ret:
pop es
pop dx
pop ax
pop bx
iret ;pop IP,pop CS,popfend start
运行结果如图:
更多推荐
汇编实验
发布评论