home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,803 of 4,675   
   James Harris to Benjamin David Lunt   
   Re: Fast conversion to a boolean of 0 or   
   07 Mar 19 08:46:42   
   
   From: james.harris.1@nospicedham.gmail.com   
      
   On 06/03/2019 22:13, Benjamin David Lunt wrote:   
   > "Rick"  wrote in message   
   > news:867ac6de-826c-4630-b5b4-943e8e728158@googlegroups.com...   
   >> On Wednesday, March 6, 2019 at 2:47:41 PM UTC-5, James Harris wrote:   
   >>> A small programming challenge if you are interested, just for fun.   
   >>>   
   >>> Say we want any value other than 0 in EAX to be reduced to 1 - e.g. so   
   >>> that if non-zero means true but we want true to be 1.   
   >>>   
   >>> Is there a faster solution than the naive   
   >>>   
   >>>       cmp eax, 0   
   >>>       je done   
   >>>       mov eax, 1   
   >>>     done:   
      
   ...   
      
   >   
   > test case of eax is non-zero:   
   >    neg    eax      ; eax = -eax  (set the carry flag)   
   >    sbb    edi,edi  ; edi = -1    (X - X - 1 = -1)   
   >    neg    edi      ; edi =  1   
   >   
   > test case of eax is zero:   
   >    neg    eax      ; eax = 0     (no carry flag)   
   >    sbb    edi,edi  ; edi = 0     (X - X - 0 = 0)   
   >    neg    edi      ; edi = 0   
      
   That's really good. I hadn't thought of starting with NEG. My solutions   
   began with   
      
      sub eax, 1   
      
   in order to have the carry flag distinguish between zero and non-zero.   
      
   >   
   > This will destroy edi, but doesn't matter what edi has to   
   > begin with.  You can use any other register too, I would   
   > presume.   
      
   How about a small mod to your idea   
      
                    ;EAX = 0 or nonzero   
      neg eax       ;EAX = 0 or nonzero   
      sbb eax, eax  ;EAX = 0 or -1   
      neg eax       ;EAX = 0 or 1   
      
   ...   
      
   > However, I would bet the undocumented instruction, if it still   
   > exists, is slower than snot in the winter, and the shift isn't   
   > real fast either.  But, doesn't destroy or use any other register.   
      
   I would expect so, too. Interesting idea, though.   
      
   >   
   > (Note, this is all from memory.  I haven't coded in assembly   
   >   in some time so I may be completely wrong here...However,   
   >   it was just for fun, just like you said it would be. :-)   
      
   Me too. I have done a bit of asm programming recently but before that   
   not for a while. And I feel rusty!   
      
      
   --   
   James Harris   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca