1

我正在制作这个小程序,我必须在屏幕上移动一个星号。问题是每次碰到边界时,我都会得到一个额外的星号。关于如何摆脱那个额外的星号有什么帮助吗?这是一个非常简单的程序。

到目前为止,这是我的代码:

.model small
.stack 256

.data
row  db 10                       ;Save the initial cursor coordinates in
col  db 40                       ;col and row.
msg1 db "*",'$'                  ;Asterisk to be used by the program.
msg2 db " ",'$'

mSetCursor macro                 ;Macro for placing the cursor in the screen.
;pre-conditions
;
;    None
;
;post-conditions
;
;    Asterisks will be printed on the screen,
;    depending on the location of the cursor.

push ax                       
push bx
push dx

mov bh,0
mov ah,2
mov dh,row
mov dl,col
int 10h

pop dx
pop bx
pop ax
endm

.code
main proc
mov ax,@data
mov ds,ax

mov ax,03                     ;Clean the screen.
int 10h

start:
mSetCursor                    ;Call the macro in charge of placing
                              ;the asterisk on the screen.
mov ah,10h
int 16h

cmp ah,2dh                    ;Using the scan code of the letter X, we end
je exit                       ;the program once the "X" key is pressed.

cmp ah,0bh                ;Using the scan code of the number 0, we clean
je clear                      ;the screen once the "0" key is pressed.

cmp ah,4bh                    ;Using the scan code of the left arrow,
je left                       ;we use the left arrow key to move the asterisk left.

cmp ah,48h                    ;Using the scan code of the up arrow,
je up                         ;we use the up arrow key to move the asterisk up.

cmp ah,4Dh                    ;Using the scan code of the right arrow, 
je right                      ;we use the right arrow key to move the asterisk right.

cmp ah,50h                    ;Using the scan code of the down arrow,
je down                       ;we use the down arrow key to move the asterisk down.

mov ah,9                  ;Code in charge of receiving the fifth message
mov dx, offset msg2           ;This code ensures that nothing happens while
int 21h                       ;keys that aren't arrow keys, 0, or X (quit) are pressed.
jmp start

left:                         ;We receive the asterisk, and decrease the value of col
mov ah,9                      ;This starts the leftward movement of the asterisk.
mov dx, offset msg1           ;Finally, we skip to the Start label.
int 21h                        
dec col

cmp col,00                ;Disallow that the asterisk move past the
je right                  ;left side of the screen.

jmp start                      

right:                        ;Receive the asterisk, and increase the value of col.
mov ah,9                      ;This starts the rightward movement of the asterisk.
mov dx, offset msg1           ;Finally, we skip to the Start label.
int 21h
inc col

cmp col,79                    ;Disallow that the asterisk move past the 
je left                   ;right side of the screen.

jmp start

down:                         ;We receive the asterisk and increase the value of row.
mov ah,9                      ;This starts the downward movement of the asterisk.
mov dx, offset msg1           ;Finally, we jump to the Start label.
int 21h
inc row

cmp row,24                ;Disallow that the asterisk move past the
je up                     ;bottom of the screen.

jmp start

up:                           ;We receive the asterisk, and decrease the value of row.
mov ah,9                      ;This starts the upward movement of the asterisk.
mov dx, offset msg1           ;Finally, we jump to the Start label.
int 21h
dec row

cmp row,-1                ;Disallow that the asterisk move past the
je down                   ;top of the screen.

jmp start

clear:
mov ax,03                     ;Clean the screen.
int 10h
jmp start

exit:                         ;Label that lets us end the program.
mov ah,4ch
int 21h

main endp
end main
4

1 回答 1

1

您的代码的问题是当它到达边缘时,它会收到两个中断来放置星号。详细地说,假设光标在右边缘。然后按下右键。现在它将跳转到 label right。它会在当前光标位置放置一个星号。然后递增col并跳转到 label left。在那里它会递减col以防止超出边缘。但是标签 left 还包含一个插入星号的中断,导致两个*.

为了防止这种情况,而不是先递增然后递减只是跳过inc col如果到达边缘。例如,您的right标签被修改为

right:                        ;Receive the asterisk, and increase the value of col.
mov ah,9                      ;This starts the rightward movement of the asterisk.
mov dx, offset msg1           
int 21h

cmp col,78                    ;Disallow that the asterisk move past the 
je start                      ;right side of the screen.

inc col

jmp start                     ;Finally, we skip to the Start label.

您还必须为其他三个这样做。cmp col, 78根据您要停止的位置更改比较值。

在那里,我刚刚调试了您的代码,这很有趣。:)

于 2012-05-09T13:59:44.977 回答