Negate a number in assembly

Started by
22 comments, last by Spoonbender 18 years ago
Quote:Original post by Raghar
xor would work as well. BTW Have you looked into Intel's manuals?


intel manuals (they're 100% free when available)

Quote:
Printed copies of the IA-32 Intel® Architecture Software Developer's Manuals, Volumes 1-3 are not available at this time. Please check back in early May to order the manuals.
This space for rent.
Advertisement
Quote:Original post by Raghar
xor would work as well.


No, it wouldn't. The answer is to use the "neg" instruction. That's why it's there.
not eax
add eax, 1


sub eax (0), a

I talked about alternatives, not about what is optimal.
Quote:Original post by bakery2k1

neg eax


Quoted for emphasis.
Ra
Quote:Original post by gumpy macdrunken
intel manuals (they're 100% free when available)

Quote:
Printed copies of the IA-32 Intel® Architecture Software Developer's Manuals, Volumes 1-3 are not available at this time. Please check back in early May to order the manuals.


Well he could download them for now.
Is this turning into a joke thread?

Just in case, there is an instruction that does exactly what the OP wants and there is no reason not to use it.

Quote:Original post by bakery2k1
neg eax
-Mike
You could also use:

xor ebx, ebxsub ebx, eax   // result is now in ebx


another alternative:

xor eax, -1sub eax, -1


:-)
Hmmm....I'm pretty sure the only way to negate a number in assembly is to use xor somehow........
Quote:Original post by dustamulet
Hmmm....I'm pretty sure the only way to negate a number in assembly is to use xor somehow........


Yeah, the negate (neg) instruction won't work. </sarcasm> Why do people even discuss alternatives when we have the neg instruction?
Quote:Original post by dustamulet
Hmmm....I'm pretty sure the only way to negate a number in assembly is to use xor somehow........


Quote:Original documentation by Intel
NEG-Two's Complement Negation
Opcode        Instruction  64-Bit Mode  Compat/Leg Mode  DescriptionF6 /3         NEG r/m8     Valid        Valid            Two's complement negate r/m8.REX + F6 /3   NEG r/m8*    Valid        N.E.             Two's complement negate r/m8.F7 /3         NEG r/m16    Valid        Valid            Two's complement negate r/m16.F7 /3         NEG r/m32    Valid        Valid            Two's complement negate r/m32.REX.W + F7 /3 NEG r/m64    Valid        N.E.             Two's complement negate r/m64.

Description
Replaces the value of operand (the destination operand) with its two's complement. (This operation is equivalent to subtracting the operand from 0.) The destination operand is located in a general-purpose register or a memory location.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!

This topic is closed to new replies.

Advertisement