The MASM Forum

General => The Campus => Topic started by: waqar ahmad on February 15, 2013, 08:06:07 AM

Title: need help on matrix multplication in masm
Post by: waqar ahmad on February 15, 2013, 08:06:07 AM
Hello everyone,
                        i need masm program of matrix chain multiplication or matrix multiplication. Please help me
            Thanks
Title: Re: need help on matrix multplication in masm
Post by: qWord on February 15, 2013, 08:42:29 AM
you may shows us what you already have done?
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 15, 2013, 08:49:19 AM
I only done research. it is more confusing me. i have c language code which is very simple. but i don't know how to implement two dimensional array in masm.
Title: Re: need help on matrix multplication in masm
Post by: jj2007 on February 15, 2013, 09:32:42 AM
To get you started:
A two-dimensional array is an area of memory to which you have a pointer, e.g.

.data
MyArray DWORD 10, 11, 12, 13, 20, 21, 22, 23
R1C1  dd 0

You would load the start address into a register:
.code
  mov esi, offset MyArray

To get row 1, column 2, you would need to calculate the offset:
  cols=4
  mov eax, [esi+1*cols*DWORD+2*DWORD]
  mov R1C2, eax
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 15, 2013, 09:38:10 AM
Would you Please Explain This Line:

    mov eax, [esi+1*cols*DWORD+2*DWORD]
Title: Re: need help on matrix multplication in masm
Post by: jj2007 on February 15, 2013, 09:44:58 AM
mov eax, [esi+1*cols*DWORD+2*DWORD]

esi is the starting point to the memory block that contains your data
The example has 4 columns, and DWORD size, i.e. 4 bytes per item.
To load the dword in row1, column 2 (zero-based!), your offset is
  1*4*4+2*4
In this example, immediate values are used. You can also use e.g.
  mov ecx, 1  ; desired row
  mov edx, 2  ; desired column
  mov eax, 4  ; number of columns
  imul eax, ecx, DWORD  ; 4*ecx*4=16
  mov eax, [esi+eax+4*edx]
same as
  mov eax, [esi+4*4+4*2]
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 15, 2013, 09:49:55 AM
ok,  i have a link which has same functionality that you are applying would you please see this :
and explain it to me

http://cs.smith.edu/~thiebaut/ArtOfAssembly/CH05/CH05-2.html
Title: Re: need help on matrix multplication in masm
Post by: Gunther on February 16, 2013, 02:06:49 AM
Hi waqar ahmad,

first things first: welcome to the forum.

To  your link. It's a question what you would prefer: row major order like in C (first row, second row, ..., last row), or column major order like in FORTRAN, Pascal, BASIC (first column, second column, ..., last column). Furthermore, your pointed site is 16 bit code. But you should read the theoretical background and migrate the source to 32 bit code.

Gunther
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 03:03:18 AM
thanks,   I am doing doing matrix chain multiplication,   i am trying to convert c code into assembly. I am working on MASM in 16 bit..
Title: Re: need help on matrix multplication in masm
Post by: RuiLoureiro on February 16, 2013, 05:01:02 AM
Quote from: waqar ahmad on February 16, 2013, 03:03:18 AM
...        I am working on MASM in 16 bit..

:biggrin:
Hi waqar ahmad,
                             16 bit is obsolete !
It is hard to code it !
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 05:10:45 AM
I have to do this in 16 bit
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 05:14:03 AM
I am not much Familiar with 32 bit but if matrix chain multiplication is available in 32 bit please tell me..
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 05:22:54 AM
Anyone who can convert this line into assembly MASM:

work=c[j][k]+c[k+1][j+i]+p[j-1]*p[k]*p[j+i];
Title: Re: need help on matrix multplication in masm
Post by: RuiLoureiro on February 16, 2013, 05:42:18 AM
Quote from: waqar ahmad on February 16, 2013, 05:14:03 AM
I am not much Familiar with 32 bit but if matrix chain multiplication is available in 32 bit please tell me..
Hi  waqar ahmad,
               1.  with 32 bit it is much easy.
               2.  could you explain to me what you want to say with
                    «matrix chain multiplication» ? What exatly .
               3. you can search this forum for «The calculator or go to page 18 now». It
                   can do some matrix multiplication etc.
Have a good work !  :biggrin:
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 05:51:38 AM
Matrix chain Multiplication:  multiplying more than 2 matrix in assembly
Title: Re: need help on matrix multplication in masm
Post by: RuiLoureiro on February 16, 2013, 06:04:13 AM
Quote from: waqar ahmad on February 16, 2013, 05:51:38 AM
Matrix chain Multiplication:  multiplying more than 2 matrix in assembly

Ok ! Try to search «The calculator» ? It does what you want ?
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 06:21:18 AM
I need assembly code of matrix multiplication.  not a calculator to multiply matrix
Title: Re: need help on matrix multplication in masm
Post by: RuiLoureiro on February 16, 2013, 06:27:29 AM
Quote from: waqar ahmad on February 16, 2013, 06:21:18 AM
I need assembly code of matrix multiplication.  not a calculator to multiply matrix
Yes the calculator was done in Assembly by magic waqar ahmad ;)
Title: Re: need help on matrix multplication in masm
Post by: waqar ahmad on February 16, 2013, 06:30:21 AM
But where is the code of that calculator?
Title: Re: need help on matrix multplication in masm
Post by: RuiLoureiro on February 16, 2013, 06:31:47 AM
Quote from: waqar ahmad on February 16, 2013, 06:30:21 AM
But where is the code of that calculator?
The author has the assembly code !
Title: Re: need help on matrix multplication in masm
Post by: hutch-- on February 16, 2013, 07:38:02 PM
The original question looks and sounds like homework. You have been asked to provide the code that you have developed to try and perform this task but cannot do so and have asked the most basic questions about writing Intel notation assembler code. It would appear that you want someone to do this for you which is not going to get done here. Members will help you to learn assembler coding but they will not do your work for you.
Title: Re: need help on matrix multplication in masm
Post by: MichaelW on February 16, 2013, 09:09:19 PM
Quote from: waqar ahmad on February 16, 2013, 05:10:45 AM
I have to do this in 16 bit

