LSL,但用1而不是0更新右位

时间:2020-02-16 20:07:02

标签: assembly arm64

我需要一条类似于LSL的指令,但是右边的位必须用1而不是0填充。 像这样:

mov x0, 1
XXX x0, 3 -> here I should have 1111 in x0.

1 个答案:

答案 0 :(得分:5)

不幸的是,没有一条指令可以做到这一点。从您的示例中很难说出您是否想要像算术右移这样的东西,它会基于最低有效位(取决于LSb的值是1还是0)填充,或者总是总是用1而不是0填充。无论哪种方式,您都可以在2/3指令中获得相似的结果:

MOV x0, #1

/* For the fill with LSb case */
RBIT x0, x0  /* reverse the bit order of the register */
ASR x0, x0, #3 /* use arithmetic right shift to do the shift, it will fill with the old LSb, now MSb */
RBIT x0, x0 /* fill bits back */ 

/* For the fill with 1s case */
MVN x0, x0 /* bitwise not the value of the register */
MVN x0, x0, LSL #3 /* shift the register value, filling with 0s, then invert the register again, restoring the original bits and flipping the filled 0s to 1s */

/* From the comments, it looks like OP wants the shift to come from another register and not a constant like in their post so the above needs an extra instruction */
MOV x1, #3 /* load the shift amount into a register */
MVN x0, x0
LSL x0, x0, x1 /* need a separate instruction to use a register instead of a constant as the shift amount */
MVN x0, x0