OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:55 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 3:19 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
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.


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 3:53 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
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://github.com/kiznit/rainbow-os


Last edited by kzinti on Mon Oct 31, 2022 3:53 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 3:53 pm 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
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:
or eax, eax
jz .EaxIsClear


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 4:08 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 5:24 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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:
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


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 6:51 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 7:26 pm 
Offline
Member
Member

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

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Mon Oct 31, 2022 9:22 pm 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 852
Why bother replying to this, these threads are obviously not real questions but just a guy trying to be funny.


Top
 Profile  
 
 Post subject: Re: Setting Bits in Assembly
PostPosted: Tue Nov 01, 2022 5:07 am 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
Quote:
Why bother replying to this


You're right, that's not even a question


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 67 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group