【发布时间】:2013-06-29 02:05:14
【问题描述】:
我最近让数组在 masm32 中工作,但我遇到了一个非常令人困惑的问题。我有一个过程 (AddValue),它接受一个参数并将该参数添加到名为 bfmem 的数组中的一个元素中。影响哪个元素由名为index 的变量确定。然而,index 似乎正在改变它的价值,而我并不期望它。
如果index 大于0,则程序正常运行。但是,如果index 等于0,则其值将更改为传递给过程的任何值。这让我非常困惑,尤其是当index 设置为零时才会发生这种情况。我不太了解 MASM,如果这是一个非常简单的问题,请原谅我。
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
.data
bfmem dd 0 dup(30000)
index dd 0
_output db 10 dup(0)
.code
AddValue proc val ; adds val to bfmem[index]
invoke dwtoa, index, addr _output
invoke StdOut, addr _output ;prints '0' (as expected)
mov ecx, index ;mov index to register for arithmatic
mov eax, val ;mov val to register for arithmatic
add [ecx * 4 + bfmem], eax ;multiply index by 4 for dword
;then add to array to get the element's
;memory address, and add value
invoke dwtoa, index, addr _output
invoke StdOut, addr _output ;prints '72' (not as expected)
ret
AddValue endp
main:
mov index, 0
invoke AddValue, 72
invoke StdIn, addr _output, 1
invoke ExitProcess, 0
end main
我唯一能想到的是汇编器正在做某种算术优化(注意到 ecx 为零并以某种方式简化了[ecx * 4 + bfmem] 表达式,从而改变了输出)。如果是这样,我该如何解决这个问题?
任何帮助将不胜感激。
【问题讨论】:
-
push [bfmem] - add [ecx * 4 + bfmem], eax - pop [bfmem] 会保护这个值,如果这就是问题所在
-
不要推送/弹出。你只是在掩盖问题。
标签: arrays variables assembly masm masm32