Meaning that you must code it as a 16-bit application, or that you must use only 16-bit registers? A 16-bit application must use 16-bit addressing, but this addressing can be done more easily using 32-bit registers.

Title: Re: need help on matrix multplication in masm
Post by: Gunther on February 16, 2013, 10:10:48 PM
Hi waqar ahmad,

Quote from: hutch-- on February 16, 2013, 07:38:02 PM
The original question looks and sounds like homework. You have been asked to provide the code that you have developed to try and perform this task but cannot do so and have asked the most basic questions about writing Intel notation assembler code. It would appear that you want someone to do this for you which is not going to get done here. Members will help you to learn assembler coding but they will not do your work for you.

That is the central point. Try to write your 16 or 32 bit code (or whatever), post it here and we'll help you to get it working. That's the only way.

Gunther
Title: Re: need help on matrix multplication in masm
Post by: MichaelW on February 18, 2013, 07:04:28 PM
I think the most reasonable starting point would be to learn how to access a 2-dimensional array with MASM. The information on the linked AoA page should make this relatively easy, start at 5.6.2 Multidimensional Arrays and continue through 5.6.2.4 Accessing Multidimensional Array Elements in Assembly Language. I think you should start with a 32-bit console app, because it will be easier to code and debug, and then after you get it working translate it to 16-bit. For ease of use I think you should encapsulate the code in a procedure, one to get an array element, and one to set an array element.

Note that the page shows 16-bit code, and the array elements are 16-bit words. Translating it to 32-bit code can be as simple as substituting 32-bit registers for the 16-bit registers and 32-bit dwords for the array elements, with the complication that to allow for the doubling in the size of the array elements the:

add ax, ax


Should be replaced with:

add eax, eax
add eax, eax


Or better:

shl eax, 2


Or you can reduce the number of instructions required by taking advantage of the additional addressing modes available in 32-bit code, but I'll leave that explanation to someone else, after you demonstrate a willingness to do something beyond "research".

Here is a 32-bit template with some test code (that depends on a working MASM32 installation) where for simplicity I reduced the array from the 8x4 in the AoA example to 4x4.

;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================
.data
    array dd 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
.code
;==============================================================================
;--------------------------------------------------------------------------
;     0   1   2   3
;    ---------------
; 0 | 00  01  02  03
; 1 | 04  05  06  07
; 2 | 08  09  10  11
; 3 | 12  13  14  15
;
; For a row major ordered array:
; Element_Address = Base_Address+(colindex*row_size+rowindex)*Element_Size
;--------------------------------------------------------------------------

;----------------------------------------------------------------------
; The convention for returning a 32-bit integer is to leave it in EAX.
;----------------------------------------------------------------------

Get proc colindex:DWORD, rowindex:DWORD
    ; your code goes here
    ret
Get endp

Set proc colindex:DWORD, rowindex:DWORD, value:DWORD
    ; your code goes here
    ret
Set endp

;==============================================================================
start:
;==============================================================================
    invoke Get, 0, 0
    printf("%d\n", eax)
    invoke Get, 0, 1
    printf("%d\n", eax)
    invoke Get, 0, 2
    printf("%d\n", eax)
    invoke Get, 0, 3
    printf("%d\n", eax)
    invoke Get, 1, 0
    printf("%d\n", eax)
    invoke Get, 1, 1
    printf("%d\n", eax)
    invoke Get, 1, 2
    printf("%d\n", eax)
    invoke Get, 1, 3
    printf("%d\n", eax)
    invoke Get, 2, 0
    printf("%d\n", eax)
    invoke Get, 2, 1
    printf("%d\n", eax)
    invoke Get, 2, 2
    printf("%d\n", eax)
    invoke Get, 2, 3
    printf("%d\n", eax)
    invoke Get, 3, 0
    printf("%d\n", eax)
    invoke Get, 3, 1
    printf("%d\n", eax)
    invoke Get, 3, 2
    printf("%d\n", eax)
    invoke Get, 3, 3
    printf("%d\n\n", eax)

    invoke Set, 0, 0, 0
    invoke Get, 0, 0
    printf("%d\n", eax)
    invoke Set, 0, 1, -1
    invoke Get, 0, 1
    printf("%d\n", eax)
    invoke Set, 0, 2, -2
    invoke Get, 0, 2
    printf("%d\n", eax)
    invoke Set, 0, 2, -3
    invoke Get, 0, 2
    printf("%d\n\n", eax)

    invoke Set, 3, 0, -12
    invoke Get, 3, 0
    printf("%d\n", eax)
    invoke Set, 3, 1, -13
    invoke Get, 3, 1
    printf("%d\n", eax)
    invoke Set, 3, 2, -14
    invoke Get, 3, 2
    printf("%d\n", eax)
    invoke Set, 3, 2, -15
    invoke Get, 3, 2
    printf("%d\n\n", eax)

    inkey
    exit
;==============================================================================
end start

Title: Re: need help on matrix multplication in masm
Post by: Stan on February 20, 2013, 08:41:48 AM
@MichaelW

Good explanation.

Sometimes it's difficult for a student [AND MANY OTHERS] to grasp a concept when the structure is not blatantly apparent.
Showing the multi-dimensional array as a serial chunk -- excellent!