欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

汇编程序:这是一个假游戏

程序员文章站 2024-01-31 14:28:52
...

【任务】
  编写程序,模仿射击游戏中,用上、下、左、右键控制跳上、跳下、装子弹、射击的动作,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:



code ends
end start

【参考解答】

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,256
      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

      ; 游戏运行中
play: lea dx, run
      mov ah,9
      int 21h
      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 dx
      push es
      in al,60h
      pushf
      pushf
      pop bx
      and bh,11111100b
      push bx
      popf
      call dword ptr ds:[0]

  cup:cmp al,48h ; 48h是UP键的扫描码
      jne cdown
      lea dx, up
      jmp show
cdown:cmp al,50h ; 50h是down键的扫描码
      jne cright
      lea dx, down
      jmp show
cright:cmp al,4Dh ; 4Dh是UP键的扫描码
      jne cleft
      lea dx, right
      jmp show
cleft:cmp al,4Bh ; 4Bh是left键的扫描码
      jne cesc
      lea dx, left
 show:mov ah,9
      int 21h
      jmp int9ret

 cesc:cmp al, 01h ;01h是esc键的扫描码
      jne int9ret
      ;处理END,使程序结束,注意在此要恢复中断向量
      lea dx, over
      mov ah,9
      int 21h
      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 21h

int9ret:pop es
      pop dx
      pop bx
      pop ax
      iret

code ends
end start