0

What is the best way to set MSB bits of register x0 (64bit register) in arm?

The final goal is to set TCR_EL1 -64 bit system register- with the value I wish

Progman
  • 16,827
  • 6
  • 33
  • 48
Little Tree
  • 63
  • 1
  • 6
  • 2
    The `orr` instruction can be used with a single-bit immediate to set a bit. [Example](https://gcc.godbolt.org/z/qGWzTMncx) – Raymond Chen Oct 15 '22 at 16:48
  • Thanks for pointing out to the compiler explorer. ```orr x0,x0, #0x0123456789abcdef``` throws an error "immediate out of range at operand 3 -- ", but this works ```orr x0,x0, #0xfffffffffffffffe```. May I know the reason? So, how can I achieve the result of first instruction (I saw the output of compiler explorer, is it the only way?) – Little Tree Oct 15 '22 at 17:00
  • 3
    Only certain constants are permitted in ARM64 instructions. `#0x80000000000000` is allowed, but `#0x0123456789abcdef` is not. Clearly not all 64-bit values are permitted, since ARM64 instructions are only 32 bits in size. – Raymond Chen Oct 15 '22 at 17:33
  • Which bits do you wish to set, how many, and to what values? – Nate Eldredge Oct 15 '22 at 20:21
  • 1
    If the mask you want to use doesn't fit, try to first load the desired mask into a register (e.g. with `ldr x0, =...` or an appropriate sequence of `MOVK` and `MOVW` instructions) and then use that to set bits. – fuz Oct 15 '22 at 20:32
  • 1
    @LittleTree The immediate allowed in bitwise instructions are quite convoluted. The encoding gives you a base value whose length can be any power of 2 (up to 64). This value consists of all zero bits with a single consecutive range of ones, or vice versa. If it is shorter than 64 bits, it is replicated until 64 bits are reached. This is why values like `0x1`, `0x1111111111111111`, `0x001f001f001f001f` and `0x5555555555555555` are all encodable, but values such as `0x0`, `0xffffffffffffffff` and `0x5` are not. – Siguza Oct 15 '22 at 23:03
  • ARM64 `orr` allows an immediate with any number of *contiguous* bits set, so any number of bits contiguous with the MSB can be set, except for *all* of them; that can be done with a `mov x0, -1`. (This format also allows repeating patterns within 32, 16, 8, 4, or 2-bit chunks). Often easiest to let a compiler pick and do the work of figuring out whether an immediate is encodeable, like https://godbolt.org/z/6T9ov8n14 – Peter Cordes Oct 17 '22 at 05:26
  • Basically a duplicate of [Range of immediate values in ARMv8 A64 assembly](https://stackoverflow.com/q/30904718) – Peter Cordes Oct 17 '22 at 05:30

0 Answers0