Reputation: 90
I'm currently doing my project in assembly for my university.
The goal is to write the exact same application in C/C++ and asm.
The part with C++ was easy.
The problem starts when I want to access a 2D array in asm and the internet is very scarce in this type of situation.
In my main part of application I have:
extern "C" int _stdcall initializeLevMatrix(unsigned int** x, DWORD y, DWORD z);
and my asm function:
initializeLevMatrix PROC levTab: PTR DWORD, len1: DWORD, len2: DWORD
xor eax, eax
mov DWORD PTR [levTab], eax ; I want to pass 0 to the first element
mov ebx, eax
mov ecx, len1
init1:
cmp eax, ecx ; compare length of a row with a counter
jge init2 ; jump if greater or the same
inc eax ; increment counter
mov ebx, eax ; index
imul ebx, ecx ; multiply the index and the length of a row
imul ebx, 4 ; multiply by the DWORD size
mov DWORD PTR [levTab + ebx], eax ; move the value to a proper cell
jmp init1
init2:
ret
initializeLevMatrix ENDP
The function is incomplete, because I decided to fix the current problem before building it further.
The problem is that I can't get or set values.
The function should initialize the matrix as follows:
levTab[0][0..n] = 0..n
Yet I guess my poor indexing is wrong or the way I pass the parameters is wrong.
Thank you a lot for your help.
Upvotes: 2
Views: 1438
Reputation: 39676
Based on your comment "I just want to initialize the first row", it's not correct to treat len1 as the length of a row like you've written in the program. It's to be seen as the number of elements in each column.
Bring the pointer to the matrix in a register first. I suggest EDI
:
mov edi, levTab
xor eax, eax
mov [edi], eax ; I want to pass 0 to the first element
Use scaled indexed addressing
mov ebx, eax ; index
imul ebx, ecx ; multiply the index and the length of a column
mov [edi + ebx * 4], eax ; move the value to a proper cell
Upvotes: 3