如何在屏幕边框停止文字?

时间:2012-05-07 20:00:53

标签: assembly

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

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

.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

1 个答案:

答案 0 :(得分:1)

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

为防止这种情况发生,如果达到边缘,则不是先递增而是递减,只是跳过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

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