【发布时间】:2014-09-29 00:30:45
【问题描述】:
我有一个汇编程序,它有一个使用箭头键或 WASD 键在屏幕上移动的字符。字符还取决于按下的键。它很像蛇游戏,但它不会成长,也不会追逐任何东西。
问题是当字符应该打印在 (79,24) 上时,它却打印在 (78,24) 上。我的代码似乎有什么问题?
这是我的代码
.model small
.data
currentRow db 12
currentCol db 39
snake db 62
.stack 100h
.code
main proc
mov ax, @data
mov ds, ax
;main
;sets the screen size
mov al, 03h
mov ah, 00h
int 10h
;hides the cursor
mov cx, 3200h
mov ah, 01h
int 10h
snakeLoop:
;clear screen
mov ax,0600h
mov bh, 07h
xor cx,cx
mov dx,184fh
int 10h
;sets the cursor
mov dh, currentRow
mov dl, currentCol
xor bh, bh
mov ah, 02h
int 10h
;prints the character
mov dl,snake
mov ah, 02h
int 21h
;sets the cursor back to previous after printing the snake's head
mov dh, currentRow
mov dl, currentCol
xor bh, bh
mov ah, 02h
int 10h
;gets the key pressed
mov ah,01h
int 21h
cmp al,77 ;if arrow right
je right
cmp al,75 ;if arrow left
je left
cmp al,72 ;if arrow up
je up
cmp al,80 ;if arrow down
je down
cmp al, 100 ;if 'd'
je right
cmp al, 97 ;if 'a'
je left
cmp al,119 ;if 'w'
je up
cmp al,115 ;if 's'
je down
cmp al, 27 ;if escape -> exits the program
je doNothing
jmp snakeLoop ;does nothing when other keys are pressed
left:
mov snake,60 ;sets the snake's head direction '<'
cmp currentCol,0 ;checks if the snake's head is on the leftmost
je loop1
jne noLoop1
loop1:;moves it to the rightmost
mov currentCol,79
jmp snakeLoop
noloop1:
dec currentCol
jmp snakeLoop
right:
mov snake, 62 ;sets the snake's head direction '>'
cmp currentCol,79 ;checks if the snake's head is on the rightmost
je loop2
jne noLoop2
loop2:;moves it to the leftmost
mov currentCol,0
jmp snakeLoop
noloop2: ;normal navigation through screen
inc currentCol
jmp snakeLoop
up:
mov snake, 94 ;sets the snake's head direction '^'
cmp currentRow,0 ;checks if the snake's head is on the topmost
je loop3
jne noLoop3
loop3:;moves it to the bottommost
mov currentRow,24
jmp snakeLoop
noLoop3: ;normal navigation through screen
dec currentRow
jmp snakeLoop
down:
mov snake, 118 ;sets the snake's head direction 'v'
cmp currentRow,24 ;checks if the snake's head is on the bottomost
je loop4
jne noLoop4
loop4: ;moves it to the topmost
mov currentRow,0
jmp snakeLoop
noLoop4: ;normal navigation through screen
inc currentRow
jmp snakeLoop
doNothing:
;end loop
;end main
mov ax, 4c00h
int 21h
main endp
end main
【问题讨论】:
-
将 cmets 放入汇编源代码是不是可选的。让其他人愿意阅读或调试您的代码不仅很重要,它还可以帮助您发现愚蠢的逻辑错误。就像在左/右反转边界检查一样。
-
我编辑了发布的代码。我很抱歉没有放 cmets。我还修复了边界检查。右下角打印的问题依然存在。