【问题标题】:Assembler - trouble with arrays汇编程序 - 数组的麻烦
【发布时间】:2015-01-19 01:20:19
【问题描述】:

我对汇编器中的代码有一些问题,该代码负责将一个数组复制到另一个数组(整数数组)。

这就是我在 c++ 中创建数组的方式:

int *myArray=new int[myFunctions::getCounter()];
int *newArray;
int *newArray = new int[counter+1]; // I have some information in [0]

这就是我声明汇编函数的方式

extern int  copy(myArray[], int *newArray, int howManyElements, int howManyProcesses, int indexOfProcess);

关于流程:这是项目的一部分。过程(循环中)的工作方式如下:

在示例中,我们在数组中有 3 个进程和 10 个元素

第一个进程复制到新的数组元素中:[1] [4] [7] [10] 第二 [2] [5] [8] 第三 [3] [6] [9]

在 C++ 中:

for (int i=indexOfProcess; i<=howManyElements; i+=howManyProcess){

        newArray[i]  = myArray[i];

    }

在 c++ 中它工作正常。在汇编程序中我遇到了麻烦。只有第一个元素被正确复制。其余元素不变。 MASM 代码:

copy proc uses ebx ecx edx esi edi myArray:DWORD, newArray:DWORD, howManyElements:DWORD, howManyProcesses:DWORD, indexOfProcess:DWORD
 
 
 
 
local ahowManyProcesses:DWORD 
local ahowManyElements:DWORD              
 
local  adressOfArray:DWORD
 
mov eax, howManyElements          
mov ahowManyElements, eax     
mov eax, howManyProcesses          
mov ahowManyProcesses, eax    
 
 
xor eax, eax            
 
mov ecx, indexOfProcess          
mov ebx, myArray       
mov edx, newArray   
mov adressOfTab, edx
 
 
add ebx, ecx               
add edx, ecx               
add eax, ecx                   
 
 
loop:
mov eax, [ebx]           
mov [edx], eax          
add ebx, ahowManyProcesses 
add edx, ahowManyProcesses
add eax, ahowManyProcesses
 
 
cmp eax, ahowManyElements 
jbe loop                              
 
 
 
mov eax, adressOfArray
ret
 
copy endp
 
end

例如:

myArray [1] = 10 [2] = 11 [3] = 3 [4] = 13...

过程前的newArray [1] = 0 [2] = 0 [3] = 0 [4] = 0...

过程后的newArray [1] = 10 [2] = 0 [3] = 0 [4] = 0...

我认为 DWORD/BYTE 存在一些问题。我尝试将 ecx 更改为 [ecx+4] 和其他组合,但我不知道如何修复代码。

感谢您的帮助:)

【问题讨论】:

    标签: assembly integer cpu-registers dword


    【解决方案1】:

    我认为你至少有两个问题。一,您没有缩放整数大小。您必须将指针增加 4 倍的项目数。另一个问题是您尝试将eax 用于循环计数器,但同时您也将其用作副本的临时对象,因此您会覆盖您的计数器。

    mov ecx, indexOfProcess          
    mov ebx, myArray       
    mov edx, newArray   
    mov adressOfTab, edx
    
    lea ebx, [ebx+ecx*4]       ; scale by 4 for int size
    lea edx, [edx+ecx*4]       ; scale by 4 for int size
    
    loop:
    mov eax, [ebx]
    mov [edx], eax
    mov eax, ahowManyProcesses ; the increment in items
    lea ebx, [ebx+eax*4]       ; scale by 4 for int size
    lea edx, [edx+eax*4]       ; scale by 4 for int size
    add ecx, eax               ; do not scale, it is a counter
    
    cmp ecx, ahowManyElements 
    jbe loop
    

    PS:你应该学会使用调试器,这样你就可以修复自己的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2014-02-03
      • 2014-05-25
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      相关资源
      最近更新 更多