Search code examples
c++arraysassemblyx86character-arrays

How to increment an array in x86 assembly?


How would you increment an array using x86 assembly within a for loop. If the loop (made using c++) looked like:

for (int i = 0; i < limit; i++)

A value from an array is put in a register then the altered value is placed in a separate array. How would I increment each array in x86 assembly (I know c++ is simpler but it is practice work), so that each time the loop iterates the value used and the value placed into the arrays is one higher than the previous time? The details of what occur in the loop aside from the array manipulation are unimportant as I would like to know how this can be done in general, not a specific situation?


Solution

  • The loop you write here would be:

       xor eax, eax   ; clear loop variable
       mov ebx, limit
    loop:
       cmp eax, ebx
       je done
    
       inc eax
       jmp loop
    
    done:
     ...
    

    I really don't understand what you mean by "increment an array".

    If you mean that you want to load some value from one array, manipulate the value and store the result in a target array, then you should consider this:

    Load the pointer for the source array in esi and the target pointer in edi.

     mov esi, offset array1
     mov edi, offset array2
     mov ebx, counter
    
     loop:
     mov eax, [esi]
     do what you need
     move [edi], eax
    
     inc esi
     inc edi
    
     dec ebx
     jne loop