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:
and here is my translation. but the system crashes always by executing the code. :confused:
so if someone has an idea please let me know.
thanks
[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
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
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
@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.:(
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.:(
Which procedure is failling?
Are you looking to optimize the code?
;memfile->pos
mov , eax --> mov .MEMFILE.pos, eax
Are you looking to optimize the code?
;memfile->pos
mov , eax --> mov .MEMFILE.pos, eax
@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
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
Is memcpy fixes stack after execution ?If not ,
push sizeBuffer
push eax
push lpBuffer
call memcpy
[B]add esp,4*3[/B]
@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.
but after fixing this the program crashes again at the same point.
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.
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.
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.
@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.
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.
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
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
@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
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
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.
Also sometime i find that what should work don't becase it needed to be INVOKED or something.
@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)
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)
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
does in c++:
x += y --> x = x + y
x = ++y --> x = y + 1
x = ++y --> x = y + 1
wrong, it means
y = y + 1;
x = y;
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
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
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
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
ok thanks.
i will test it. maybe this was the problem.
i will test it. maybe this was the problem.
remember that if you're adding to pointers, C adds elementsize * value,
not "value amount of bytes" as assembly does.
not "value amount of bytes" as assembly does.
big thanks to all your replays!
it now works:)
it now works:)