News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

c/cpp matrix

Started by nibbleNbits, December 11, 2011, 12:31:50 PM

Previous topic - Next topic

nibbleNbits

why doesn't this work, I'm missing somthing

; Playing with array's
; Creates a 4x2 matrix of ints
; array of pointer to array's
; example 4x2 matrix
;
  • ->[1][2]
    ; [1]->[3][4]
    ; [2]->[5][6]
    ; [3]->[7][8]

    .586
    .model flat, C
    option casemap :none

    .data

    .code
    ;-------------------------------------------------------------------
    ; void matrx_ptrs(int **intmat,int rows, int cols); cpp calling function
    ;-------------------------------------------------------------------
    matrx_ptrs proc public intmat:PTR DWORD, rows:DWORD, cols:DWORD

        push eax
       push ebx
       push ecx
       push edx
       
       ;--------------- calc size ---------------------
       mov ebx, rows
       imul ebx,cols
       add ebx, rows
       ;------------------------------------------------------
       
       mov edx, intmat               ; matrix [rows][cols]
       mov ecx,rows               ; row counter start at end of rows
       mov eax,1                  ; integer value starting at one to add to matrix
       .while(ecx < ebx)            ; should be 4-12
          mov [edx+ecx*4],eax
          add eax,1
          add ecx,1
       .endw
       
       pop edx
       pop ecx
       pop ebx
       pop eax

        ret

    matrx_ptrs endp

    end


qWord

Quote from: nibbleNbits on December 11, 2011, 12:31:50 PM;-------------------------------------------------------------------
; void matrx_ptrs(int **intmat,int rows, int cols); cpp calling function
;-------------------------------------------------------------------
matrx_ptrs proc public intmat:PTR DWORD, rows:DWORD, cols:DWORD
...
   mov edx, intmat               ; matrix [rows][cols]
You must dereference the pointer a second time by:
mov edx,[edx]
Alternatively you can change the Cpp function:
Quote; void matrx_ptrs(int *intmat,int rows, int cols); cpp calling function

Quote from: nibbleNbits on December 11, 2011, 12:31:50 PM
   ;--------------- calc size ---------------------
   mov ebx, rows
   imul ebx,cols
   add ebx, rows  ; <- ???

You are calculating the number of cells and not the size.
FPU in a trice: SmplMath
It's that simple!

nibbleNbits

c/cpp requires the ** notation. it works fine in c++
int val = 0;
for(int i = 0;i < rows; i++)
      for(int j = 0; j < cols; j++)
   {
      intmat[j] = val;
      val++;
   }
            
I added mov edx, [edx] but that didn't help
mov edx, [intmat]
mov edx, [edx]

I realized that about calculating rows and not the size, poor choice of words  :red

still does't work

qWord

Quote from: nibbleNbits on December 11, 2011, 01:41:51 PM
c/cpp requires the ** notation.
surely not: you can simply use an two dimensional integer array.
The following code is speculative, because I don't know the data type of intmat:

mov edi,intmat  ; -> push/pop edi
; edi = pointer to array of  'row'-pointers
xor eax,eax
mov edx,1 ; skip first row
.while edx < rows
mov esi,PDWORD ptr [edi+edx*4] ; -> push/pop esi
; esi = pointer to row[edx]
xor ecx,ecx
.while ecx < cols
; loop through all elements in row
mov SDWORD ptr [esi+ecx*4],eax
lea eax,[eax+1]
lea ecx,[ecx+1]
.endw
lea edx,[edx+1]
.endw
FPU in a trice: SmplMath
It's that simple!

nibbleNbits

qWord thanks for you input and help. If I use a int[4][2] and a slightly different masm algo I can make it work just fine, however I don't alway know the size. Hence the reason for dynamic allocation using pointers, in this case pointer to pointers,

int **intmat;
intmat = new int*[rows];
            
for(int i= 0; i < rows; i++)
    intmat = new int[cols];

anyway you algo works fine except the first row? not sure I have figured out just yet why you skipped row 1? the last (3) rows come out correct.

again thanks so much for you input

nibbleNbits

I changed mov edx,1 ; skip first row to mov edx,0 ; skip first row and all is well, almost orgasm time.

thanks again

qWord

Quote from: nibbleNbits on December 11, 2011, 03:01:37 PManyway you algo works fine except the first row? not sure I have figured out just yet why you skipped row 1? the last (3) rows come out correct.
Reading your first post, I thought that you want to skip the first row.

qWord
FPU in a trice: SmplMath
It's that simple!