装配不能在右下方打​​印

时间:2014-09-28 12:43:31

标签: assembly printing dos

我有一个汇编程序,它有一个使用箭头键或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

1 个答案:

答案 0 :(得分:0)

这是问题所在:

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

当MS-DOS在最后一行的最后一个位置打印时,它会自动执行换行。请改用Video-BIOS:

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