syntax - Acessing struct fields within an assembly X64 function -


is possible access directly struct fields within assembly function? , how can access via assembly global variable?

in inline assembly on intel syntax can 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 in assembly x64 function (not inline), att syntax (gcc), if it's not possible how do in inline function?

for many similar problems, easiest solution write example in c want, use gcc -m64 -s ... generate assembler source, , use source template own assembly code.

consider following example:

#include <stdio.h>  typedef struct {     int a;     int b; } s;  int foo(const s *s) {     int c = s->a + s->b;      return c; }  int main(void) {     s s = { 2, 2 };      printf("foo(%d, %d) = %d\n", s.a, s.b, foo(&s));      return 0; } 

if generate asm using gcc -wall -o1 -m64 -s foo.c -o foo.s following "foo" function:

.globl _foo _foo: lfb3:     pushq   %rbp lcfi0:     movq    %rsp, %rbp lcfi1:     movl    (%rdi), %eax     addl    4(%rdi), %eax     leave     ret 

as can see, movl (%rdi), %eax gets element of struct, , addl 4(%rdi), %eax adds element b, , function result returned in %eax.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -