• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Matrix in assembly mxn dimension in emu 8086

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

zemzela

New Member
Joined
Jan 5, 2012
I made a code in assembly 8086. I load matrix (array) in memory with dimension 3x3. but this code works just for this dimension of matrix 3x3. Could someone give me an idea how could i make it to work with dimension m x n? the array is loaded in memory and at the end just print the result, another array. thanks

Code:
; multi-segment executable file template.

data segment
matrix db 1, 2, 3, 4, 5, 6, 7, 8, 9 ; load matrix in memory

ends

stack segment
dw 128 dup(0)
ends

code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax



mov bx, matrix ; move matrix to offset bx
mov ch, 3
mov cl, 0


COLUMNAHEAD: 

mov dl, [bx] ; get the first element of matrix in dl
add dl, 30h ; add to show number
mov ah, 02h
int 21h ; print first number
inc cl 

cmp cl, ch ; compare if the counter is at the end of column


jge ROWAHEAD ; if greater go to row
add bx, 3 ; if not inc offset for 3 
jmp COLUMNAHEAD





ROWAHEAD:
inc cl ; 1 element of roe
inc bx ; inc offset for one place
mov dl, [bx] ; get the number in dl
add dl, 30h ; convert to number
mov ah, 02h
int 21h ; print the number

cmp cl, 5 
je COLUMNAHEAD
jne ROWAHEAD 


COLUMNBACK:
inc cl 
sub bx, 3
mov dl, [bx]
add dl, 30h
mov ah, 02h
int 21h 
cmp cl, 7
jne COLUMNBACK 
je ROWBACK

ROWBACK:
dec bx 
mov dl, [bx] 
add dl, 30h
mov ah, 02h
int 21h 
JMP MIDDLE


MIDDLE:
add bx, 3
mov dl, [bx] 
add dl, 30h
mov ah, 02h
int 21h 

JMP END

END: 


this is the code i wrote. it works for the matrix 
1, 2, 3,
4, 5, 6, 
7, 8, 9 and print 1, 4, 7, 8, 9, 6, 3, 2, 5
 
Can't help you in assembly, but I'm curious why you don't write this in C?
 
I don't know C, I know C++, If you know C++, you may help me how to write this in C++, and I would translate in assembly. Thanks
 
If you know C++, then this C code should be easy to understand. I don't know C++.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)  {
	
   int **mat; // Pointer to pointer
   int rows, cols, i, j;
   printf("How many rows you want ");
   scanf("%d", &rows);
   //rows = 10;
   //cols = 10;
   mat = malloc(rows*sizeof(int*));       // array of number of rows


   printf("How many cols ");
   scanf("%d", &cols);
   for (i=0; i<rows; i++) {                // for each row ...
      mat[i] = malloc(cols * sizeof(int)); // add these many cols
   }
   for (i = 0; i<rows; i++) {
      for (j = 0; j<cols; j++) {
         mat[i][j] = (i+1) * (j+1);                
         printf("%4d ", mat[i][j]);        //these two print lines
         //printf("%4d ", *(*(mat+i)+j));  //do the same thing
      }
      putchar('\n');
   }
   for(i=0;i<rows;i++)                     //free malloc'd memory
      free(mat[i]);                        //rows first
   free(mat);                              //then the array pointer itself

   printf("\n\n"); 
      return 0;
}
 
Back