开发者

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

开发者 https://www.devze.com 2023-04-11 23:42 出处:网络
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, t

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 wor开发者_StackOverflow社区ds 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?


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?


Although ARM (or mips, and I assume others) cannot fit a full register sized immediate, ARM does have alu operations with immediate values. And you are not limited to 0x00 to 0xFF you can

orr r0,r0,#0x02000000 

for example, no problem. 0x00 to 0xFF shifted anywhere in the 32 bits some instructions might give you a ninth bit 0x000 to 0x1FF (shifted anywhere), at least I seem to remember something about that working (for one/some instructions). You code the instruction as above and the assembler takes care of packing the immediate into the instruction for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号