Matrix in assembly mxn dimension

AI Thread 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top