Mail Archives: djgpp/1998/02/18/16:31:52
On 18 Feb 1998 08:07:18 GMT, knakasato AT aol DOT com (KNakasato) wrote:
>>I need to get the absolute value of a registers (bx)
>>How is this done in asm.(in one command, not using cmp)
>
>There's many way, but best way might be test/jz combo that you rejected for
>some unknown
>reason.
>
[snip - some ways to abs]
unpredictable branches should be avoided where ever possible
you may want to try:
mov ebx,eax
sar ebx,31
xor eax,ebx
sub eax,ebx
which calculates eax = abs(eax) without jumping ( it destroys ebx)
( for 16 bit code, replace eax/ebx with ax,bx and 31 with 15 )
you can even insert some more instructions, as in:
mov ebx,eax
nop
sar ebx,31
nop
xor eax,ebx
nop
sub eax,ebx
nop
which takes the same amount of cycles ( 4 that is )
cu
P
" Do it only with the Best "
or, in C, for those who don't read asm:
int MyAbs(int n)
{
return (n ^ (n >> 31)) - (n >> 31);
}
- Raw text -