im working for a while on translating the test file from the minifmod.lib to asm. the implementation of the extern works fine but i have problems to translate this:



[size=9]
typedef struct
{
int length;
int pos;
void *data;
} MEMFILE;


unsigned int memopen(char *name)
{
MEMFILE *memfile;

memfile = (MEMFILE *)calloc(sizeof(MEMFILE),1);

{
HRSRC rec;
HGLOBAL handle;

rec = FindResource(NULL, name, RT_RCDATA); //(NULL, name, RT_RCDATA);
handle = LoadResource(NULL, rec);

memfile->data = LockResource(handle);
memfile->length = SizeofResource(NULL, rec);
memfile->pos = 0;
}

return (unsigned int)memfile;
}

void memclose(unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;

free(memfile);
}

int memread(void *buffer, int size, unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;

if (memfile->pos + size >= memfile->length)
size = memfile->length - memfile->pos;

memcpy(buffer, (char *)memfile->data+memfile->pos, size);
memfile->pos += size;

return size;
}

void memseek(unsigned int handle, int pos, signed char mode)
{
MEMFILE *memfile = (MEMFILE *)handle;

if (mode == SEEK_SET)
memfile->pos = pos;
else if (mode == SEEK_CUR)
memfile->pos += pos;
else if (mode == SEEK_END)
memfile->pos = memfile->length + pos;

if (memfile->pos > memfile->length)
memfile->pos = memfile->length;
}

int memtell(unsigned int handle)
{
MEMFILE *memfile = (MEMFILE *)handle;

return memfile->pos;
}
[/SIZE]


and here is my translation. but the system crashes always by executing the code. :confused:



[size=9]
MEMFILE struct
mf_length dd ?
mf_pos dd ?
mf_data dd ?
MEMFILE ends

memopen PROC C lpName:DWORD
LOCAL rec:HRSRC
LOCAL handle:HGLOBAL
LOCAL memfile:DWORD

push 1
push sizeof MEMFILE
call calloc
mov memfile, eax

push RT_RCDATA
push 101 ;lpName
push NULL
call FindResource
mov rec, eax

push rec
push NULL
call LoadResource
mov handle, eax

push handle
call LockResource
mov ecx, memfile
mov [ecx+8], eax ;memfile->data

push rec
push NULL
call SizeofResource
mov ecx, memfile
mov [ecx], eax ;memfile->length

xor eax, eax
mov [ecx+4], eax ;memfile->pos

mov eax, memfile ;RETURN
ret
memopen ENDP

memclose PROC C handle:DWORD
LOCAL memfile:DWORD

mov eax, handle
mov memfile, eax
push memfile
call free
ret
memclose ENDP

memread PROC C lpBuffer:DWORD, sizeBuffer:DWORD, handle:DWORD
LOCAL memfile:DWORD

mov eax, handle
mov memfile, eax
mov ecx, memfile

mov eax, [ecx+4] ;IF (memfile->pos + size >= memfile->length)
add eax, sizeBuffer
.IF eax >= [ecx] ;THEN size = memfile->length - memfile->pos

mov eax, [ecx]
sub eax, [ecx+4]
mov sizeBuffer, eax

.ENDIF

mov eax, [ecx+8] ;memcpy(buffer, (char *)memfile->data+memfile->pos, size)
add eax, [ecx+4]

push sizeBuffer
push eax
push lpBuffer
call memcpy

mov ecx, memfile
mov eax, sizeBuffer ;memfile->pos += size
inc eax ;RETURN
mov [ecx+4], eax

ret
memread ENDP

memseek PROC C handle:DWORD, pos:DWORD, mode:BYTE
LOCAL memfile:DWORD

mov eax, handle
mov memfile, eax
mov ecx, memfile

.IF mode == SEEK_SET ;memfile->pos = pos
mov eax, pos
mov [ecx+4], eax
.ELSEIF mode == SEEK_CUR ;memfile->pos += pos
mov eax, pos
inc eax
mov [ecx+4], eax
.ELSEIF mode == SEEK_END ;memfile->pos = memfile->length + pos
mov eax, pos
add eax, [ecx]
mov [ecx+4],eax
.ENDIF

mov eax, [ecx+4] ;IF (memfile->pos > memfile->length)
.IF eax > [ecx] ;THEN memfile->pos = memfile->length
mov eax, [ecx]
mov [ecx+4], eax
.ENDIF

ret
memseek ENDP

memtell PROC C handle:DWORD
LOCAL memfile:DWORD

mov eax, handle
mov memfile, eax
mov ecx, memfile

mov eax, [ecx+4] ;RETURN

ret
memtell ENDP
[/SIZE]


so if someone has an idea please let me know.
thanks
Posted on 2002-03-31 04:51:32 by adapix
With only a quick look...

push RT_RCDATA
push 101 ;lpName
push NULL
call FindResource
mov rec, eax


