简体   繁体   English

将数组作为参数传递给外部ASM(Assembly)x86并对其进行访问

[英]Passing array as a parameter to an external ASM (Assembly) x86 and accessing it

As there isn't too much information I could find on mixing C++ and Assembly, I wanted some clarification on some issues I am having with assembly. 由于在混合C ++和汇编程序方面找不到太多信息,因此我想对汇编程序遇到的一些问题进行澄清。

I am working on a project where we have to pass an array to a separate .asm document in order to reverse the string. 我正在做一个项目,在该项目中,我们必须将数组传递给单独的.asm文档,以便反转字符串。 Please see the code of the .cpp file I started with (not including header below): 请查看我开始使用的.cpp文件的代码(不包括下面的标题):

extern"C" int swap(char*, int);

int main()
{
    const int SIZE = 20;
    char str1[20] = { '\0' };

    cout << "Please enter a string: ";
    cin >> str1;

    swap(str1,SIZE);

    return 0;
}

From here, I would start the following in my .asm document (not including the header in the code below): 从这里开始,我将在.asm文档中开始以下操作(不包括下面代码中的标题):

_swap PROC
    push ebp
    mov ebp,esp ;stack pointer to ebp

    mov ebx,[ebp+8] ; address of first array element
    mov ecx,[ebp+12]

The asm code above is what was shown in class to access the array. 上面的asm代码是在类中显示的用于访问数组的代码。 At this point, does the pointer iterate to the next index if I add 4 to ebx register? 此时,如果我在ebx寄存器中加4,指针是否会迭代到下一个索引?

Let say the first index (0) in my array is 'a'. 假设我数组中的第一个索引(0)是'a'。 Does performing mov ebx,[ebp+8] set ebx to the address of where 'a' is? 执行mov ebx,[ebp + 8]是否将ebx设置为“ a”所在的地址? If so, incrementing ebx 4 bytes should move it to the next index, correct? 如果是这样,递增ebx 4字节应将其移至下一个索引,对吗?

Since I am still learning and this is very new to me, I apologize for any syntax mistakes. 由于我仍在学习,这对我来说是很新的,对于任何语法错误,我深表歉意。 I believe I am just trying to get more clarifications rather than answers. 我相信我只是想获得更多的说明,而不是答案。

At this point, does the pointer iterate to the next index if I add 4 to ebx register? 此时,如果我在ebx寄存器中加4,指针是否会迭代到下一个索引?

Your declaration of swap is extern"C" int swap(char*, int); 您的swap声明为extern"C" int swap(char*, int);

Since the pointer is a pointer to char , you should add 1 to ebx to make it point to the next element. 由于该指针是指向char的指针,因此应在ebx加1以使其指向下一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM