淡泊明志 宁静致远
分类: C/C++
2006-12-05 11:33:39
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
long int my_labs(long int number)
{
return
(number>= 0 ? number : -number);
}
long int my_asmlabs(long int number)
{
__asm__(
"mov %1,%%eax; \n\r" //由输入寄存器 %1移动到eax
"or %%eax,%%eax; \n\r" //或运算
"jge 4f; \n\r" //SF = OF 跳转
//(符号位和溢出位相同的时候,为正数)
"neg %%eax; \n\r" //取负数预算指令
"4:"
"mov %%eax,%0; \n\r" //eax值赋给输出寄存器
%0
:"=r"(number) //输出寄存器 %0
:"r"(number) //输入寄存器 %1
);
return
number;
}
int main()
{
printf("%d\n",my_asmlabs(1));
printf("%d\n",my_asmlabs(-1));
printf("%d\n",my_asmlabs(0x7FFFFFFF));
printf("%d\n",my_asmlabs(-0x7FFFFFFF));
system("pause");
return 0;
}