Matrix in assembly mxn dimension

Click For Summary

Discussion Overview

The discussion revolves around modifying an assembly code written for a 3x3 matrix to work with matrices of arbitrary dimensions (m x n). Participants explore the challenges of adapting the code, including the need to generalize certain hard-coded values and improve the algorithm for printing matrix elements in a spiral order.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests changing the hard-coded column size in the code from "mov ch,3" to a variable that holds the actual column size (e.g., "mov ch,dx").
  • Another participant emphasizes the need to modify comparison statements to reflect the new dimensions of the matrix, indicating that "cmp cl, 7" should be changed to "cmp cl,dx".
  • Some participants note that the existing code contains many hard-coded values and recommend storing the number of rows and columns in memory instead of using registers.
  • There are suggestions to utilize the LOOP command to streamline the code and reduce redundancy in printing numbers.
  • One participant expresses uncertainty about writing the algorithm for the generalized version of the code and seeks further assistance.
  • Another participant questions the platform requirements for the code, noting that it appears to be written for 8086 architecture and inquires about the necessity of specific hardware capabilities.
  • Further discussion includes the suggestion to optimize the code by having a single branch point for both inner and outer loops, with a focus on advancing the BX register based on the number of columns and rows.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the existing code to accommodate arbitrary matrix dimensions, but there is no consensus on the specific approach or algorithm to achieve this. Multiple competing views on optimization and implementation details remain present.

Contextual Notes

Participants highlight limitations in the current code, including hard-coded values and the need for a more flexible algorithm. There is also mention of the segmented memory model and its implications for compatibility with different hardware platforms.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
6K
  • · 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