www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/18/16:31:52

From: patrickd AT cmdnet DOT lu (DOSTERT Patrick)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: !!Absolute value with inline ASM!!
Date: Wed, 18 Feb 1998 19:42:49 GMT
Organization: All USENET -- http://www.Supernews.com
Lines: 48
Message-ID: <34eb2f64.624947@news.supernews.com>
References: <34E859F8 DOT ADB0A110 AT mail DOT htk DOT fi> <19980218080701 DOT DAA03384 AT ladder03 DOT news DOT aol DOT com>
Reply-To: patrickd AT cmdnet DOT lu
NNTP-Posting-Host: 20684 AT 195 DOT 95 DOT 116 DOT 182
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019