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 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk 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); }