Arrays em assembly (80x86)
- Um array (vetor) em assembly pode ser visto como um bloco sequencial
de memoria. Uma forma de acessar um componente (celula) do array e' por meio
do modo de enderecamento indexado, calculando o deslocamento do elemento
desejado a partir do endereco de inicio do array.
Por exemplo, para acessar o terceiro elemento do array (assumindo
que cada elemento possui dois bytes):
mov ax, [array + 3 * 2]
Assim, de forma geral para acessar o elemento em uma posicao
n qualquer do array:
mov ax, [inicio_do_array + N *
tamanho_do_elemento]
- Um array multidimensional (matriz) e' representado na memoria
de forma equivalente a um vetor, porem e' utilizada uma forma diferente para
calcular os indices e acessar os elementos. Formula para um array de
duas dimensoes:
(linha * tamanho_da_linha + coluna ) * tamanho_do_elemento
Abaixo uma duvida que surgiu em uma lista de discussao sobre o uso de arrays
bi-dimensionais (matrizes) usndo assembly com NASM:
http://pub1.ezboard.com/fpatorjksmessageboards33340frm5.showMessage?topicID=6.topic
Moving through a 2D array in Assembly
I'm writing a C program that calls a function written in Assembly. The assembly
function takes in a 2D array and I need to loop through it. I've been working
for a while but I can't seem to figure it out. I'm also using NASM. Here's
how it's being called from C:
int the_array[100][100], r = 10, c 20;
.
.
. some code here...
.
// we only want to deal with the first 10 rows and 20 columns
i = the_function(the_array, r, c);
In the .data section of the assembler I have this variable:
themat dd 0
In the code section this is the basics of what I'm doing to use the variable:
; getting the array
mov eax, [esp + 4]
mov [themat], eax
; here's how I'm getting the element from the array: (problem area)
; esi = for looping through the columns
; edx = for looping through the rows
mov eax, [themat + 4*esi + edx]
mov eax, [eax]
Re: Moving through a 2D array in Assembly
If both esi and edx equal zero, eax will equal the first element in the array,
which is correct. But I'm not sure how to set the other parts up so they
work correctly. I also know r and c are being passed in with their correct
values. I've been working on it all afternoon but I can't seem to make any
progress, any suggestions? (note: that statement is inside of a for loop
that is inside of a for loop)
the_array[y][x] in 1-D terms is the_array[(y*c)+x] where c is the number
of columns.
i wrote this in masm, but you should be able to adapt it:
r dd 10
cc dd 20 (c is a reserved word
in masm)
...
mov ecx, 200
mov edi, 0 ;column
mov esi, 0 ;row
mov ebx, themat
_loop:
mov eax, esi ;eax=y
mul cc ;y*20
mov edx, edi ;edx = x
add eax, edx ;eax = y*20+x
mov edx, [ebx+eax*4]
inc edi
cmp edi, cc
jne @F
inc esi
mov edi, 0
@@:
;PrintDec edx ;debug output stuff
dec ecx
jnz _loop