Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to set one bit of a register in ARM?

I'm writing ARM assembly code that at some point has to set a single bit of a register to 1. This is best done of course via "register-or-bitmask" method. However, according to ARM documentation, the Assembly ORR command (bitwise OR) does not take immediate values. In other words you can only bitwise-OR a value in one register with a value in another register. When you think about it, it makes sense because ARM instructions are themselves 32-bit long, so there's no way to cram a 32-bit mask into an instruction. However, writing an immediate value to a register just to use it right a way is inefficient because it produces a read-after-write hazard which stalls the CPU. In general, what is the most efficient way to ORR a register with a mask without wasting a register on constantly keeping that mask in memory? Does ARM recommend anything?

like image 784
Phonon Avatar asked Jan 01 '26 00:01

Phonon


1 Answers

ORR r0, r1, #0x4

is perfectly fine in standard ARM. You can encode immediate values in a 32-bit ARM instruction, but their range is limited. See this explanation.

Your link points to the Thumb documentation; are you sure you need to be using Thumb instructions?

like image 139
Graham Borland Avatar answered Jan 04 '26 22:01

Graham Borland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!