How can I negate a 64-bit number in x86 that is stored in two 32-bit registers?
Asked
Active
Viewed 42 times
0
-
2Use any of the usual methods. Subtract from zero but mind the carry. Invert the bits, add one, mind the carry. – Jester Dec 30 '20 at 14:15
-
@Jester how do I mind the carry in this situation? – lightboltandfire Dec 30 '20 at 15:03
-
1You can do it the following way: Negate both registers independently. If the register holding the low 32 bits is zero, this is the result. If it is not zero, you must decrement (= subtract 1 from) the (result in the) register holding the high 32 bits. – Martin Rosenau Dec 30 '20 at 15:40
-
4An easy way to implement @Martin’s algorithm is `neg high; neg low; sbc high, 0` – prl Dec 30 '20 at 17:38
-
1Another way requires only two operations but uses another register: `xor dest, dest; neg low; sbc dest, high`. (This puts the high part of the result in `dest` and leaves the low part in `low`.) – prl Dec 30 '20 at 17:46