So interesting program,who can give an explanation?
Posted: Tue Jun 03, 2003 6:44 am
//main.c
#include <stdio.h>
#include <stdlib.h>
int get_bit(int bitidx,volatile void *addr);
int main()
{
int i;
unsigned long test = 0xF0FFFFF3;
i = get_bit(2,&test);
printf("i=%d\n",i);
i = get_bit(2,&test);
printf("i=%d\n",i);
return 0;
}
int get_bit(int bitidx,volatile void *addr)
{
int bitval;
__asm__ __volatile__(
"btl %2,%1;
setc %0;"
:"=m"(bitval)
:"m"(*(unsigned long*)addr),"Ir"(bitidx));
return bitval;
}
Compile and link with: gcc main.c -o mainthen
run this program(main) you will get the following output:
i = 0 //the correct result as expected
i = 1075187968
but if you comment the 1st printf,then the output is
i = 0
You can try this program youself,who can give a reasonable explanation?thanks!
#include <stdio.h>
#include <stdlib.h>
int get_bit(int bitidx,volatile void *addr);
int main()
{
int i;
unsigned long test = 0xF0FFFFF3;
i = get_bit(2,&test);
printf("i=%d\n",i);
i = get_bit(2,&test);
printf("i=%d\n",i);
return 0;
}
int get_bit(int bitidx,volatile void *addr)
{
int bitval;
__asm__ __volatile__(
"btl %2,%1;
setc %0;"
:"=m"(bitval)
:"m"(*(unsigned long*)addr),"Ir"(bitidx));
return bitval;
}
Compile and link with: gcc main.c -o mainthen
run this program(main) you will get the following output:
i = 0 //the correct result as expected
i = 1075187968
but if you comment the 1st printf,then the output is
i = 0
You can try this program youself,who can give a reasonable explanation?thanks!