From: "Andrew Crabtree" Newsgroups: comp.os.msdos.djgpp Subject: Re: !!Absolute value with inline ASM!! Date: Tue, 17 Feb 1998 09:22:37 -0800 Organization: Hewlett Packard Lines: 23 Message-ID: <6cch0m$khn$1@rosenews.rose.hp.com> References: NNTP-Posting-Host: ros51675cra.rose.hp.com Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Shawn Hargreaves wrote in message ... >Henri Ossi writes: >> I need to get the absolute value of a registers (bx) >> How is this done in asm. >> (in one command, not using cmp) >I don't know of any such method. Me neither. Unless Intel has an undocumented abs instruction. > I think a test followed by conditional >negation is probably the best you can do: that is what gcc produces for >the abs() function. I can do better (well ok, some guy on comp.lang.asm.x86 did ;). Eliminating the conditional is easy. Just sign extend the value to the next size up (convert word to dword say), then xor the values, then subtract. If the sign bit was zero then both the xor and sub are effectively nops. If it was one then you negate everything, and then add 1 to it, which gives the exact 2s complement. Andy