在Assembly中围绕屏幕移动方块

时间:2014-04-19 16:00:05

标签: assembly

我被要求编写一个汇编代码在屏幕上绘制一个矩形(屏幕是25行和80列),然后从用户读取一个字符,然后在屏幕上移动方块,如果字符是&# 39; R'如果角色是'

,则结束该计划

这是我使用emu8086的代码:

code segment
start:

lea ax, data
mov ds, ax
mov es, ax
; setting video mode
mov ah, 0
mov al, 03h
int 10h

mov al, 0             
mov ah, 6   

mov bh, 0h ;changing the color to black
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h     

mov dh, 8
mov dl, 20
mov bh, 0ffh ;changing the color to white
int 10h     

choose:
mov ah, 1
int 21h
cmp al, 'e'
je finish
cmp al, 'r'
je right ;unnecessary.


right:
mov al, 0             
mov ah, 6   

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h  

mov bh, 0ffh
mov ch, 0
mov cl, 20
mov dh, 8
mov dl, 40
int 10h

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h

mov bh, 0ffh
mov ch, 0
mov cl, 40
mov dh, 8
mov dl, 60
int 10h    

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h

mov bh, 0ffh
mov ch, 0
mov cl, 60
mov dh, 8
mov dl, 79
int 10h     

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h 

mov bh, 0ffh
mov ch, 8
mov cl, 60
mov dh, 16
mov dl, 79
int 10h   

mov bh, 0h
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h   

mov bh, 0ffh
mov ch, 16
mov cl, 60
mov dh, 24
mov dl, 79
int 10h

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h 

mov bh, 0ffh
mov ch, 16
mov cl, 40
mov dh, 24
mov dl, 60
int 10h

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h

mov bh, 0ffh
mov ch, 16
mov cl, 20
mov dh, 24
mov dl, 40
int 10h

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h 

mov bh, 0ffh
mov ch, 16
mov cl, 0
mov dh, 24
mov dl, 20
int 10h  

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h

mov bh, 0ffh
mov ch, 8
mov cl, 0
mov dh, 16
mov dl, 20
int 10h 

mov bh, 0h 
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79  
int 10h

mov bh, 0ffh
mov ch, 0
mov cl, 0
mov dh, 8
mov dl, 20
int 10h

jmp choose


finish:
mov ax, 4c00h
int 21h  

ends

end start

此代码有效,但我认为可以缩短。任何想法如何做到这一点?

2 个答案:

答案 0 :(得分:1)

你知道这个:

mov ch, 0
mov cl, 0

也可以表示为:

mov cx,0

更快,你也可以这样做:

xor cx, cx

答案 1 :(得分:0)

为减少重复的块,请使用表格并将所需的值放入其中。然后从表中获取值并将指针增加到下一部分值。比较指针是否到达表的末尾,否则跳回,以便从表中获取下一个值。 例如:

Table DW 0,0,2479h, 0FF00h,0820h,0 ; each of this entry contains a 16 bit value
Tab_End = $                        ; the offset address of this location

lea si, Table
mov ax, 0600h
AGAIN:
mov bx, [si]
mov cx, [si+2]
mov dx, [si+4]
add si, 6
; push si
int 10h
; pop si
cmp si, Tab_End
jnz AGAIN ; jump if the zeroflag is not set (and the compared values are not equal)

提示:我认为这个软件中断不会破坏我们在SI中的指针,但是否则我们必须先保存我们的寄存器,然后再将其恢复。