在程序集中向左或向右移动字符

3 x86 assembly masm irvine32

我正在组装一个简单的游戏,目前我只是想根据键盘上的数字 1 还是 2 向左或向右移动播放器图标(在这种情况下它是一个字符 X)。目前我只有两个 if 然后通过减少或增加 X 坐标值来向左或向右移动我的 X。我正在使用 Irvine 32 库来获取像 Gotoxy 这样的 proc 来获取坐标值。到目前为止,我的程序将 x 显示在正确的起始位置,但是由于某种原因,当我点击 1 或 2 时,x 会向上移动一个空格而不是向左或向右移动。我在我拥有的书中无法弄清楚,我在互联网上找到的东西不在我的装配知识范围内。就目前而言,我只想向左或向右移动我的字符。稍后我将制作适当的 procs 等,但现在这是一个非常奇怪的问题。任何帮助,将不胜感激。下面是我的汇编代码:

        TITLE Matrix Rain

; Description:Dodge falling numbers to reach the final level
; Authors: John , Killian
; Revision date:2/11/2015

INCLUDE Irvine32.inc
.data
player byte 'X'; Player Character
direction byte ?; Direction the player moves
startY byte 24; Starting Y position
startX byte 39; Starting X position

breakLoop byte 9 ; base case to break

right byte 2; moves player right
left byte 1; moves player left
.code

main PROC
    ;Player Starting Point
    mov dh , startY; column 24
    mov dl,startX ; row 39
    call Gotoxy; places cursor in the middle of the bottom part of the console window
    mov al, player; Copies player character to the AL register to be printed
    call WriteChar; Prints player to screen console
    call Crlf
    ;Player Starting point
    XOR al,al; Zeroes al out
    call ReadKey;Reads keyboard 
    mov eax, 3000
    call delay

    _if1: cmp al , left
         je THEN1
         jmp ENDIF1
         THEN1:

            inc startX
            mov dl, startX
            call Gotoxy
            call Clrscr

            mov al, player
            call WriteChar
            XOR al,al
    ENDIF1:

    _if2: cmp al , right
         je THEN2
         jmp ENDIF2
         THEN2:

            dec startX
            mov dl,startX
            call Gotoxy
            call Clrscr

            mov al, player
            call WriteChar
            XOR al,al
    ENDIF2:
        exit
        main ENDP

        END main
Run Code Online (Sandbox Code Playgroud)

rkh*_*khb 5

您的代码存在一些问题:

1) 数字不是字符

right byte 2; moves player right
left byte 1; moves player left
Run Code Online (Sandbox Code Playgroud)

rightleft现在持有的纯数字1和2。然而,ReadKey在返回ALASCII码按下的按键。您可以查看键代码的参考12或者您可以编写:

right byte '2'; moves player right
left byte '1'; moves player left
Run Code Online (Sandbox Code Playgroud)

2) 寄存器不适合永久保存值

尽管您初始化DH它会在您到达call Gotoxy. mov dh, startY在调用之前插入一个Gotoxy

inc startX
mov dl, startX
mov dh, startY
call Gotoxy
Run Code Online (Sandbox Code Playgroud)

下一期:

call ReadKey                ; Reads keyboard
mov eax, 3000
call delay
Run Code Online (Sandbox Code Playgroud)

ReadKey返回 中按下的键的 ASCII 码AL,但mov eax, 3000会破坏这个结果(AL是 的一部分EAX)。RTFM并将块更改为:

LookForKey:
mov  eax,50          ; sleep, to allow OS to time slice
call Delay           ; (otherwise, some key presses are lost)
call ReadKey         ; look for keyboard input
jz   LookForKey      ; no key pressed yet
Run Code Online (Sandbox Code Playgroud)

3)Clrscr改变当前光标位置

调用会Clrscr破坏 的效果Gotoxy。在 之后Gotoxy删除对该函数的调用,最好在开发阶段删除所有调用。


我建议立即实施循环:

cmp al, '3'
jne LookForKey

exit
Run Code Online (Sandbox Code Playgroud)

这将在按下“3”时退出程序,否则重复循环。