Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 01-19-2017, 09:46
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
IIUC, you want to express something like this in assembly:

Code:
int result = n ? -1 : 0;
In that case, using neg+sbb does the trick:

Code:
mov eax, [val]
neg eax
sbb eax, eax
This works since neg sets the carry flag if its operand is non-zero, and clears it otherwise. The sbb instruction then subtracts the carry flag from zero and thus yields -1 for non-zero inputs, and zero otherwise.

Example:
eax contains a non-zero value, say x. neg eax turns this into -x and sets the CF. sbb eax, eax computes eax = eax - eax - cf <-> eax = 0 - CF.
Same drill when eax is zero initially.
Reply With Quote
The Following 2 Users Say Thank You to mcp For This Useful Post:
chants (01-19-2017), niculaita (02-08-2017)
  #2  
Old 01-19-2017, 16:55
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
Yes, that was the pearl of wisdom needed here. I had long forgotten this old SBB trick. From a C perspective its the casting to bool operation.

It would be nice if all the assembler bit tricks were compiled somewhere as similar to:
https://graphics.stanford.edu/~seander/bithacks.html
Reply With Quote
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 00:56.


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