Is it possible to access directly struct fields within an assembly function? And how can I access via assembly an global variable?
In inline assembly on intel syntax I can do this:
struct str
{
int a;
int b;
}
int someGlobalVar;
__declspec(naked) void __fastcall func(str * r)
{
__asm
{
mov dword ptr [ecx].a, 2
mov dword ptr [ecx].b,someGlobalVar
}
}
How do I do this in a assembly x64 function (not inline), with ATT syntax (gcc), if it's not possible how do I do this in an inline function?
For this any many similar problems, the easiest solution is to write an example in C that does what you want, then use
gcc -m64 -S ...
to generate assembler source, and then use that source as a template for your own assembly code.Consider the following example:
If we generate asm using
gcc -Wall -O1 -m64 -S foo.c -o foo.S
we get the following for the "foo" function:As you can see,
movl (%rdi), %eax
gets the element a of the struct, and thenaddl 4(%rdi), %eax
adds element b, and the function result is returned in%eax
.