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

标签 c++ arrays assembly x86 masm

我目前正在为我的大学做我的 assembly 项目。
目标是用 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

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

非常感谢您的帮助。

最佳答案

根据您的评论“我只想初始化第一行”,将 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

关于c++ - x86 MASM - 传递和访问二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40962781/

相关文章:

c++ - 在初始化时定义位集大小?

assembly - 用该行的作用来注释复制循环的每一行

c++ - 使用 step 遍历一维数组和二维数组

c - 如何编写没有 'main'的C程序?

c++ - 将 asm 指令编码为操作码

c++ - std::mutex 是否强制缓存内聚?

c++ - 类和模板类的不同规则?

c++ - 从 C++ 程序启动外部应用程序并将其附加到 Visual 2008 调试器,同时在 WinAPI 中调试主机

java - Java 数组是如何处理的?

Javascript - 数组的平均值不正确?