x86 MASM - 传递和访问二维数组

x86 MASM - passing and accessing a 2D array

我目前正在为我的大学做我的项目。
目标是用 C/C++ 和 asm 编写完全相同的应用程序。 C++ 的部分很简单。
当我想访问 asm 中的二维数组并且在这种情况下互联网非常稀缺时,问题就开始了。

在我申请的主要部分,我有:

extern "C" int _stdcall initializeLevMatrix(unsigned int** x, DWORD y, DWORD z);

和我的 asm 函数:

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

功能不完整,因为我决定在进一步构建之前解决当前问题。

问题是我无法获取或设置值。
该函数应按如下方式初始化矩阵:

levTab[0][0..n] = 0..n

但我猜我糟糕的索引是错误的,或者我传递参数的方式是错误的。

非常感谢您的帮助。

根据您的评论 "I just want to initialize the first row",像您一样将 len1 视为 行的长度 是不正确的'已经写在程序中了。它被视为每列中的元素数。

首先将指针指向寄存器中的矩阵。我建议 EDI:

mov  edi, levTab
xor  eax, eax
mov  [edi], eax           ; I want to pass 0 to the first element

使用缩放索引寻址

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