Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 01-19-2017, 08:23
mcp mcp is offline
Friend
 
Join Date: Dec 2011
Posts: 73
Rept. Given: 4
Rept. Rcvd 12 Times in 11 Posts
Thanks Given: 7
Thanks Rcvd at 47 Times in 35 Posts
mcp Reputation: 12
You can move the value to a register and use NEG on it (assuming x86, 32 bit):
Code:
mov eax, [val]
neg eax
mov [val], eax
Alternatively, subtract from zero:
Code:
xor exc, ecx
mov eax, [val]
sub ecx, eax
mov [val], ecx
Reply With Quote
The Following User Says Thank You to mcp For This Useful Post:
chants (01-19-2017)
  #2  
Old 01-19-2017, 08:47
chants chants is offline
VIP
 
Join Date: Jul 2016
Posts: 826
Rept. Given: 47
Rept. Rcvd 50 Times in 31 Posts
Thanks Given: 737
Thanks Rcvd at 1,140 Times in 529 Posts
chants Reputation: 51
It is a bit early in the morning :-D

What I really meant to ask, was can we take an expression that is 0 or non-zero and convert it to 0 or -1. Either an all-0 or all-1 bitmask.

0->0
non-zero->-1

With pure arithmetic and no conditional statements. With conditional statements, there are many ways and all are easy. But without, its not as simple.

I noticed that (A OR -A) = 0 for 0, but turns the high bit on except for the sign bit power of 2 value e.g. 0x80000000. Perhaps using CDQ.

Code:
mov eax, [val]
mov edx, eax
neg edx
or eax, edx
cdq
mov eax, edx
Could that really be the simplest algorithm to do it?

The only other interesting property like this I could think of is <0 -> -1, 0 -> 0, >1 ->1 which also would be about as tricky without conditionals and purely arithmetically.

Actually you do something along the lines of multiply the output of previous code by the carry flag after subtracting from 0(sub, adc, mul) and I don't think you can avoid the mul.

Any thoughts
Reply With Quote
The Following User Says Thank You to chants For This Useful Post:
niculaita (02-08-2017)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex-Rays and negative structure offsets jonwil General Discussion 3 02-20-2019 10:37


All times are GMT +8. The time now is 20:14.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( Since 1998 )