I am a newcomer to assembler (know some basic instructions, and registers but I am not very used with it)

However I know C programming in Windows API well, and I'm trying to port some code

How will this code be in assembler?

void DispName(WIN32_FIND_DATA * fd)
{
MessageBox(NULL, fd->cFileName, NULL, MB_OK);
}

Like this?

DispName proc fd:DWORD

local s:DWORD

mov eax, addr (WIN32_FIND_DATA PTR ).cFileName

mov s, eax

invoke MessageBox, NULL, addr s, NULL, 0

DispName endp
Posted on 2002-01-29 15:18:09 by jazzy_
Off hand I just see two things.
1 you can't use addr outside of an invoke statement, Instead use offset or lea;
lea eax,.WIN32_FIND_DATA.cFileName

Note that .WIN32_FIND_DATA.cFileName and (WIN32_FIND_DATA PTR ).cFileName are more or less the same thing, its just personal preference.

2, there is no real need to move eax into s, but if you do just shouldn't also use addr with s in the invoke statement.

In fact you can compress everything into one statement:
invoke MessageBox, NULL, addr (WIN32_FIND_DATA PTR ).cFileName,NULL,0
Posted on 2002-01-29 16:17:26 by Eóin
I'm afraid that (WIN32_FIND_DATA PTR ).cFileName just ain't gonna work like you expect. What that does is tell MASM that fd names a WIN32_FIND_DATA structure, and not a pointer. The result is that you will address some data near the arguments on the stack, not the pointed-to data.

If fd was a register, then the register would be treated as a pointer. So you need to load the contents of fd into a register to do what you want.

Reason: the x86 does not do data indirection via memory (in one instruction).
Posted on 2002-01-29 20:58:16 by tank
Actualy you will find that:



INVOKE MessageBox ,NULL,ADDR (WIN32_FIND_DATA PTR [fd]).cFileName,NULL,MB_OK


Will work because if you look at a disassmbly, masm will of built something like this:



PUSH 0
PUSH 0
LEA eax,[ebp + 18]
PUSH eax
PUSH 0
CALL MessageBoxA



This also can corrupt EAX so its something to be watched out for.
Posted on 2002-01-30 00:20:06 by huh
No, it won't really work unless the argument is the entire WIN32_FIND_DATA structure, instead of the pointer that is declared.
Posted on 2002-01-30 12:54:22 by tank
If the fd were a register it'd work, but you cannot use the contents of a memory location as a reference to some other memory location.


mov eax, fd
lea eax, (WIN32_FIND_DATA PTR [eax]).cFileName

invoke MessageBox, NULL, eax, NULL, 0


Mirno
Posted on 2002-01-30 13:40:53 by Mirno
Thank you were much, both these work


mov eax,
invoke MessageBox, NULL, addr , NULL, MB_OK

mov eax,
invoke MessageBox, NULL, addr (WIN32_FIND_DATA PTR ).cFileName, NULL, MB_OK
Posted on 2002-01-30 15:45:52 by jazzy_
I'm sorry, my bad :(. I was thinking of fd as a structre declared in the data section. I forgot that its address was passed to the proc. Thanks for spotting that tank.

And jazzy_ , I'm glad to see you got it working. :)
Posted on 2002-01-30 18:46:12 by Eóin