[英]x86 MASM - passing and accessing a 2D array
我目前正在為我的大學進行組裝項目。
目的是用C / C ++和asm編寫完全相同的應用程序。 使用C ++的部分很簡單。
當我想在asm中訪問2D陣列並且這種情況下互聯網非常稀缺時,問題就開始了。
在應用程序的主要部分中,我有:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.