Re: Local variables used inside an asm block are not recognized as used

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well, as written, gcc discards the whole asm statement as unneeded when using any optimization since none of the outputs (ie ret) get used and the statement is not volatile.

I believe that adding "m" (hello) as an input will resolve your other problem. Note that you may not easily see the string in the asm output since gcc may encode this using something like this:

   movabsq $8022916924116329800, %rax
    movq    %rax, 32(%rsp)
    movl    $10, %eax
    movw    %ax, 44(%rsp)

Also, I'd probably write this statement as something more like this (untested):

    asm volatile
    (
        "syscall"
        : "=a" (ret)
        : "0" (1), "S" (hello), "d" (hello_size), "D" (1), "m" (hello)
        : "rcx", "r11", "memory", "cc"
    );

This lets gcc do as much of the work as possible, which generally produces better code. And are you sure rcx and r11 get clobbered? Seems odd.

dw

On 10/5/2014 11:13 AM, Daniel Kamil Kozar wrote:
#include <unistd.h>

int main(void)
{
     const char hello[] = "Hello World!\n";
     const size_t hello_size = sizeof(hello);
     ssize_t ret;
     asm
     (
         "movl $1, %%eax\n\t"
         "movl $1, %%edi\n\t"
         "movq %1, %%rsi\n\t"
         "movl %2, %%edx\n\t"
         "syscall"
         : "=a"(ret)
         : "g"(hello), "g"(hello_size)
         : "%rdi", "%rsi", "%rdx", "%rcx", "%r11"
     );
     return 0;
}

Unfortunately, this snippet does not work anymore with gcc 4.9.1. An
inspection of gcc's result when run with -S shows that the "hello"
variable is not even created. Adding "static" to the variable's
declaration fixes the issue, however I'm still wondering what's wrong
with the original code and why gcc does not seem to see that the local
variable is actually used by the asm block.

Thanks,
-dkk





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux