Matrix in assembly mxn dimension

Click For Summary
The discussion revolves around modifying an existing assembly code for a 3x3 matrix to handle matrices of arbitrary dimensions (m x n). The original code successfully prints elements of a 3x3 matrix in a spiral order but is hard-coded for this specific size. Suggestions for improvement include changing the hard-coded values for the number of columns and rows to variables stored in memory, allowing for dynamic adjustment. It is recommended to replace the fixed column size in the code with a variable (e.g., using `mov ch, dx` where `dx` holds the column size) and to update comparison checks accordingly. Additionally, the use of LOOP commands is advised to streamline the code and reduce redundancy in printing numbers. The conversation also touches on the code's compatibility with 8086 architecture and the potential for optimization by consolidating loop structures. Overall, the focus is on generalizing the code to accommodate various matrix sizes while improving efficiency.
zemzi
Messages
2
Reaction score
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
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).
 
I understand your explanation, but I don't know how to write the algorithm, could you help me?
 
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?
 
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:
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
Replies
7
Views
8K
Replies
20
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 7 ·
Replies
7
Views
7K