News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

need help on matrix multplication in masm

Started by waqar ahmad, February 15, 2013, 08:06:07 AM

Previous topic - Next topic

RuiLoureiro

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 ?

waqar ahmad

I need assembly code of matrix multiplication.  not a calculator to multiply matrix

RuiLoureiro

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 ;)

waqar ahmad


RuiLoureiro


hutch--

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.

MichaelW

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.

Well Microsoft, here's another nice mess you've gotten us into.

Gunther

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
You have to know the facts before you can distort them.

MichaelW

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

Well Microsoft, here's another nice mess you've gotten us into.

Stan

@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!