Ok, I am a total newbie. Read many posts, tuts, Randys' HLA (like MASM32 better) but I cannot for the life of me figure out how to create a "function" that takes parameters, does some stuff and then returns a something to the caller. A dll to be more specific. For starters I am trying to play with a dll so I can try it in VB. (I know VB, yuck but that is why I am learning MASM) Like I want to do in VB:

Private Declare Function MyRGB lib "MyDLL" (byref lRed as long, byref lGreen as long, byref lBlue as long) as long
ByRef cause masm needs a pointer to a mem location? I dunno, I am well versed in VB and self taught but this assembly is kicking my butt.

and I want a function in a asm dll called MyRGB to take the 3 values and return the 32bit RGB number.

I took the TstDll example and modified it with

MyRGB proto lRed:DWORD, lGreen:DWORD, lBlue:DWORD

MyRGB endp

Is this how I would start?

Please steer me in the right direction.

Thanks.
Rob
Posted on 2002-06-13 01:21:35 by Gunner
Im uncertain of what you have seen in terms of assembly examples, but if i get you correctly you want the basic framework of a function that returns a value...

So, in this light, here is the basic frame and why..

First,
RGB_Fucntion PROTO :DWORD, :DWORD, :DWORD

This can be places at the beginning of your source, it tells masm the # of params to expect when calling your function, and what type of params they will be; in this case, Red, Green, Blue, as DWORDS each..

Second,
RGB_Function PROC Red:DWORD, Green:DWORD, Blue:DWORD

Somewhere in the .code segment, have this to indicate the entry point of your function, and the NAMES of the params to use as variables.. (they are really stack variables, in the stack segment).

Third, the code:
xor eax, eax,
mov edx, Blue
mov al, dl
shl eax, 8

mov edx, Green
mov al, dl
shl eax, 8

mov edx, Red
mov al, dl
;note EDX is corrupted by this function, and EAX == 00BBGGRR


This is the code part of the function, in the code segment. Its not the most efficient, but it gets the idea across...

Fourth,
ret
RGB_Function ENDP


This tells the assembler the funciton has ended. It returns from the function to the calling poceedure, with EDX changed (last holding the RED value), and EAX having the desired information. THe ENDP means "end Proc"...

Last thought, the standard amunst windows, and i assume VB alike, is that the RETURN value (weather its a pointer, or actualy data) is always found in EAX.

DLL's are more complicated, but if VB were to call a function in one, i would expect, that it wants the returned info in 'EAX' as well...

Hope it helps..
:alright:
NaN
Posted on 2002-06-13 02:20:55 by NaN
For starters I am trying to play with a dll so I can try it in VB.
You have come to the right place :)

(I know VB, yuck but that is why I am learning MASM)
VB is as powerful as the programmer using it. The only thing it cannot be used for is drivers and Ring 0 code.

ByRef cause masm needs a pointer to a mem location?
No, in this case you need to pass them ByVal, otherwise you are adding a layer of indirection that you do not need. A basic rule that covers pretty much all programming languages: you would usually only pass variables by reference (ByRef) when the called function intends to change the value of that variable.
Posted on 2002-06-13 06:03:22 by sluggy

You have come to the right place :)

VB is as powerful as the programmer using it. The only thing it cannot be used for is drivers and Ring 0 code.


True, True indeed. But all the COM overhead for simple things, all the support files needed for simple things, and with each OS MS changes this and that and things go wrong... I have big goals and in the future I would love to convert my two programs to assembly.

Let me say YIPEEEE! I have gotten it to work. I am persistant, so I kept at it and I finally gotten it to work.

GetRGB proc RedColor:BYTE, GreenColor:BYTE, BlueColor:BYTE
xor eax, eax
mov ah, BlueColor
shl eax, 8
mov ah, GreenColor
mov al, RedColor

ret
GetRGB endp

The code was from a macro I seen somewhere. I think I have an understanding of what it does. I now have a starting point. I will play and learn.

I am sure I will be back here again ;)

~Rob
Posted on 2002-06-13 09:14:48 by Gunner
GetRGB proc RedColor:BYTE, GreenColor:BYTE, BlueColor:BYTE
You need to change the 'BYTE" declarations to 'DWORD', because that is what you will be receiving from your VB app. You can then discard the top three bytes (i.e. ignore them) and just operate on the lowest order byte. Or, you could be really good, and check that the values you receive are between 0 and 255 inclusive before doing anything :)
Posted on 2002-06-13 19:06:56 by sluggy

You need to change the 'BYTE" declarations to 'DWORD', because that is what you will be receiving from your VB app. You can then discard the top three bytes (i.e. ignore them) and just operate on the lowest order byte. Or, you could be really good, and check that the values you receive are between 0 and 255 inclusive before doing anything :)


Well, since the return is in eax my VB declaration is:
Private Declare Function GetRGB Lib "TestASM.dll" ( _
ByVal lRed As Byte, _
ByVal lGreen As Byte, _
ByVal lBlue As Byte _
) As Long

and Debug.Print GetRGB(125, 210, 94) gives the correct value so I must be doing something right. I will add some kind of check in the GetRGB function to make sure the values are within range, it this what you are talking about? Any way, thanks sluggy. Posting a new question over in Algos.

Thanks
Posted on 2002-06-13 19:53:51 by Gunner