Setting Bits in Assembly
-
- Member
- Posts: 61
- Joined: Tue Sep 13, 2022 9:29 pm
- Libera.chat IRC: FunnyGuy9796
Setting Bits in Assembly
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
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
https://www.felixcloutier.com/x86/bts
https://www.felixcloutier.com/x86/btr
Last edited by kzinti on Mon Oct 31, 2022 3:53 pm, edited 1 time in total.
Re: Setting Bits in Assembly
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)
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)
Code: Select all
or eax, eax
jz .EaxIsClear
-
- Member
- Posts: 5562
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Setting Bits in Assembly
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.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.
It's not faster. You should use TEST instead.devc1 wrote:You can use or to check if the value is cleared, and I think this is faster than a cmp.
Re: Setting Bits in Assembly
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.devc1 wrote:Edit : according to the instruction table, cmp is as fast as OR (1 clock cycle)
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):
Code: Select all
or $(1 << 5), %ax
-
- Member
- Posts: 5562
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Setting Bits in Assembly
OR causes a false dependency, so OR will usually be slower.devc1 wrote:Edit : according to the instruction table, cmp is as fast as OR (1 clock cycle)
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.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.
Re: Setting Bits in Assembly
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.
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
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
You're right, that's not even a questionWhy bother replying to this