Setting Bits in Assembly

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
FunnyGuy9796
Member
Member
Posts: 61
Joined: Tue Sep 13, 2022 9:29 pm
Libera.chat IRC: FunnyGuy9796

Setting Bits in Assembly

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Setting Bits in Assembly

Post 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
Last edited by kzinti on Mon Oct 31, 2022 3:53 pm, edited 1 time in total.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: Setting Bits in Assembly

Post 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)

Code: Select all

or eax, eax
jz .EaxIsClear
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Setting Bits in Assembly

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Setting Bits in Assembly

Post 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):

Code: Select all

or $(1 << 5), %ax 
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Setting Bits in Assembly

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Setting Bits in Assembly

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Setting Bits in Assembly

Post by Gigasoft »

Why bother replying to this, these threads are obviously not real questions but just a guy trying to be funny.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: Setting Bits in Assembly

Post by devc1 »

Why bother replying to this
You're right, that's not even a question
Post Reply