Simple assembly question

Started by
3 comments, last by lucinpub 20 years, 11 months ago
hey, I am trying to build an assembly funtion that passes an address of an int array declared to a c functoin. Some times I need to send off the base address, but other times I might want to send off the address of another cell. The c code then loops through portions of the array. As I am a rank amateur in asm, I am having a difficult time. something like this mov edi, dword ptr[ edi + 16 ] indexes into the array, correct? now what if I want the memory address of that cell so that the function it gets passed to could, for example, loop through the next 6 cells and sum or something.
Lucas Henekswww.ionforge.com
Advertisement
in VC++, you can use C variables in your assembler something like this. I''m not sure how this works in other compilers.

#define SOME_SIZE 16

void asmfunction(int *pointer){	__asm	{		// notice using the C variable "pointer" in asm		mov esi, pointer		// read from the pointer		mov eax, dword ptr[esi]		// read from the next cell		mov eax, dword ptr[esi+4]		// or in a loop		xor ecx, ecxL0:		mov dword ptr[esi+ecx*4], ecx		inc ecx		cmp ecx, SOME_SIZE		jl L0	}}int main(){	int array[SOME_SIZE];	asmfunction(array);	return 0;} 
You''re on the right track. If you don''t know already EAX is used for return values, so whatever is in EAX when your function returns is what the return value is.

Also, remember when doing things like [edi + 16] that 16 is bytes from the start, not items in array. If it is a char array, edi+16 will reference the 16th item. If its a short array it will reference the 8th item, and if its an int array it references the 4th item.

Hope that was of some help
thanks for the input guys, but i figured it out. All i had to do for what I was looking to do was
add edi 16
now I feel silly for how easy that was.
Lucas Henekswww.ionforge.com
If you want a more general solution than add, use

lea edi, dword ptr[edi+16]

This way, you can also do something like

lea edi, dword ptr[edi+4*ecx+16]

in a single clock cycle.

lea will load the address of its operand into a register.

This topic is closed to new replies.

Advertisement