内联asm和c数组问题

Inline asm and c array questions

本文关键字:数组 问题 asm 内联      更新时间:2023-10-16

这是一个家庭作业。我有3个数组,v1={5,4,3,2,1},v2={1,2,3,4,5}和v3={2,3,5,1,4},赋值是将1改为6。当然,在asm或c中,任何像v1[4]=6这样的解决方案都是被禁止的。这就是我的代码:

第一个代码

void main(){
int myArray[5]={5,4,3,2,1};
__asm {
    mov ecx,0 //using ecx as counter
myLoop: 
    mov eax, myArray[ecx] //moving the content on myArray in position ecx to eax
    cmp eax,1            //comparing eax to 1
    je is_one            //if its equal jump to label is_one
    inc ecx              //ecx+1
    cmp ecx,5     //since all vectors have size 5, comparing if ecx is equal to 5
    jne myLoop          //if not, repeat
    jmp Done            //if true, go to label Done
is_one: 
    mov myArray[ecx],6 //changing the content in myArray position ecx to 6
    inc ecx            //ecx+1
    cmp ecx,5          // ecx=5?
    jne myLoop         //no? repeat loop
    jmp Done           //yes? Done
Done:
    }
printArray(myArray);
}

这不起作用,尝试了许多类似mov eax,6mov [eax+ecx],6的方法,直到我找到这个解决方案,才起作用

以后多次尝试编码

void main(){
int myArray[5]={5,4,3,2,1};
__asm {
    mov ecx,0 //using ecx as counter
myLoop: 
    mov eax, myArray[TYPE myArray*ecx] //I don't understand how this works
    cmp eax,1            //comparing eax to 1
    je is_one            //if its equal jump to label is_one
    inc ecx              //ecx+1
    cmp ecx,5     //since all vectors have size 5, comparing if ecx is equal to 5
    jne myLoop          //if not, repeat
    jmp Done            //if true, go to label Done
is_one: 
    mov myArray[TYPE myArray*ecx],6 //Uhh...
    inc ecx            //ecx+1
    cmp ecx,5          // ecx=5?
    jne myLoop         //no? repeat loop
    jmp Done           //yes? Done
Done:
    }
printArray(myArray);
}

这就像一种魅力。但我不明白MOV array[TYPE array * index], value是如何或为什么工作的(除了TYPE返回链接中解释的大小之外(,为什么其他的不工作。

此外,由于我必须对3个数组执行此操作,我尝试将所有代码复制并粘贴到changingArray(int myArray[]),在main中声明了3个数组,并将它们传递给changingArray,但现在没有更改它们。我很确定,对于向量,你不必通过&,我可能错了。尽管如此,我不明白为什么它不能改变他们。所以…

最终代码

void changingArray(int myArray[]){
__asm {
    mov ecx,0 //using ecx as counter
myLoop: 
    mov eax, myArray[TYPE myArray*ecx] //I don't understand how this works
    cmp eax,1            //comparing eax to 1
    je is_one            //if its equal jump to label is_one
    inc ecx              //ecx+1
    cmp ecx,5     //since all vectors have size 5, comparing if ecx is equal to 5
    jne myLoop          //if not, repeat
    jmp Done            //if true, go to label Done
is_one: 
    mov myArray[TYPE myArray*ecx],6 //Uhh...
    inc ecx            //ecx+1
    cmp ecx,5          // ecx=5?
    jne myLoop         //no? repeat loop
    jmp Done           //yes? Done
Done:
    }
printArray(myArray);
}
void main(){
   //for some odd reason, they arent changing
   int v1[5]={5,4,3,2,1}; 
   int v2[5]={1,2,3,4,5};  
   int v3[5]={2,3,5,1,4};
   changingArray(v1);
   changingArray(v2);
   changingArray(v3);
}

TL:DR部分

将3个数组中的数字1更改为6的作业v1={5,4,3,2,1},v2={1,2,3,4,5}和v3={2,3,5,1,4}

1-我不明白为什么第一个代码不起作用,但后来的代码(MOV array[TYPE array * index], value指令(进行了多次尝试。

2-因为我需要用3个数组来做这件事,所以我把所有的代码都放在changingArray(int myArray[])中,在main中我声明了我的3个数组,如最终代码所示。虽然多次尝试代码确实更改了数组,但这并没有。也许我只是在c而不是asm中犯了一个错误,但我没有看到。

抱歉英语不好,这不是我的第一语言。

mov eax, myArray[TYPE myArray*ecx]

这里引用的地址是(myArray的基地址(+sizeof(myArray元素的类型(*ecx。在汇编语言中,索引应该以字节为单位。