Set all bits in CPU register to 1 efficiently

后端 未结 2 2111
天涯浪人
天涯浪人 2020-11-27 07:17

To clear all bits you often see an exclusive or as in XOR eax, eax. Is there such a trick for the opposite too?

All I can think of is to invert the zero

2条回答
  •  心在旅途
    2020-11-27 07:58

    Peter's already provided a perfect answer. I just wanna mention, that it depends on the context, too.

    I for once did a sar r64, 63 of a number I know will be negative in a certain case, and if not, I don't need no all bits set value. A sar has the advantage that it sets some interesting flags, although decoding 63, really?, then I could've done a mov r64, -1, too. I guess it was the flags, that let me do it anyway.

    So bottom line: context. As you know, you usually delve into assembly language, because you want to process the extra knowledge you, but not the compiler has. Maybe some of your registers whose value you don't need anymore has a 1 stored (so logical true), then just neg it. Maybe somewhere earlier in your program you did a loop, then (provided it is manageable) you can arrange your register usage so a not rcx is all that's missing.

提交回复
热议问题