1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define ROW 10
#define COLUMN 25
int GetMatrixValue(int** pMatrix, int i, int j)
{
int ret_val;
__asm{
; preserve regs
push esi
push ebx
push edx
; get matrix pointer in source reg.
mov esi, pMatrix
; load indice
mov ebx, i
mov edx, j
; get address of row depending on i
mov esi, [esi+ebx*4]
; get value in row i column j
mov eax, [esi+edx*4]
; restore regs
pop edx
pop ebx
pop esi
mov ret_val, eax
}
return ret_val;
}
int main(void)
{
int** pMatrix = NULL;
int i, j, count;
pMatrix = (int**)malloc(ROW * sizeof(int*));
for(i = 0; i < ROW; i++)
pMatrix[i] = (int*)malloc(COLUMN * sizeof(int));
count = 0;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COLUMN; j++)
{
pMatrix[i][j] = count;
count++;
}
}
//TODO: free columns and rows
#if _DEBUG
assert( GetMatrixValue(pMatrix, 0, 0) == 0 );
assert( GetMatrixValue(pMatrix, 1, 0) == 25 );
assert( GetMatrixValue(pMatrix, 2, 1) == 51 );
printf("test passed!");
#else
printf("matrix[0][0] = %d (expected: 0)\n", GetMatrixValue(pMatrix, 0, 0));
printf("matrix[1][0] = %d (expected: 25)\n", GetMatrixValue(pMatrix, 1, 0));
printf("matrix[2][1] = %d (expected: 51)\n", GetMatrixValue(pMatrix, 2, 1));
#endif
return 0;
} |
Partager