简体   繁体   中英

How to bit-shift an immediate value

Is there a way to bit-shift an immediate value like this :

MOV R0, #N, LSR #1 (not working)

or do I have to do this in two instructions ?

MOV R0, #N MOV R0, R0, LSR #1

Let the assembler worry about encoding immediates into machine code using the barrel shifter.

As Michael says, assemblers support expressions on assemble-time constants, so you should be able to write

MOV R0, #(N >> 1)

as long as that results in a value that can be encoded using the limited number of immediate bits and rotate count.

If you aren't sure it will always assemble, and are ok with leaving the choice up to the assembler, use the pseudo-instruction that gets a constant into a register in whatever way is most efficient:

ldr r0, =(N >> 1)     @ I think I have the right syntax here

This assembles to either a mov-immediate, or a PC-relative load from a nearby constant, or maybe another choice.


If you care about which encoding is used for mov r0, #value (eg 0x4 ROR 2 vs. 0x1 ROR 0 ), then use:

mov r0, #0x4, #2        @ rotates to the right by 2.

Unlike mov with register operands, mov -immediate doesn't offer a choice of barrel-shifter options, like LSL, LSR, ROL, ROR. It's always rotate-right, and the count has to be even for ARM encoding.

IIRC, Thumb allows any count, but there are fewer immediate bits.

See the ARMv7 reference manual for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM