Hi,

I want to know how to use inline assembler instruction, and wonder what is the meaning of "memory" in clobbered register list. According to the manual of GCC, the "memory" will cause GCC to not keep memory values cached in registers across the assembler instruction and not optimize stores or loads to that memory. IMO, that means the "memory" notifies GCC the assembler instruction meant to modify memory locations; If variables are cached into registers before the assembler instruction, then these varialbes should be written back to relevant memory locations before the assembler instruction, and read when being used again after the assembler instruction. On the other hand, the "memory" affected inputs and outputs of the assembler instruction only. Then the volatile keyword play its role, if the memory affected is not listed in the inputs and outputs of the assembler instruction. So, I think the "memory" and the volatile keyword should be used to implement a barried. I write the following C code to test the effect of the twos:

        /* asm.c */
        int foobar() {
          int i, t, sum = 0;

          for (i = 0; i <= 10; i++) {
            t = 1 << i;
            sum += t;
          }
          asm volatile ("nop" : : : "memory");
          return sum;
        }
        ... ...

I compiled the asm.c using gcc 4.4.7:
        gcc -S -O1 asm.c

Then, the asm.s is generated as follows:

        foobar:
            pushl   %ebp
            movl    %esp, %ebp
            pushl   %ebx
            movl    $0, %eax
            movl    $0, %ecx
            movl    $1, %edx
        .L2:
            movl    %edx, %ebx
            sall    %cl, %ebx
            addl    %ebx, %eax
            addl    $1, %ecx
            cmpl    $11, %ecx
            jne     .L2
        #APP
        # 10 "hello.c" 1
            nop
        # 0 "" 2
        #NO_APP
            negl    %eax
            popl    %ebx
            popl    %ebp
            ret
        ... ...

It is obvious that every variables are cached into register, and even though the 'sum' is used after the barrier, it does not written back to its memory location and read again. I think my idea about the "memory" and the volatile keyword must be misunderstanding. Any suggestion?

Reply via email to