如何将数组的元素分配给 x86 程序集中的变量

How do I assign elements of an array to a variable in x86 Assembly?

本文关键字:程序 x86 程序集 变量 集中 分配 数组 元素      更新时间:2023-10-16

我有一个字符数组和一个temp_char变量,我想将给定索引处的字符传递给我的temp_char变量,但我真的不知道该怎么做。

我本以为类似的东西

mov     temp_char,[index*4+CharsArray]

本来可以工作的,但它不会编译。

movzx   ecx,[index*4+CharsArray]
mov     temp_char,cl

将编译,但随后导致程序中断。

我找不到任何真正解释如何将东西放入或从组装中的数组中取出的东西,如果有人能指出我正确的方向,那么我将不胜感激。 到目前为止,我已经能够蒙混过关并让事情顺利进行,但这让我完全被难住了。

谢谢。

附言

我正在尝试使用全局数组将 C++ 中的 for 循环转换为赋值的 __asm

for 循环基本上是:

for(int i(0); i<length; ++i)
{
    temp_char = CharArray[i]
    // do other stuff
}

所以为了我的__asm我有

void myfunction(int length) {
char temp_char
int index
__asm{
           mov  ebx, len    ;for part       
           mov  index,0             
           jmp  checkend
forloop:   mov  edx,index   ;add one to index           
       add  edx,1
       mov  index,edx
 checkend: cmp  index,ebx    ; check to see if completed
       jge  endfor   
           mov  temp_char,[index*4,CharsArray]   ; program breaks here
           //do other stuff

如果我的另一个程序集都是废话,我不会太烦恼,一旦我可以将我的字符从我的数组读取到我的临时变量中,我就可以解决这个问题。

希望这能更清楚。

.PPS

好吧,显然我必须从数组移动到寄存器,再寄存器移动到可变,很公平,我理解这一点。 唯一的问题是我该怎么做? 我可以让我的代码编译的唯一方法是使用上述方法:

movzx    ecx, [index*4+CharsArray]   ;as soon as program gets here it throws exception
mov      temp_char, cl

这就是我想知道的全部内容,如果有人可以提供帮助,上述两行代码应该如何工作。

谢谢。

如何只加载 cl:

mov cl, [index*4+nCharsArray]
mov temp_char, cl