assembly cannot print on bottom right

I have an assembly program which has a character that moves around the screen either using arrow keys or WASD keys. The character also depends on the key pressed. It is pretty much like a snake game but it does not grow and it does not chase anything.

The problem is when the characted is supposed to be printed on (79,24), it is printed in (78,24) instead. What seems to be wrong with my code?

here's my code

.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

Upvotes: 1

Views: 210

Answers (1)

rkhb
rkhb

Reputation: 14409

This is the problem:

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

When MS-DOS prints at the last position in the last line, it performs automatically a line feed. Use the Video-BIOS instead:

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

Upvotes: 1

Related Questions