汇编实验——外设控制
程序员文章站
2024-03-23 22:32:34
...
汇编程序的简洁写法
编写程序:从键盘上输入一个字符串,以$结束,再将字符串倒序输出(字符串不超过80个字符)。要求用更简洁的写新法完成
输入样例:abcd#1234 efg$
输出样例:gfe 321#dcba
提示解法:
1、输入输出:字符的输入输出可以用DOS中断的01H功能,也可以用BIOS中断;
2、数据结构:可以用栈结构实现倒序,也可以专门设数据区,接受输入、变换并输出。
编写的程序如下:
.8086
.MODEL small
.data
str db 13, 10, 100H dup('$')
.stack 100H
.code
start:
mov ax, @data
mov ds, ax
lea bx, str
add bx, 2
mov cx, 0
input:
mov ah, 01h ;DOS INT21H 01H 输入字符并回显
int 21h
cmp al, '$'
je savestr
push ax ;栈只对字类型操作
inc cx
jmp input
savestr:
pop ax
mov [bx], al
inc bx
loop savestr
lea dx, str ;DOS INT21H 09H 输出字符串
mov ah, 09H ;DS:DX首地址 以'$'结束
int 21h
stop:
mov ax, 4c00h
int 21h
end start
射击游戏框架
编写程序一个“射击游戏”(有些太弱了哈),用上、下、左、右键控制跳上、跳下、装子弹、射击的动作,按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 running...',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
jne cdown
lea dx, up
jmp show
cdown:
cmp al,50h
jne cright
lea dx, down
jmp show
cright:
cmp al,4Dh
jne cleft
lea dx, right
jmp show
cleft:
cmp al,4Bh
jne cesc
lea dx, left
show:
mov ah,9
int 21h
jmp int9ret
cesc:
cmp al, 01h
jne int9ret
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