I found that when I compile this code with -O2: #include <stdio.h>
#define bit_swap(v, count, res) \ do { \ asm volatile ( \ "1: sarb %1\n" \ " rclb %0\n" \ " sarb %1\n" \ " rclb %0\n" \ " loop 1b\n" \ : "=a" (res) \ : "d" (v), "c" (count) \ ); \ } while (0) int main() { char u1, u2; u1=0xA; bit_swap(u1, 4, u2); bit_swap(u2, 4, u1); printf("u1=0x%x u2=0x%x\n", u1, u2); } , gcc removes loop counter cx register between the first bit_swap() and the second bit_swap() statements, which results in the problem that the second bit_swap() is running 2^32 loops instead of 4 loops also it corrupts a final result in u1. #gcc -v Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../gcc/configure --enable-languages=c,c++ --prefix=/home/dinar/tmp/gcc-i686-pc-linux-gnu/ Thread model: posix gcc version 4.1.0 20051026 (experimental) -- Summary: gcc -O2 removes important instruction for ASM Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: gvaxon at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24922