2014-09-28 26 views
0

我有一個程序集程序,其中有一個可以使用箭頭鍵或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 
+0

將註釋放在彙編源代碼中是* not * optional。讓別人願意閱讀或調試你的代碼不僅很重要,它還可以幫助你找到愚蠢的邏輯錯誤。就像倒轉左/右邊界檢查一樣。 – 2014-09-28 13:03:11

+0

我編輯了張貼的代碼。我很抱歉沒有發表評論。我還修正了邊界檢查。打印在右下角的問題仍然存在。 – 2014-09-28 13:41:13

回答

0

這就是問題所在:

;prints the character 
mov dl,snake 
mov ah, 02h 
int 21h 

當MS-DOS打印在最後一行的最後一個位置,它會自動執行換行。使用視頻BIOS代替:

;prints the character 
mov al, snake 
xor bh, bh 
mov cx, 1 
mov ah, 0Ah 
int 10h