Gnu assembler question for ARM

Nick Clifton nickc@redhat.com
Fri May 4 11:02:00 GMT 2007


Hi Andrea,

> Let's assume my function gets a pointer to a "C"-struct and wants to use its fields. Currently my code references the fields by hard-coding the offsets in the code, which makes the code not very readable and error prone. What is the best way to use records in assembler? I don't want to define them or allocate them in assembler, just access their fields when I receive a reference to them.

Why not let gcc do this for you by using its support for extended inline 
assembler ?  eg:

   % cat struct.c
   struct a {
     int field1;
     char field2;
     int field3;
   } A;

   int main (void)
   {
     int result;
     asm ("mov %[return_value], %[the_field_I_want]"
          : [return_value] "=r" (result)
          : [the_field_I_want] "r" (A.field3));
     return result;
   }

   % gcc -S -O2 struct.c
   % cat struct.s
   [...]
         .type   main, %function
   main:
         ldr     r3, .L3
         ldr     r0, [r3, #8]
         mov r0, r0
         bx      lr
         .align  2
   .L3:
         .word   A

         .comm   A,12,4
   [...]

ie the compiler has worked out for you that "field3" of the "struct a" 
structure is at an 8 byte offset from the start of the structure.

Cheers
   Nick



More information about the Binutils mailing list