【问题标题】:Assembly not jumping properly组件没有正确跳跃
【发布时间】:2026-02-07 21:05:01
【问题描述】:

我无法理解为什么我的 cmp 语句无法正常工作。

当我运行它时,我首先输入 0,然后它转到 storeValue。我在第二个值中输入 0,它会像预期的那样进入 searchArray。

我的 cmp 和 jump 语句上有断点,并且在 AL 上有一个监视,所以我不明白为什么它应该在此时提示输入搜索值时存储第一个 0。

感谢收看。

.DATA
prompt1     BYTE    "Enter a value to put in array or 0 to search array.", 0
prompt2     BYTE    "Enter a value to search array for.",0                         
intArray    DWORD   ?
numElem     DWORD   0
SearchVal   DWORD   ?

resultNope  BYTE    "Not in array.",0

.CODE
_MainProc PROC
            lea     ebx, intArray           ;get the address of array.
start:      input   prompt1, intArray, 50   ;read in integer
            atod    intArray                ;convert to int

            mov     al, [ebx]               ;move int to register
            cmp     al, 0                   ;if integer is positive - store it!
            jg      storeValue              ;JUMP!

            cmp     al, 0                   ;if 0 - time to search array!
            je      searchArray             ;JUMP!

storeValue: add     numElem, 1              ;Adds 1 to num of elements in array.
            mov     [ebx], al               ;moves number into array.
            add     ebx, 1                  ;increment to next array address.
            jmp     start                   ;get next number for array. JUMP!

searchArray:input   prompt2, searchVal, 50  ;What are we searching array for?
            atod    searchVal               ;convert to int
            lea     ebx, intArray           ;get address of array.
            mov     ecx, 1                  ;set loop counter to 1.

【问题讨论】:

    标签: arrays loops assembly


    【解决方案1】:

    您忘记展示inputatod 的工作原理。看着我的水晶球,我猜input 需要一个缓冲区来将用户输入存储为文本,而参数50 大概就是它的大小。请注意,您没有这样的缓冲区,甚至没有 50 字节的空间。我还认为,由于atod 显然只需要 1 个参数,即要转换的文本缓冲区,因此它可能会返回eax 中的值。你的storeValue 是从al 写的,这也强化了这一点,否则这将毫无意义。

    长话短说:

    1. 为输入的文本分配适当大小的缓冲区
    2. 将此数组传递给atod
    3. 在致电atod 后不要破坏al

    (也适用于搜索部分。)

    【讨论】:

    • 对不起,我还在学习 80x86 asm,我认为 atod 和 input 是本书解释它的标准语法。仔细研究它,我发现它们是宏。不过谢谢!我想通了,在我将 atod 从 EAX 调用到 [EBX] 之后,我愚蠢地忘记了移动值,这解决了我的问题。谢谢!