Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matrix in assembly mxn dimension

  1. Dec 24, 2011 #1
    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???
     
  2. jcsd
  3. Dec 24, 2011 #2

    chiro

    User Avatar
    Science Advisor

    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).
     
  4. Dec 24, 2011 #3
    I understand your explanation, but I don't know how to write the algorithm, could you help me?
     
  5. Dec 24, 2011 #4

    chiro

    User Avatar
    Science Advisor

    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?
     
  6. Dec 25, 2011 #5

    rcgldr

    User Avatar
    Homework Helper

    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: Dec 25, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matrix in assembly mxn dimension
  1. Help in assembly (Replies: 6)

  2. C++ to Assembly (Replies: 4)

  3. Assembly help (Replies: 2)

  4. Assembly language (Replies: 5)

  5. Assembly laguage (Replies: 5)

Loading...