Search code examples
assemblymasmirvine32

Summing arrays correctly


I'm using MASM & Irvine 32bit Assembly, and I have arrays A, B, C, and I'm stacked on performing A + B = C, summing each [i] item of array A with array B and writing to array C.

For instance,

 arrA 1, 2, 4, 1
 +
 arrB 2, 1, 1, 3 
 =
 arrC 3, 3, 5, 4

I've tried to work with pointers, but I had 00, 00, 00, 0F output.

Don't pay attention to StrHex_MY procedure, it is tested on outputting arrays.

Code:


.586
.model flat, stdcall
ExitProcess PROTO, dwExitCode:DWORD

include \Irvine\Irvine32.inc
includelib \Irvine\kernel32.lib
includelib \Irvine\user32.lib
include module.inc

.data
  CaptionGreet BYTE "Test me", 0

  arrA DWORD 1, 2, 4, 1 
  arrB DWORD 2, 1, 1, 3 
  arrC DWORD 0, 0, 0, 0

  toOut DB 64 dup(?)

.code

main PROC

    mov edi, OFFSET arrA ; Address of arrA
    mov esi, OFFSET arrB ; Address of arrB

    mov eax, 0 ; Register with result

    mov ecx, LENGTHOF arrA ; Lenght of arrays

    L1:
        add eax, [edi] ; Add current arrA element to eax
        add eax, [esi] ; Add current arrB element to eax

        add edi, TYPE arrA ; Move pointer to the next arrA element
        add esi, TYPE arrB ; Move pointer to the next arrB element

        mov arrC, eax ; Move current eax value to arrC

        loop L1

    ; Converting result to HEX toOut. Don't pay attention to this part
    ; ----
    push OFFSET toOut
    push OFFSET arrC
    push 256
    call StrHex_MY
    ; ---

    ; Output result
    INVOKE MessageBoxA, 0, ADDR toOut, ADDR CaptionGreet, 0
    INVOKE ExitProcess,0

main ENDP
END main


Solution

  • I guess you want to add each item of arrA to its correspondent item of arrB and to store it to the correspondent item of arrC. So

    add eax, [edi] ; Add current arrA element to eax
    

    is wrong. You have to "reinitialise" EAX with the item:

    mov eax, [edi] ; Copy current arrA element to eax
    

    You need a third pointer to arrC. Right now you store the result repeatedly to the first item of arrC. I've chosen EBX as third pointer.

    INCLUDE Irvine32.inc
    
    .data
      CaptionGreet BYTE "Test me", 0
    
      arrA DWORD 1, 2, 4, 1
      arrB DWORD 2, 1, 1, 3
      arrC DWORD 0, 0, 0, 0
    
      toOut DB 64 dup(?)
    
    .code
    
    main PROC
    
        mov edi, OFFSET arrA    ; Address of arrA
        mov esi, OFFSET arrB    ; Address of arrB
        mov ebx, Offset arrC    ; Address of arrC
    
        mov eax, 0              ; Register with result
    
        mov ecx, LENGTHOF arrA  ; Length of arrays
    
        L1:
            mov eax, [edi]      ; Copy current arrA element to eax
            add eax, [esi]      ; Add current arrB element to eax
    
            add edi, TYPE arrA  ; Move pointer to the next arrA element
            add esi, TYPE arrB  ; Move pointer to the next arrB element
    
            mov [ebx], eax      ; Move current eax value to current arrC element
            add ebx, TYPE arrC  ; Move pointer to the next arrC element
    
            loop L1
    
        mov esi, OFFSET arrC
        mov ecx, 4
        mov ebx, 4
        call DumpMem
    
    
        INVOKE ExitProcess,0
    
    main ENDP
    
    END main