WriteDec 在循环存储到数组中的 5 次调用后停止工作?

WriteDec stops working after 5 calls, in a loop storing to an array?

对于一个 class 项目,我们被要求创建一个斐波那契数列程序。我正在使用 Visual Studio 2017 来 运行 我的程序(32 位)。当我 运行 以下程序时一切顺利,直到我尝试打印以控制台序列中的第 6 个数字 5,(0,1,1,2,3,5)。超过第 5 个数字的任何内容都不会输出到控制台。我正在使用 Irvine32 库中的 WriteDec 程序来完成 write decimals 到控制台。

我被迷惑了,因为 eax 当我在调试器模式下单步执行时,寄存器保存了正确的值,但 WriteDec 不会打印出来。在计算序列的第 8 项时,我用来存储斐波那契数列的地址具有这些值: 0x00406000 00 00 00 00 01 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 05 00 00 00 08 00 00 00 0d 00 00 00

输出如下: 0 1 123

  ;Fibionacci Sequence

; This program outputs a fibonacci sequence and sum

Include Irvine32.inc

.data
array DWORD 0,1

.code

main proc
    mov ecx, 6 ; would be the 8th term in the sequence
    mov esi, 4
    mov eax, 0
    call WriteDec
    call crlf
    mov eax, 1
    call WriteDec
    call crlf
    L1: 
        mov edx, array[esi]
        mov edi, array[esi-4]
        add edx,edi
        mov array[esi+4],edx
        mov eax, edx
        call WriteDec ; Writes an unsigned 32-bit decimal number to standard output in decimal format with no leading zeros.
        add esi,4
    loop L1
    invoke ExitProcess,0
main endp
end main

当您写入超过为 array DWORD 0,1 保留的两个双字的末尾时,您可能会覆盖 WriteDec 使用的一些数据。

how could i test if writedec overwrites data?

你搞反了。 您的代码 正在您的数组之外写入。 链接器可能将它放在 WriteDec 需要读取的某些数据之前 ,例如也许是控制台的文件句柄或其他东西。因此,在您踩到 WriteDec 的常量之后,以后对它的调用将停止打印任何内容。

您的调试器输出确认 WriteDec 没有覆盖您存储的斐波那契数列值,因此我们可以得出结论 WriteDec 不是 写入 到那个记忆,只是阅读它。

而且它不是一个指针,否则当用小整数覆盖它时它会崩溃。


试试

array DWORD 0,1
  DWORD 10 DUP(?)       ;  or DUP(0)  because that's what you'll really get

为前 2 个值创建一个带有显式初始值设定项的更大数组,然后在同一部分中添加更多 space。