为什么这个数组声明会侵入下一个数组声明?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,“不应该”!=“是”。使用调试器逐步检查它以查看范围错误在哪里。
Well, "should not" != "is". Step through it with a debugger to see where your range error is.
我在这里怀疑这一部分:
我认为最后一行可能会破坏你的文件名。
遵循 Per Larsen 的建议,使用调试器逐步执行或添加一些打印语句。希望这能让您知道从哪里开始寻找。
I would suspect this section here:
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.
只是一个猜测,但我想说定义中的 (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.