Re:ARM immediate value encoding (2014)
by webkike on Tuesday January 30, 2018 @02:07PM
(#16267989)
so... what is the proper way to set a register to the value 0x101, or any other value that cannot be represented?
[ Parent
| on HN ]
Re:ARM immediate value encoding (2014)
by brandmeyer on Tuesday January 30, 2018 @03:10PM
(#16268552)
In ARMv7 and previous, the program counter is an ordinary register, so you can perform a load relative to the PC for anything that doesn't fit into a register's literal field. Compilers typically emit so-called literal pools before the entry or after the exit point of each function (or sometimes before a basic block inside very large functions). Some compilers support literal pool merging as a code size reduction technique, too.
Edit: Also, ARMv8 has dedicated instructions for for pc-relative loads and jumps.
On ARM1156, and ARMv7 and newer, use MOVW, which takes a simple 16-bit immediate. If 16 bits doesn’t do it, use a MOVW/MOVT pair which can load any 32-bit value.
On older ARMs, either do a load immediate followed by an immediate arithmetic, like so:
MOV R0, 0x0100
ADD R0, R0, 0x01
Or use a PC relative load: LDR R0, [PC, offset where you stored 0x101]
Which the assembler allows you to abbreviate as: LDR R0, =0x101
And it will find a spot for 0x101 and generate the correct PC relative load.
Re:ARM immediate value encoding (2014)
by JoshTriplett on Tuesday January 30, 2018 @02:11PM
(#16268017)
Use two instructions; load part of it then load the rest.
If I understand correctly, for some values that would take four instructions.
Re:ARM immediate value encoding (2014)
by jhawthorn on Tuesday January 30, 2018 @02:37PM
(#16268260)
You would use the LDR instruction to load the word from RAM. So just one instruction, but with some slower memory access.
When writing ARM assembly, the assembler (at least the ones I've seen) abstract this detail away from you.
You can use
ldr r0, =0xff000000
and the assembler will emit a MOV if it can, and if it can't it will put the value somewhere in RAM and use LDR to load it up.ARM is fun.
Modern ARM actually has a 16-bit immediate version of MOV, plus MOVT which is the same but moves to the top 16 bits of a register. With a pair of those, you can load any 32-bit value in 2 instructions, without polluting the data cache like you would with LDR [pc+offset].