arraysassemblyx86masm32

how to merge multiple arrays to one array in masm32


I want to concatenate array1 array2 ... to one array how is fast way:

.data
array1      db  0CFh, 0C2h, 0ABh, 01Bh, 0C1h, 007h, 0F7h, 06Dh, 0DAh, 0F2h, 0DCh, 03Ch....; about 2000 dup
...
array2      db  0BFh, 03Dh, 087h, 0F1h, 0A6h, 097h, 0A6h, 05Bh, 010h, 0F2h, 051h, 05Eh ...

.data ?
arrayOut        dd    ?
.code
        invoke crt_memcpy, array, addr array1, sizeof array1
        mov ecx, [arrayOut]
        add ecx,sizeof array1
        invoke crt_memcpy,dword ptr[ecx], addr array2, sizeof array2    ???

I also try use code as below:

        mov eax, 0                             
        mov ecx, sizeof array1 *2   
        mov edi, 0
        mov esi, 0
L1:
        mov al, [array1 + esi * TYPE BYTE]      
        movsx eax, al    
        mov arrayOut[esi], eax 
        cmp ecx,esi
        jbe GoOnNext
        inc esi     
        loop L1
GoOnNext:
        mov ecx, sizeof array2 *2
        mov edi,0
L2:                       
        mov bl, [array2 + edi * TYPE BYTE]    
        movsx ebx, bl   
        mov arrayOut[esi], ebx   
        cmp ecx,edi
        jbe nextstep
        inc esi          
        add edi,1
        loop L2
nextstep:
        mov eax, arrayOut

But if the size of the array is large, I get the wrong result。


Solution

  • But if the size of the array is large, I get the wrong result

    With a declaration like arrayOut dd ?, there's not enough room to copy to! Based on the mention that both arrays contain about 2000 bytes, you could make this something like:

    arrayOut db 4096 dup ?
    

    Next is a basic way to copy a block of memory:

    cld                         ; Makes the addresses ESI and EDI increment
    mov  edi, OFFSET arrayOut   ; Destination
    mov  esi, OFFSET array1     ; Source
    mov  ecx, SIZEOF array1     ; Count
    rep movsb
    

    In order to concatenate, you then copy the second array but you don't modify the value in the destination register EDI. This way the second copy continues where the first copy left off.

    mov  esi, OFFSET array2     ; Source
    mov  ecx, SIZEOF array2     ; Count
    rep movsb