Search code examples
assemblymasm32

masm32: SIMPLE array manipulation


I have a very simple problem:

I want to store bytes in a 1d array in masm32 (I just started with it yesterday, used c# before), and then modify it with some simple math, but I didnt found anything useful in the net.

tiles BYTE 12 dup (0) ; array of 12 bytes with value 0

this is how i declare the array in the .data section, basically what I want to do in C# syntax is:

for(int i = 0; i < tiles.Length; i++)
    tiles[i] += 2;

Solution

  • I can't remember the exact directives masm32 uses, but the basic structure should be something like this:

        mov edi, addr tiles ; might be called offset, some assemblers (notably gas) would use something like lea edi, [tiles] instead
        mov ecx, 12 ; the count, this could be gotten from an equ, read from a variable etc.
    for_loop:
        add byte ptr [edi], 2 ; tiles[i] += 2
        inc edi ; move to next tile
        dec ecx ; count--
        jnz for_loop ; if (count != 0) goto for_loop
    

    Or if you want it to be structured more like the c# code:

        mov edi, addr tiles
        sub ecx, ecx ; ecx = 0
    for_loop:
        cmp ecx, 12 ; ecx < tiles.Length ?
        jnl done ; jump not less
        add byte ptr [edi+ecx], 2 ; tiles[i] += 2
        inc ecx ; i++
        jmp for_loop
    done:
    

    Notice that if you change the type of tiles some of the code will have to change (the ones involving edi in particular).