Archived

This topic is now archived and is closed to further replies.

lucinpub

Simple assembly question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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, ecx
L0:
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;
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites