Matrix in assembly mxn dimension

In summary, the code loads an array in memory and prints the result. However, the code only works for this dimension of the matrix.
  • #1
zemzi
2
0
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


; 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


Matrix given in memory to print the spiral in opposite direction from clockwise (left column down the right lower range, right up column, a series of upper left, etc. until you get to the environment). This works just for dimension 3x3. this should works for mxn dimension. but i don't know how, any suggestion?
 
Technology news on Phys.org
  • #2
Hey zemzi and welcome to the forums.

For the statement "mov ch,3", you should change that to the size of the column (that is, do something like mov ch,dx where dx contains the column size. You also have to change your compare for cl being 7 to mxn (i.e. change "cmp cl, 7" to "cmp cl,dx" where dx contains the value mxn).

You will actually have to change quite a few things since you have hard-coded a lot of variables in your routines.

Also I would advise you to experiment with LOOP commands as you would cut down the code and it would add to your skillset. The LOOP command uses a few registers that were designed for this purpose like the counter, and this will help you also from having to replicate your code to tell DOS to print a number (which you do quite a few times).
 
  • #3
I understand your explanation, but I don't know how to write the algorithm, could you help me?
 
  • #4
zemzi said:
I understand your explanation, but I don't know how to write the algorithm, could you help me?

From the code above it seems like you are writing code for say a 286 onwards. I don't see any 32 bit registers or other extensions, and it seems you are using a segmented memory model. Is this correct? Do you need a minimum platform requirement (like say Intel/AMD x86 32-bit as an example) and if so what is it?
 
  • #5
chiro said:
From the code above it seems like you are writing code for say a 286 onwards.
It's 8086 code as mentioned in the OP. It's using the old masm syntax for segments (as opposed to using the .model directive), but that's ok for this code.

To generalize this code, you probably want to keep the number of columns and rows in memory instead of registers. Your code could be optimized a bit. One optimization would be to have a single branch point for both the inner and outer loops. I'll leave it to you to figure out how to advance BX through the matrix based on numcol and/or numrow.

numcol db 4
numrow db 3

NEXTBYTE:
...
inc cl
cmp cl,numcol
jl NEXTBYTE
mov cl,0
... ;adjust bx for nextrow
inc ch
cmp ch,numrow
jl NEXTBYTE
... ; done
 
Last edited:

1. What is a matrix in assembly language?

A matrix in assembly language is a two-dimensional array of data. It is a collection of elements organized into rows and columns, where each element can be accessed by specifying its row and column index.

2. How do I declare a matrix in assembly?

To declare a matrix in assembly, you first need to reserve memory space for it. This can be done using the ALLOC directive, which allocates a specified number of bytes in the memory. Then, you can use the DB (define byte) or DD (define double word) directives to initialize the elements of the matrix.

3. How do I access elements of a matrix in assembly?

To access elements of a matrix in assembly, you need to use the MOV (move) instruction. The syntax for accessing an element in a matrix is MOV destination, [base + index*scale], where base is the starting address of the matrix, index is the index of the element, and scale is the size of each element in bytes.

4. Can I perform mathematical operations on matrices in assembly?

Yes, you can perform mathematical operations on matrices in assembly by using the appropriate instructions. For example, to add two matrices, you can use the ADD instruction to add each corresponding element and store the result in a new matrix.

5. How do I print a matrix in assembly?

To print a matrix in assembly, you can use the LEA (load effective address) instruction to load the address of the first element of the matrix into a register, and then use the MOV instruction to move each element into a register that is used for displaying the value. You can then use the appropriate system call to output the values to the screen.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
7
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top