为什么这个数组声明会侵入下一个数组声明?

发布于 2024-08-28 22:55:01 字数 1175 浏览 9 评论 0原文

我正在使用 Microsoft Visual C++ Express Edition 使用 MASM 学习 IA-32 的汇编,并且出现了这个困难。当我这样做时:

INCLUDE Irvine32.inc

QUANT = 47

.data

    fibonacciVetor DWORD 1, 1, (QUANT - 2) DUP(0)
    fileName BYTE "vetor.txt", 0
    fileHandler DWORD 0

.code
main PROC

    mov esi, 0
    mov ecx, QUANT

L1: mov eax, fibonacciVetor[esi * TYPE fibonacciVetor]
    add eax, fibonacciVetor[esi * TYPE fibonacciVetor + 4]
    mov fibonacciVetor[esi * TYPE fibonacciVetor + 8], eax
    inc esi
    loop L1

    mov edx, OFFSET fileName
    call CreateOutputFile
    mov fileHandler, eax
    mov edx, OFFSET fibonacciVetor
    mov ecx, QUANT * TYPE fibonacciVetor
    call WriteToFile
    mov eax, fileHandler
    call CloseFile

    exit

main ENDP
END main

该程序无法正确运行,因为 fileName 字符串在进程中间被删除。 Irvine32.inc 库可以在 Kip Irvine 的网站上找到。我使用它是因为我的教授使用的教科书是“基于英特尔的计算机的汇编语言”,第五版,作者:Kip Irvine。当我为此更改变量声明时:

    fileName BYTE "vetor.txt", 0
    fibonacciVetor DWORD 1, 1, (QUANT - 2) DUP(0)
    fileHandler DWORD 0

程序运行正确。

为什么简单地改变声明的顺序会影响程序的运行方式或不会影响程序的运行方式,因为 fileName 变量应该在 fibonacciVetor 结束后立即分配,并且当我写入数组时不应该受到影响?

非常感谢。

I am learning Assembly for IA-32 with MASM, using Microsoft Visual C++ Express Edition, and this difficulty came up. When I do this:

INCLUDE Irvine32.inc

QUANT = 47

.data

    fibonacciVetor DWORD 1, 1, (QUANT - 2) DUP(0)
    fileName BYTE "vetor.txt", 0
    fileHandler DWORD 0

.code
main PROC

    mov esi, 0
    mov ecx, QUANT

L1: mov eax, fibonacciVetor[esi * TYPE fibonacciVetor]
    add eax, fibonacciVetor[esi * TYPE fibonacciVetor + 4]
    mov fibonacciVetor[esi * TYPE fibonacciVetor + 8], eax
    inc esi
    loop L1

    mov edx, OFFSET fileName
    call CreateOutputFile
    mov fileHandler, eax
    mov edx, OFFSET fibonacciVetor
    mov ecx, QUANT * TYPE fibonacciVetor
    call WriteToFile
    mov eax, fileHandler
    call CloseFile

    exit

main ENDP
END main

This program does not run correctly, because the fileName string is erased in the middle of the process. The Irvine32.inc library can be found in Kip Irvine's website. I'm using it because the textbook my professor is using is "Assembly Language for Intel-based Computers", 5th Edition by Kip Irvine. When I change the variables declaration for this:

    fileName BYTE "vetor.txt", 0
    fibonacciVetor DWORD 1, 1, (QUANT - 2) DUP(0)
    fileHandler DWORD 0

The program runs correctly.

Why is it that simply changing the order of the declaration has influence in how the program runs or does not, since the fileName variable should be allocated right after the end of the fibonacciVetor and should not be affected when I write to the array?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-09-04 22:55:01

...因为 fileName 变量应该是
结束后立即分配
fibonacciVetor 且不应该
当我写入数组时受到影响

嗯,“不应该”!=“是”。使用调试器逐步检查它以查看范围错误在哪里。

...since the fileName variable should be
allocated right after the end of the
fibonacciVetor and should not be
affected when I write to the array

Well, "should not" != "is". Step through it with a debugger to see where your range error is.

π浅易 2024-09-04 22:55:01

我在这里怀疑这一部分:

L1: mov eax, fibonacciVetor[esi * TYPE fibonacciVetor]
    add eax, fibonacciVetor[esi * TYPE fibonacciVetor + 4]
    mov fibonacciVetor[esi * TYPE fibonacciVetor + 8], eax

我认为最后一行可能会破坏你的文件名。

遵循 Per Larsen 的建议,使用调试器逐步执行或添加一些打印语句。希望这能让您知道从哪里开始寻找。

I would suspect this section here:

L1: mov eax, fibonacciVetor[esi * TYPE fibonacciVetor]
    add eax, fibonacciVetor[esi * TYPE fibonacciVetor + 4]
    mov fibonacciVetor[esi * TYPE fibonacciVetor + 8], eax

I think that last line might be what clobbers your fileName.

Follow Per Larsen's advice and either step through it with a debugger or add some print statements. Hopefully this gives you an idea of where to start looking.

冬天旳寂寞 2024-09-04 22:55:01

只是一个猜测,但我想说定义中的 (Quant - 2) 项是问题所在。如果您要使用零相对数组,我可以看到说 (Quant - 1),但使用 -2 时,您将失去放置最后一个元素的空间。

只要去掉-2就可以了。内存既便宜又充足。毕竟,您不是在对 KIM-1 进行编程。

Just a guess, but I would say that the (Quant - 2) term in the definition is the problem. I could see saying (Quant - 1) if you are going to use a zero relative array, but with -2, you are losing the space to put your last element.

Just get rid of the -2. Memory is cheap and plentiful. You aren't programming a KIM-1, after all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文