Rafael Elkoby
Rafael Elkoby

Reputation: 93

assembly function wont change the values in the array sent from c

the assembly function calculates the values of a given matrix col, it calculates everything fine but still prints a funny result. can't find the problem.

checked everything and the calculation works just fine the only problem is the values in the string that seem like they haven't been changed

  ; void sum_col(int n , int m , long int*matrix[], long int new_col[])
  ;               [BP+4]  [BP+6]       [BP+8]             [BP+12]
    .MODEL SMALL
    .STACK 100h
    .DATA 


    .CODE 
    .386
    PUBLIC _lab9c
    _lab9c proc near 

    PUSH BP
    MOV BP,SP
    ;; used registers push
    PUSH SI
    PUSH DI


    MOV DI , [BP+8] ;DI POINTS TO MATRIX
    MOV SI , [BP+12] ;SI POINTS TO NEW COL 
    MOV BX , [DI] ;POINT TO A CERTIAN ROW

    MOV CX,[bp+6]
    Nullify:
    MOV DWORD PTR [SI],0
    ADD SI,4
    loop Nullify

    MOV DX,[BP+4]
    MAT_ROW:
    MOV SI,[BP+12] ;SI POINTS TO NEW COL
    MOV CX,[BP+6]
    L1:
    MOV EAX , [BX]
    ADD EAX , [SI]
    MOV [SI],EAX
    ADD SI,4
    ADD BX,4
    MOV EAX,0
    LOOP L1
    SUB DX,1
    CMP DX,0
    JE END1
    ADD DI,2
    MOV EBX,[DI] ;POINT TO A CERTIAN ROW
    JMP MAT_ROW


    END1:
    MOV SI , [BP+12] ;SI POINTS TO NEW COL 

    POP DI
    POP SI
    POP BP
    RET

    ENDP _lab9c
    end

and this is the c code

#include <stdio.h>
#include <stdlib.h>
extern void lab9c(int n, int m, long int *matrix[], long int new_col[]);
int main() 
{
  int n = 4, m = 3, i, j;
  long int *matrix[4];
  long int new_col[3];  
  for(i=0; i < n; i++)
     matrix[i] = (long int *)malloc(m*sizeof(long int));
  for(i=0; i < n; i++)
    for(j=0; j < m; j++)
       matrix[i][j] = 100*i+j;
  lab9c(n, m, matrix, new_col);
  printf("matrix:\n");    
  for(i=0; i < n; i++) {
for(j=0; j < m; j++)  
      printf("%8ld",matrix[i][j]); 
    printf("\n");
  } // for
  printf("new_col:\n");    
  for(i=0; i < m; i++)
     printf("%8ld",new_col[i]); 
 return 0;
} // main

Upvotes: 0

Views: 95

Answers (1)

Rafael Elkoby
Rafael Elkoby

Reputation: 93

new_coal address is not at [bp+12], it is in [bp+10] . the (long int *matrix[]) is in the size of 2 bytes and not 4 because it carries addresses. thanks for the help!

Upvotes: 1

Related Questions