我目前正在为我的大学做我的 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/