现在的位置: 首页 > 综合 > 正文

ARM Immediate Values的导入方法

2014年02月27日 ⁄ 综合 ⁄ 共 2047字 ⁄ 字号 评论关闭

You can’t fit an arbitrary 32-bit value into a 32-bit instruction word. ARM data
processing instructions have 12 bits of space for values in their instruction word. This is arranged as a four-bit rotate value and an eight-bit immediate value:

Immediate values.

The 4-bit rotate value stored in bits 11-8 is multiplied by two giving a range of 0-30 in steps of two.

Using this scheme we can express immediate constants such as:

  • 0x000000FF
  • 0x00000FF0
  • 0xFF000000
  • 0xF000000F

But immediate constants such as:

  • 0x000001FE
  • 0xF000F000
  • 0x55550000

...are not possible.

An assembler will convert big values to the rotated form. Impossible values will cause an error.

Some assemblers will use other tricks such as using MVN instead
of MOV to
form the bitwise complement of the required constant. For example the impossible instruction MOV
r0,#0xFFFFFFFF
 could be assembled as MVN
r0,#0
.

Remarks

The impact of this is that some constants are “ARM friendly”.
Some are not. Study of the numbers you’re using can sometimes reveal scope for optimisation.

Loading Wide Values

You can form constants wider than those available in a single instruction by using a sequence of instructions to build up the constant. For example:

MOV r2, #0x55           ; R2 = 0x00000055
ORR r2, r2, r2, LSL #8  ; R2 = 0x00005555
ORR r2, r2, r2, LSL #16 ; R2 = 0x55555555

...or load the value from memory:

LDR r2, =0x55555555

The pseudo-instruction LDR
Rx,=const
 tries to form the constant in a single instruction, if possible, otherwise it will generate an LDR.

LTORG用于声明一个数据缓冲池,(也称为文字池)的开始。在使用伪指令LDR时,常常需要在适当的地方加入LTORG声明数据缓冲池,LDR加载的数据暂时放于数据缓冲池。

语法 LTORG

使用说明:

当程序中使用LDR之类的指令时,数据缓冲池的使用可能越界。为防止越界发生,可使用LTONG伪操作定义数据缓冲池。通常大的代码段可以使用多个数据缓冲池。ARM汇编编译器一般把数据缓冲池放在代码段的最后面,即下一代码段开始之前,或者END伪操作之前。LTORG伪操作通常放在无条件跳转指令之后,或者子程序返回指令之后,这样处理器就不会错误的将数据缓冲池中的数据当作指令来执行。

    LDR伪指令在汇编时,如果立即数可以用<shifter_operand>表示的话就替换为MOV或MVN指令;如果不能用其表示则需要将该立即数放到一个文字池中,并生成一条将该文字池内容加载到目标寄存器的LDR指令。而使用LDR指令必须保证文字池在其可以访问的地址范围之内,对于ARM指令集来说就是4KB,所以必须在LDR指令前后4KB的范围内用LTORG显式地在代码段中添加一个文字池。

LTORG是在此指令出现的地方放一个文本池(literal pool). 在ARM汇编中常用到

    ldr   r0, =instruction     将地址instruction载入r0.

此时编译器将ldr尽可能的转变成mov或mvn指令。

如果转变不成, 将产生一个ldr指令, 通过pc相对地址从一块保存常数的内存区读出instruction的值。此内存区既是文本池。一般的, 文本池放在END指令之后的地方。但是, 如果偏移地址大于4k空间, ldr指令会出错(因为ldr的相对偏移地址为12-bit的值). 此时使用LTORG放到会出错的ldr指令附近, 以解决此问题。编译器会收集没有分配的ldr的值放到此文本池中。

抱歉!评论已关闭.