Page 1 of 1
Setting Bits in Assembly
Posted: Mon Oct 31, 2022 3:19 pm
by FunnyGuy9796
I am wondering if to set a bit in assembly one must use the OR operation. I have found resources that say this is what the operation is for but other say it is used for other purposes.
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 3:53 pm
by kzinti
If you only one to set one bit, you can use the BTS instruction. Similarly you can use the BTR instruction to clear one bit.
https://www.felixcloutier.com/x86/bts
https://www.felixcloutier.com/x86/btr
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 3:53 pm
by devc1
Google is your friend :
https://www.felixcloutier.com/x86/or
OR sets the ZF (Zero flag) and PF (Parity flag).
You can use or to check if the value is cleared, and I think this is faster than a cmp.
Edit : according to the instruction table, cmp is as fast as OR (1 clock cycle)
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 4:08 pm
by Octocontrabass
FunnyGuy9796 wrote:I am wondering if to set a bit in assembly one must use the OR operation. I have found resources that say this is what the operation is for but other say it is used for other purposes.
The x86 instruction set has lots of instructions that can set one bit, and all of those instructions can be used for other purposes. The one that works best will depend on where the bit is.
devc1 wrote:You can use or to check if the value is cleared, and I think this is faster than a cmp.
It's not faster. You should use TEST instead.
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 5:24 pm
by nexos
devc1 wrote:Edit : according to the instruction table, cmp is as fast as OR (1 clock cycle)
No. OR can operate using the processor's boolean logic, whereas CMP operates using subtraction internally and hence passes through the ALU. Boolean logic operations are a lot more natural to the CPU then ALU operations, hence OR is faster.
But why use OR to test bits when you have TEST, which is devoted to that job?
For the OP, I would recommend using OR. E.g., this will set bit 5 in AX (this is in AT&T syntax):
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 6:51 pm
by Octocontrabass
devc1 wrote:Edit : according to the instruction table, cmp is as fast as OR (1 clock cycle)
OR causes a false dependency, so OR will usually be slower.
nexos wrote:OR can operate using the processor's boolean logic, whereas CMP operates using subtraction internally and hence passes through the ALU. Boolean logic operations are a lot more natural to the CPU then ALU operations, hence OR is faster.
Most x86 CPUs use the same ALUs for both boolean and arithmetic operations, so there's usually no difference in performance between boolean and arithmetic operations if all other things are equal.
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 7:26 pm
by kzinti
To set a bit, you use bts.
To clear a bit, you use btr.
To set multiple bits, you use or
To clear multiple bits, you use and
To test one or multiple bits, you use test.
All of them will have very comparable performance, if not the same. Any difference here really doesn't matter with modern processors.
There are other instructions that can accomplish more or less the same thing, but they would obfuscate the meaning of what you are trying to express.
Keep it simple. We write code to be read by humans, not by machines.
Re: Setting Bits in Assembly
Posted: Mon Oct 31, 2022 9:22 pm
by Gigasoft
Why bother replying to this, these threads are obviously not real questions but just a guy trying to be funny.
Re: Setting Bits in Assembly
Posted: Tue Nov 01, 2022 5:07 am
by devc1
Why bother replying to this
You're right, that's not even a question