如何从ASM的堆栈中的C代码访问双指针 [英] How do I access double pointer from C code in ASM's stack

查看:75
本文介绍了如何从ASM的堆栈中的C代码访问双指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个main.c函数,该函数具有以下语句:

I have a main.c function which has the following statement:

int  initarr(int**arr, int n, int (*initfunc)(int));

然后我像这样调用c中的函数:

Then I call the function in c like that:

success = initarr(&arr, n, getNum); 

问题是,在汇编代码的MODEL SMALL中,上面函数中的第一个参数将以WORDDWORD的形式出现在内存中吗?

The question is, in assembly code, in MODEL SMALL, the first argument in the function above will be in the memory as WORD or DWORD?

换句话说,当我编写代码时,它将起作用:

In other words, when I write the code, will this work:

PUSH BP
MOV BP,SP

PUSH DI
MOV DI, WORD PTR[BP+4] ; DI will hold &arr.

现在DI将保留arr的地址.

And now DI will hold arr's address.

如果为true,那么如何才能访问arr [0]的值?

PUSH BP
MOV BP,SP

PUSH DI
PUSH BX
MOV DI, WORD PTR[BP+4]
MOV BX, WORD PTR[DI]

BX会保存阵列的地址吗?我的意思是第一个单元格的地址?

Will BX hold the address of the array? I mean, the address of the first cell?

如果是,我现在如何访问arr [0]?

If so, how can I access arr[0] now?

也许MOV DX, WORD PTR[BX]吗?

推荐答案

小模型中,地址的大小为2个字节.双指针仍然是地址!

In model small, addresses are sized 2 bytes. A double pointer is still an address!

为了访问arr [0],您需要编写以下内容:

in order to access arr[0], you would write the following:

MOV DI, [BP+4]  ; no need casting, DI register is sized 2 bytes.
MOV SI, [DI]    ; get what DI is pointing to, and put it to SI. 
                ; you could say: DI=arr[][], SI = arr[]
MOV AX, [SI]    ; now AX = arr[0]

如果我们要访问arr [i],我们将执行以下操作:

if we would like to access arr[i], we would do the following:

MOV DI, [BP+4]  
MOV SI, [DI]     
MOV AX, i
SHL AX, 1   ; because arr[] contains int numbers which are sized 2 bytes.
ADD SI, AX
MOV AX, [SI]    ; now AX = arr[i].  SI = &arr[i].

这篇关于如何从ASM的堆栈中的C代码访问双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