Moving through a 2D array

hey,

I'm writing a C program that calls a function written in Assembly. The assembly function takes in a 2D array and I need to loop through it. I've been working for a while but I can't seem to figure it out. I'm also using NASM. Here's how it's being called from C:

int the_array[100][100], r = 10, c 20;
.
.
. some code here...
.
// we only want to deal with the first 10 rows and 20 columns
i = the_function(the_array, r, c);

In the .data section of the assembler I have this variable:

themat dd 0

In the code section this is the basics of what I'm doing to use the variable:

; getting the array
mov eax, [esp + 4]
mov [themat], eax

; here's how I'm getting the element from the array: (problem area)
; esi = for looping through the columns
; edx = for looping through the rows
mov eax, [themat + 4*esi + edx]
mov eax, [eax]

If both esi and edx equal zero, eax will equal the first element in the array, which is correct. But I'm not sure how to set the other parts up so they work correctly. I also know r and c are being passed in with their correct values. I've been working on it all afternoon but I can't seem to make any progress, any suggestions? (note: that statement is inside of a for loop that is inside of a for loop).

Comments

  • ok,an array[100][100] filled with integers is totaly 100*4*100 bytes big,because there are 100*100 integers,which are on x86 systems in 32bit-mode 4 bytes.to access an element of this,you can use the following formular:

    OFFSET Element = OFFSET Array + (100*4)*y+x

    In Asm-Code this is:

    mov ebx,5*100*4+5*4
    mov eax,[IntTable + ebx] ; IntTable[5][5] is now in eax.

    When youre using inline-assembly in a win32-c++ compiler,you could access the IntTable (when its global or in the same proc,of course) simply as shown above,in a __asm{} block.

    when youre doing it in an extern Object-Modul completly written in asm,you could access global-variables simply by declaring it as extern:

    extrn IntTable:DWORD

    When you pass the address of the table via a functioncall,you need to know how the c++compiler puts the arguments on the stack(c-convention,stdcall-convention,ect) and access it through the esp/ebp registers (stackframe).and dont forget,that most calling-conventions put the address of the first element in the table on the stack:
    (I used the tasm-style,it uses stdcall)

    .MODEL FLAT;STDCALL
    ASMfUNC proc IntTable:DWORD
    mov ebx,[IntTable]
    mov edx,100*4*5+5*4
    mov eax,[ebx+edx]
    ret
    AsmFunc ENDP
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion