1

I have this Asm from a book that suppose to do a Boolean cast: rax := rax ? 1 : 0

1. neg      rax
2. sbb      rax,rax
3. neg      rax

But as i understand this code and the instructions (below), this would work only if the register will have 0 in it.

because lets assume that (register have some positive number in it):

rax = 9    ;initial register
rax = -9   ;after line 1
rax = -17  ;after line 2
rax = 17   ;after line 3

register have 0 in it:

rax = 0    ;initial register
rax = 0    ;after line 1
rax = 0    ;after line 2
rax = 0    ;after line 3

I think that the author meant to do add in line 2 instead of sbb am i correct?

Instruction explanations: neg instruction:

neg Destination      ;Destination = -Destination      
if(Destination == 0)
  CF = 0;
else 
  CF = 1;

sbb instruction:

sbb Destination, Source      ;Destination = Destination - (Source + CF);

1 Answer 1

1

SBB:

Destination = Destination - (Source + CF);

-9 - ( -9 + 1 ) = -9 - ( -8 ) = -9 + 8 = -1, not -17
1
  • @Oh you right, so embarrassing
    – Moshe D
    Oct 3, 2018 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.