Lets say i have a procedure that works on elements of an array but i want it to work on any array.

How do i use a variable for my_array in:

lea esi,my_array

Any examples appreciated.

I don't understand terms such as VARAG :REQ etc which is probably c convention
Posted on 2001-09-18 16:32:00 by titan
VARARG and REQ are marco argument specifiers. Macro's are like Scripts for MASM. They are programs (with its own type of language) that tell the compiler how to write *more* asm, before the compiler actually converst it all to op-code (machine language, binary code). Instead of manually writing code, sometimes its faster and easier to tell the compiler to use a MACRO and the script it runs will do the dirty work for you.

For you question, write a function to do just that.

mul_3x3_array PROTO :DWORD, :DWORD


my_3x3_array db 0,1,2
db 3,1,3
db 1,0,5

my_3x1_vector db 10, 0, 3


mul_3x3_array PROC uses esi edi lpArray:DWORD, lpVector:DWORD

mov esi, lpArray
mov edi, lpVector

mov row, 0
.while( row < 3 ) ; the 'x' s

mov col, 0
.while( col < 3 ) ; the 'y' s
mov ecx, col

mov dh, [edi+ecx] ; get vector col value
; no offset, used on all rows

xor eax, eax
mov eax, row
mov dl, 3 ; 3 bytes per row
mul dl ; ax = al * dl

add ecx, eax ; add on the row offset

xor eax, eax ; clear eax
mov al, [esi+ecx] ; get row x, col y
mul dh ; ax = al * dh
; = Array[x,y] * Vector[y]
mov [esi+ecx],al ; save al now in the old array location

inc col ; next column

inc row ; next row


mul_3x3_array ENDP


invoke mul_3x3_array, addr my_3x3_array, addr my_3x1_vector

and my_3x3_array will be modified to:

my_3x3_array db 10,0,6
db 30,0,9
db 10,0,15

my_3x1_vector db 10, 0, 3

I didnt test this, but it should work. As well this isnt a proper matrix multiply, as i still havent summed each row to form a 3x1 result. (I didnt want to make it any longer than its needed to prove the point).

The bottom line is pass the proc the address of you array, and write code to do what YOU want to have happen. Here, for each new Column, i recalculate the offset needed based on the current row/column position to handle the array properly.

I hope this gives you some insight...
Posted on 2001-09-18 17:49:12 by NaN
To allow your procedure to work on any array, you shouldn't need to use VARARG (I wont explain VARARG, read the masm help file for a bit of understanding).

If you pass the pointer to the array, which can be done easily using INVOKE, the procedure will be able to read the array using the pointer. It can also directly change the array using the pointer.

invoke afunc ,ADDR my_array

The 'ADDR' automacticly pushes the address of my_array onto the stack for the procedure to read.
Posted on 2001-09-18 20:14:07 by huh
WOW guys ...that was great.

I just made my first proto type.
Talk about flexability with invoke.

NAN i used part of your example to get what i wanted done but i first ran into a couple of problems. If you have time read on.

LEA ESI, lpArray ; this won't work

MOV ESI,lpArray ; this does


also i had to remove hWnd from the procedure declaration in

myproc PROC "hWnd" uses esi edi lpArray:DWORD

I found in the example tutorials that hWnd was found right after PROC

What does it do?
Why did i have to remove it to get the procedure to work?

Thanx a GIG
Understanding the real ability of invoke has made my day.
Posted on 2001-09-19 17:20:17 by titan
The hWnd thing is common, but you got it mis-understood.

most API's have hWnd as the first parameter.. (get use to this idea), but PROC's in general have no rules for this. Its only a microsoft type style. As well you dont "" place it in as you have, as hWnd is just another parameter, so place it first in the parameter list.

lea esi, lpArray

I couldnt tell you off hand... I have a hunch that ESI can't be used with lea?? OR that data segment lables dont work with it...

Perhaps someone else could enlighten us on this... I just work around these nuances when i come up to them... (sorry for the bogus code :) )

Posted on 2001-09-20 01:23:55 by NaN
If lpArray is a parameter, then LEA will point to the parameter. Its name suggests that the parameter is a pointer to the array, not the array itself.
Posted on 2001-09-20 14:28:45 by tank
Thanx tank, I totaly missed the point there!. (and was a bit stumped because of it). I thought MY code wasnt workng because of this. I didnt realize it was a modification he had done...

It all makes sence now.... (shoulda checked my code before i replied) :)

Posted on 2001-09-20 16:21:22 by NaN