如何将整数写入通过 INVOKE 传递的数组?
how to write integer to array passed with INVOKE?
我无法解决这个涉及将整数写入数组的简单问题。正如您在下面的代码中看到的,我将地址传递给过程并尝试写入数组。我尝试以各种不同的方式访问数组,但在过程中访问地址位置似乎与在 MAIN 中访问地址位置的方式不同。我无法想象为什么 freqTable
在函数 get_freq
中无法访问。请赐教。
.data
target BYTE "aaaa",0
freqTable DWORD 256 DUP(0)
.code
get_freq PROC,table PTR DWORD
mov table[0],'b' <--- writing 'b' to wrong address
ret
get_freq endp
main PROC
INVOKE get_freq,ADDR freqTable
exit
main ENDP
问题在于,在表达式 mov table[0],'b'
中,您引用了 table
的地址,而不是作为参数传递的 table
=ADDR freqTable
的值。因此,您将写入包含 LOCAL 变量 table
的堆栈,而不是写入 table 的值指向 (=freqTable
).
的 DATA 段
因此,要实现此功能,您必须使用 table
的值作为地址:
get_freq PROC table: PTR DWORD
mov eax, table <--- get value of table=ADDR freqTable to EAX
mov DWORD PTR [eax], 'b' <--- writing 'b' to address with value EAX
ret
get_freq endp
如果要给地址加索引,可以使用
mov DWORD PTR [eax+ecx], 'b'
例如, 以 ECX
作为变址寄存器。对于您的情况 ECX
应该是 0
.
it appears accessing address locations in a procedure doesn't work the same as inside MAIN.
是的。使用直接地址(全局变量)和作为参数传递的地址(如在 PROC 中)是有区别的。上面的例子试图说明这一点。
全局变量始终可以通过使用 OFFSET
指令来访问,例如
mov eax, offset freqTable
如果您添加另一个间接方式,例如使用传递给过程的参数,则必须像
一样考虑到这一点
push offset freqTable <!-- store address on stack
call get_freq <!-- call procedure with variable on stack
...
mov eax, variableName <!-- get address from stack
mov DWORD PTR [eax], 'b' <!-- use it
因此,使用参数的第二种可能性开辟了一种将 运行-time-values 传递给过程的方法,而使用 OFFSET
方式提供的编译时值是不可能的.
我无法解决这个涉及将整数写入数组的简单问题。正如您在下面的代码中看到的,我将地址传递给过程并尝试写入数组。我尝试以各种不同的方式访问数组,但在过程中访问地址位置似乎与在 MAIN 中访问地址位置的方式不同。我无法想象为什么 freqTable
在函数 get_freq
中无法访问。请赐教。
.data
target BYTE "aaaa",0
freqTable DWORD 256 DUP(0)
.code
get_freq PROC,table PTR DWORD
mov table[0],'b' <--- writing 'b' to wrong address
ret
get_freq endp
main PROC
INVOKE get_freq,ADDR freqTable
exit
main ENDP
问题在于,在表达式 mov table[0],'b'
中,您引用了 table
的地址,而不是作为参数传递的 table
=ADDR freqTable
的值。因此,您将写入包含 LOCAL 变量 table
的堆栈,而不是写入 table 的值指向 (=freqTable
).
因此,要实现此功能,您必须使用 table
的值作为地址:
get_freq PROC table: PTR DWORD
mov eax, table <--- get value of table=ADDR freqTable to EAX
mov DWORD PTR [eax], 'b' <--- writing 'b' to address with value EAX
ret
get_freq endp
如果要给地址加索引,可以使用
mov DWORD PTR [eax+ecx], 'b'
例如, 以 ECX
作为变址寄存器。对于您的情况 ECX
应该是 0
.
it appears accessing address locations in a procedure doesn't work the same as inside MAIN.
是的。使用直接地址(全局变量)和作为参数传递的地址(如在 PROC 中)是有区别的。上面的例子试图说明这一点。
全局变量始终可以通过使用 OFFSET
指令来访问,例如
mov eax, offset freqTable
如果您添加另一个间接方式,例如使用传递给过程的参数,则必须像
一样考虑到这一点push offset freqTable <!-- store address on stack
call get_freq <!-- call procedure with variable on stack
...
mov eax, variableName <!-- get address from stack
mov DWORD PTR [eax], 'b' <!-- use it
因此,使用参数的第二种可能性开辟了一种将 运行-time-values 传递给过程的方法,而使用 OFFSET
方式提供的编译时值是不可能的.