Is not lpName a pointer to a string? NOT the
string. Try:

push lpName
Posted on 2002-03-31 05:16:39 by bdjames
@bdjames
lpName is a pointer but i use here direct the resource ID and loading the resource looks good debugging the code.
i tested it also with lpName but the the system crashes the same way.:(
Posted on 2002-03-31 05:35:18 by adapix
Which procedure is failling?

Are you looking to optimize the code?



;memfile->pos
mov , eax --> mov .MEMFILE.pos, eax
Posted on 2002-03-31 06:35:57 by bdjames
@bdjames
thanks for the better notification.
mov .MEMFILE.pos, eax looks better as my code.

i set an int 3 infront of all procs and the 10 time the memread proc called the system crashes after return to the calling procedure.

yes i want to optimized the code but first the whole thing must run before optimizing.

thanks for your help
Posted on 2002-03-31 08:07:59 by adapix
Is memcpy fixes stack after execution ?If not ,


push sizeBuffer
push eax
push lpBuffer
call memcpy
[B]add esp,4*3[/B]
Posted on 2002-03-31 10:52:08 by LaptoniC
@LaptoniC
i checked all the calls and you are right the stack after the call to the msvcrt.dll was not fixed but returning to the main routine it is fixed.
maybe this problem is caused by declaring the externels.



extrn memcpy :PROC
extrn calloc :PROC
extrn free :PROC


but after fixing this the program crashes again at the same point.
Posted on 2002-03-31 14:16:52 by adapix
LaptoniC has got it. If any of the functions are
standard C, then you have to balence the stack
frame for each one:

push sizeBuffer
push eax
push lpBuffer
call memcpy
add esp,4*3

push 1
push sizeof MEMFILE
call calloc
add esp, 4*3

push memfile
call free
add esp, 4

The extrns look ok, but take a look at extrndef.

You might want to try the WinAPI memory functions:
CopyMemory,
HeapAlloc,
HeapFree,
to test your code.
Posted on 2002-03-31 17:01:26 by bdjames
@bdjames
yes i fixed all the calls. but it crashes at the same point.
using the winApi memory functions was my first try on this but it didnt work for me. so i decided to use the c procedures.
Posted on 2002-03-31 17:30:27 by adapix
Should this:

mov ecx, memfile
mov eax, sizeBuffer ;memfile->pos += size
inc eax ;RETURN
mov , eax

be:

mov ecx, memfile
mov eax, sizeBuffer ;memfile->pos += size
add , eax

and

.ELSEIF mode == SEEK_CUR ;memfile->pos += pos
mov eax, pos
inc eax
mov , eax

to

.ELSEIF mode == SEEK_CUR ;memfile->pos += pos
mov eax, pos
add , eax
Posted on 2002-03-31 18:01:00 by bdjames
@bdjames
no i think thats ok. cause a += b means:
increment b
and set a = b

but im not sure. i have no c books here to look if this ok. maybe im wrong and it has to look like this:
set a = b
increment b
Posted on 2002-04-01 01:51:00 by adapix
M32.lib got a mem copy funtion...Mybe it might make things work.

Also sometime i find that what should work don't becase it needed to be INVOKED or something.
Posted on 2002-04-01 02:58:58 by cmax
@cmax
yes i know about the mem copy procedure but first i want to translate this example so i know that it works.
after that i can play a bit with the whole lib. (maybe i translate everything to asm)
Posted on 2002-04-01 06:38:21 by adapix
I double checked and it means the same in c as it
does in c++:

x += y --> x = x + y
x = ++y --> x = y + 1
Posted on 2002-04-01 15:37:38 by bdjames

x = ++y --> x = y + 1

wrong, it means


y = y + 1;
x = y;
Posted on 2002-04-01 21:58:17 by f0dder
so this seems to be ok.

a += b


b = b + 1;
a = a;


and so my instruction here is also ok.

memfile->pos += size


mov eax, sizeBuffer
inc eax
mov , eax
Posted on 2002-04-02 11:01:38 by adapix
Nope. f0dder showed how to translate the "prefix" operator ++ when it's used within an expression. It's not the same as +=.

a += b is equivalent to a = a + b.

and

memfile->pos += size is equivalent to
temp_ptr = &(memfile->pos); *temp_ptr += size

Translation for a += b:

mov eax,b
add a,eax

Translation for memfile->pos += size:

mov eax,memfile ; get pointer to memfile struct
mov ecx,size ; get value to add
add ,ecx ; add value to int field
Posted on 2002-04-02 12:54:07 by tenkey
ok thanks.
i will test it. maybe this was the problem.
Posted on 2002-04-02 13:42:21 by adapix
remember that if you're adding to pointers, C adds elementsize * value,
not "value amount of bytes" as assembly does.
Posted on 2002-04-02 14:03:26 by f0dder
big thanks to all your replays!

it now works:)
Posted on 2002-04-03 10:16:38 by adapix