Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 打印C++;数组通过汇编_C++_Arrays_Assembly_X86 - Fatal编程技术网

C++ 打印C++;数组通过汇编

C++ 打印C++;数组通过汇编,c++,arrays,assembly,x86,C++,Arrays,Assembly,X86,目前,我有很多问题。当我调试我的代码时,我的C++运行正常,直到我到达汇编函数调用,它跳转到我的汇编代码中的L2而不是L1。我不知道它为什么这样做。最重要的是,我试图将数组打印到屏幕上,但现在,我只得到了一个巨大的数字。我尝试包括欧文的库,并使用“WriteDec”尝试打印出元素,但该库甚至无法识别 下面是我在汇编中的当前代码,从C++代码中取出3个数组,并将它们添加到一起。 .model flat, C .model flat, STDCALL .code ASMsumarray PROC,

目前,我有很多问题。当我调试我的代码时,我的C++运行正常,直到我到达汇编函数调用,它跳转到我的汇编代码中的L2而不是L1。我不知道它为什么这样做。最重要的是,我试图将数组打印到屏幕上,但现在,我只得到了一个巨大的数字。我尝试包括欧文的库,并使用“WriteDec”尝试打印出元素,但该库甚至无法识别

下面是我在汇编中的当前代码,从C++代码中取出3个数组,并将它们添加到一起。

.model flat, C
.model flat, STDCALL

.code
ASMsumarray PROC,
ptr1:PTR DWORD,     ; points to array
ptr2:PTR DWORD,     ; points to array
ptr3:PTR DWORD      ; points to array
pushad              

    mov esi,ptr2  
    mov edi,ptr3  
    mov ecx,10    

    L1:
        mov ebx,[edi]  ;mov first elem of ptr3 to ebx
        add ebx,[esi]  ;add elem from ptr2 to ebx
        mov [esi],ebx  ;mov ebx to spot in ptr2. ptr2 elem now contains sum of ptr2 and 3
        add edi,4
        add esi,4
    loop L1
        mov edi,ptr1  ;mov ptr1 to esi
        mov ecx,10    ;mov ptr2 to edi
        sub edi,40
        sub esi,40
    L2:
        mov ebx,[edi]  ;mov first elem of ptr2 to ebx
        add ebx,[esi]  ;add elem from ptr1 to ebx
        mov [esi],ebx  ;mov ebx to spot in ptr1. ptr1 elem now contains sum of ptr1 and 2
        add edi,4
        add esi,4
    loop L2

    popad               ;//pop registers off the stack
        ret
ASMsumarray ENDP

这是C++代码。< /P>

#include <iostream>
#include <ctime>

using namespace std;

extern "C" int ASMsumarray(int array1[], int array2[], int array3[]);

int main() {
    srand(time(NULL));      //seed rand num     

    int array1[10] = { 1,1,1,1,1,1,1,1,1,1 };
    int array2[10] = { 1,1,1,1,1,1,1,1,1,1 };
    int array3[10] = { 1,1,1,1,1,1,1,1,1,1 };
/*
    for (int i = 0; i < 10; i++) {
    array1[i] = rand() % 10;
    array2[i] = rand() % 10;
    array3[i] = rand() % 10;
    cout << endl;
    }
    cout << endl;
    */

    //for (int i = 0; i < 10; i++) {
    //cout << array1[i] << endl;
    //}

    cout << "The number is " << ASMsumarray(array1, array2, array3) << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
外部“C”int-ASMsumarray(int-array1[],int-array2[],int-array3[]);
int main(){
srand(time(NULL));//种子rand num
int array1[10]={1,1,1,1,1,1,1,1,1};
int-array2[10]={1,1,1,1,1,1,1,1};
int-array3[10]={1,1,1,1,1,1,1,1};
/*
对于(int i=0;i<10;i++){
数组1[i]=rand()%10;
array2[i]=rand()%10;
数组3[i]=rand()%10;
库特
最重要的是,我试图将数组打印到屏幕上,但现在,我只得到了一个巨大的数字

您的函数应该返回一个
int
,但您正在使用
popad
来恢复所有寄存器,包括调用方的
eax
值。因此,您的返回值是调用方在
eax
中留下的任何垃圾

此外,您从未将值放入
eax
:您正在使用的调用约定要求在
eax
中返回值,就像几乎所有x86调用约定一样

只需使用
push
/
pop
(并尽可能少地使用,例如,对指针和临时表使用
eax
ecx
、和
edx
)来保存/恢复实际需要使用的保留呼叫的寄存器即可


<>和BTW,C++调用方没有打印数组,它只打印了和(函数返回值)。函数的返回值是标量。不清楚为什么修改数组内容。


当我调试我的代码时,我的C++运行正常,直到我到达汇编函数调用,它跳转到我的汇编代码中的L2而不是L1. ./P>
这毫无意义。请确保在实际函数入口点(不是L1或L2)设置断点。或者一步一步地将指令输入到函数中,而不是使用C语句。

子edi,40
是错误的。您刚刚从
ptr1
加载了
edi
,不应该从中减法。顺便说一句,重新加载
esi
比减法更容易阅读。标题有点误导Eing因为它们通常被称为C数组,而C++有一个<代码> STD::数组< /Cord>我可以问你为什么使用汇编代码来做这个?@ USER442实验室。教授只告诉我我的增量是关闭的,这是一个愚蠢的错误,但是我在这里被绊倒了。你有<代码> MOV EDI,PTR1但是,然后<代码>子EDI,40 < /代码>。这是不允许的。感觉