Hacker News new | ask | show | jobs
by db999999 4310 days ago
Try:

  #include <string.h>

  void bar(void *s, size_t count)
  {
        memset(s, 0, count);
        __asm__ ("" : "=r" (s) : "0" (s));
  }

  int main(void)
  {
        char foo[128];
        bar(foo, sizeof(foo));
        return 0;
  }

  gcc -O2 -o foo foo.c -g
  gdb ./foo
  ...
  (gdb) disassemble main
  Dump of assembler code for function main:
   0x00000000004003d0 <+0>:	sub    $0x88,%rsp
   0x00000000004003d7 <+7>:	mov    $0x80,%esi
   0x00000000004003dc <+12>:	mov    %rsp,%rdi
   0x00000000004003df <+15>:	callq  0x400500 <bar>
   0x00000000004003e4 <+20>:	xor    %eax,%eax
   0x00000000004003e6 <+22>:	add    $0x88,%rsp
   0x00000000004003ed <+29>:	retq   
  End of assembler dump.
  (gdb) disassemble bar
  Dump of assembler code for function bar:
   0x0000000000400500 <+0>:	sub    $0x8,%rsp
   0x0000000000400504 <+4>:	mov    %rsi,%rdx
   0x0000000000400507 <+7>:	xor    %esi,%esi
   0x0000000000400509 <+9>:	callq  0x4003b0 <memset@plt>
   0x000000000040050e <+14>:	add    $0x8,%rsp
   0x0000000000400512 <+18>:	retq   
  End of assembler dump.
1 comments

That should be __asm__ __volatile__ but extended inline asm, even with no actual opcode (even if "nop" would work pretty much everywhere) is not portable. So at this point, you might just use clang/gcc/icc pragmas instead.