贺利坚老师汇编实验八

任务1 - 射击游戏框架

仿照第15周课堂项目三(见教学平台中第15周课堂项目参考),编写程序一个“射击游戏”(有些太弱了哈),用上、下、左、右键控制跳上、跳下、装子弹、射击的动作,按ESC键退出游戏。

运行结果如图:

assume cs:code
stack segment
     db 256 dup (0)
stack ends

data segment
  dw 0,0
  home  db 'Game is runing...',10,13,'$'
  jup   db 'Jump up...',10,13,'$'
  jdw   db 'Jump down...',10,13,'$'
  gbt   db 'Get bullet...',10,13,'$'
  sht   db 'Shoot...',10,13,'$'
  bbe   db 'Byebye...',10,13,'$'
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

running:
      lea dx,home
      mov ah,9
      int 21h
      call delay
      jmp running

delay:
     push dx
     push ax
     mov dx,10h
     mov ax,0
s:   sub ax,1
     sbb dx,0
     cmp ax,0
     jne s
     cmp dx,0
     jne s
     pop ax
     pop dx
     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]

begin: 
      mov bl, al ;保存al
      cmp al,48h ;48h是上键的扫描码
      je up
      cmp al,50h ;50h是下键的扫描码
      je down
      cmp al,4bh ;4bh是左键的扫描码
      je left
      cmp al,4dh ;4d是右键的扫描码
      je right
      cmp al,01h ;01h是esc键的扫描码
      je exit
      jmp int9ret

up:   
      lea dx,jup
      jmp display
down: 
      lea dx,jdw
      jmp display
left: 
      lea dx,gbt
      jmp display
right:
      lea dx,sht
      jmp display

display:
        mov ah,9
	int 21h
	jmp int9ret

 exit: 
      lea dx,bbe
      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

任务2-汇编程序的简洁写法

编写程序:从键盘上输入一个字符串,以$结束,再将字符串倒序输出(字符串不超过80个字符)。

输入样例:abcd#1234 efg$

输出样例:gfe 321#dcba

算法要求:逐个输入字符(21H中断的01H功能)并压栈,遇$后,将字符出栈按倒序写入数据区,输出字符串(21H中断的09H功能)

编写的程序如下:

.8086
.model small
.data
    str db 81 dup('$') 
  ;Make sure you encounter $ stop when reading characters.
.stack 256
.code
.startup
    mov cx,0
    lea bx,str
input:
    mov ah,1
    int 21h
    cmp al,'$'
    je s
    inc cx ;Record the length of characters entered.
    push ax
    jmp input
s:  
    pop ax
    mov [bx],al
    inc bx ;The stack is saved to the data block.
    loop s

    mov ah,02h
    mov dl,0ah
    int 21h

    lea dx,str
    mov ah,9h
    int 21h ;Output string (in reverse order).

.exit 0
end

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页