【问题标题】:Inline asm and c array questions内联 asm 和 c 数组问题
【发布时间】:2014-05-10 01:40:21
【问题描述】:

这是一个家庭作业。我有 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 ,直到我找到 this solution

多次尝试以后的代码

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 个数组,并将它们传递给 changedArray,但现在没有更改它们。我很确定使用矢量你不必传递 &,我可能是错的。不过,我不明白为什么它没有改变它们。所以...

最终代码

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中犯了一个错误,但我没有看到它。

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

【问题讨论】:

  • int 占用 4 个字节。如果没有TYPE myarray,您将按字节索引,而不是乘以int 的大小。
  • 谢谢,我明白了。

标签: c++ arrays visual-c++ assembly inline-assembly


【解决方案1】:
mov eax, myArray[TYPE myArray*ecx]

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多