如何在MASM中访问两个二维数组 [英] How to access two dimmensional arrays in MASM
问题描述
您能告诉我如何在MASM中访问两个二维数组吗?
Can you show me how I can access two dimmensional arrays in MASM?
C ++代码:
int k = 0
for (i = 0; i<5; ++i)
{
if (k == text.length())break;
for (j = 0; j<2; ++j)
{
for (t = 0; t<26; ++t)
{
if (text[k] == letters[t]){ tab[i][j] = t; k++; break; }
}
}
}
MASM
mov al, [ebx] ;ebx - begin of text array
xor esi, esi
for1:
cmp al, 00
je break_for1
mov j, 0
for2:
mov t, 0
mov ecx, adrAlphabet ;ecx - begin of letters array
for3:
;if (text[k] == letters[t]){ tab[i][j] = t; k++; break; }
mov ah, [ecx]
cmp ah, 00
je end_of_alphabet
cmp al, 00
je end_of_text
cmp al, ah
jne not_equal
;here comes the problem
mov edx, t
mov [letters + (esi*4 + j)*4], edx
;
inc k
inc ebx
jmp break_t
; end if
not_equal:
inc ecx
inc t
cmp t, 26
jne for3
break_t:
inc j
cmp j, 2
jne for2
inc esi
cmp esi, 5
jne for1
break_for1:
end
那只是我的代码的一部分,但是我只想了解数组. 您能否举一些例子,说明如何在MASM中使用tab [i] [j]? 另一个问题是,如何修改 tab 数组的长度?简而言之:在程序启动时,将计算 tab 大小,然后我要设置数组大小.
That's only part of my code, but I just want understand arrays. Can you give some example how I can use tab[i][j] in MASM? Another question is, how I can modify the length of tab array? In short: at start of program the tab size will be calculated, then I want to set array size.
谢谢
推荐答案
C ++标准保证大小为[a][b]
的二维数组等效于大小为[a*b]
的单个一维数组.
您可以不再将其视为二维数组,而将其视为普通数组,而只是连续的数据块.
A two dimensional array of size [a][b]
is guaranteed by the C++ standard to be equivalent to a single one dimensional array of size [a*b]
.
You can stop thinking of it as a two dimensional array and just think of it as a normal array, just a contiguous chunk of data.
也就是说,如果您要访问tab[i][j]
,则与访问tab+a*i+j
That is, if you want to access tab[i][j]
it's exactly the same as accessing tab+a*i+j
如果您知道a
(数组中的行数)始终为2、4或8,则可以直接使用
If you know that a
(the number of rows in our array) is always 2, 4, or 8 you can directly use an expression of the form
mov byte ptr [eax*8+ecx], someValue
我是eax,j是ecx.
Where i is eax and j is ecx.
否则,您将必须使用shifts或imul手动计算i*a+j
,并将其用作数组的索引.
Otherwise you will have to compute i*a+j
manually, using shifts or imul and use that as an index into your array.
例如:
; Write 0 at tab[i][j]
; We use EDX because of how IMUL works
mov edx, i ; EDX = i
imul a ; EDX = i*a
add edx, j ; EDX = i*a+j
add edx, offset tab ; EDX = tab[i*a+j] = tab+i*a+j
mov byte ptr [edx], 0
这篇关于如何在MASM中访问两个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!