我需要在 C 程序中调用汇编过程。在我的 C 程序中,我有一个数组的地址,在我的汇编过程中,我需要获取数组第二个索引的值。如果我将数组本身作为参数,那么对我来说会更容易。你能告诉我如何获取数组第二个元素的内容吗?
在我的 C 程序中,我调用此函数:
getIndex(&array[0]);
如果参数不是地址,我在汇编过程中的解决方案是这样的:
PUSH BP
MOV BP,SP
push SI
MOV SI,[BP+4]
ADD CX,SI
ADD SI,2
MOV AX,SI ; AX got the value of the second index of the array
我应该如何解决我的问题?感谢您的帮助。
最佳答案
基本上,您还需要一次内存地址取消引用(括号),但您的代码还存在一些其他问题。
查看您的代码,我假设数组中元素的大小为 2 个字节,您正在使用 cdecl调用约定,并有一个 16 位处理器。问题如下:
- 如果将 SI 压入堆栈,也应该将其弹出
- 您将 SI 的值添加到 CX,但从未在 CX 中使用该值
- 该过程不会恢复调用者的 BP 和 SP,也不会返回到调用该过程的位置
以下是已修复问题的代码:
push bp
mov bp,sp
push si
mov si,[bp+4] ; si = address of the first element in the array
mov ax,[si+2] ; ax = value of the second element in the array
pop si ; revert si
mov sp,bp ; revert the caller's stack pointer
pop bp ; revert the caller's base pointer
ret ; jump to the instruction after the call
关于assembly - 当我有第一个索引的地址时,如何获取 x86 程序集中数组元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16002763/