ARM立即数

忘了是在哪里找到的这篇文章,讲的不错,所以拿来自己用了。感谢作者...

 

mov r2,#0xffb 这句中,不是MOV的用法出错,而是立即数用法出错。

立即数的用法定义在Arm Architechture Reference Manual(简称ARMARM)A5-4页开始

很重要的一段:

An immdediate operand value is formed by rotating an 8-bit constant (in a 32-bit word) by an even number of bits (0,2,4,8,26,28,30). Therefore, each instruction contains an 8-bit constant and a 4-bit rotate to be applied to that constant.

Some valid constants are:
0xFF, 0x104, 0xFF0, 0xFF00, 0xFF000, 0xFF000000, 0xF000000F

Some invalid constants are:
0x101, 0x102, 0xFF1, 0xFF04, 0xFF003, 0xFFFFFFFF, 0xF000001F

而在下面的A5-6页中提到
<immediate> Specifies the immediate constant wanted. It is encoded in the instruction as an 8-bit immediate (immed_8) and a 4-bit immediate (rotate_imm), so that <immediate> is equal to the result of rotating immed_8 right by (2*rotate_imm) bits.

shifter_operand = immed_8 Rotate_Right (rotate_imm * 2)

以及
Some values of <immediate> have more than one possible encoding. When more than one encoding is available, an assembler needs to choose the correct one to use. For more precise control of the encoding, the instruction fields can be specified directly by using the syntax: #<immed_8>, <rotate_amount>

所以,综上所述,首先解释清楚了 mov r2,#0xFF,28 一句。28并不是第三个操作数,而是和0xFF并在一起作为立即数使用,将0xFF循环右移28位。而这里必须强调右移XX位必须是个偶数,因为它将等于rotate_imm*2,那么在该指令的机器码中rotate_imm = 14, 也就是在32-BIT的机器码中第11到第8 bit = 1110B

然后再来看 mov r2,#0xFFB 的出错情况
0xFFB = 111111111011B
,很显然按照 shifter_operand = immed_8 Rotate_Right (rotate_imm * 2) 的公式, shifter_operand = 0xFFB时无法得到有效的 immed_8 rotate_imm, 所以编译出现错误 error A0092: no immediate rotate operand can be created: 4091 也可以理解了,它应该是说无法生成rotate_imm,实际上immed_8也是无法生成的。

关于立即数如何分解成immed_8rotate_imm,可以参考上面给出的valid constantsinvalid constants,简而言之,如果该立即数可以分解成一个8-bit的二进制数循环右移偶数位,那么这个立即数是有效的,反之无效。

在上面的例子中,想要得到 r2 = 0xFFB 那么在汇编里就必须走两步了,一步是无论如何无法到达的。

下面加上自己的理解

---------------------------------------------------------------------------

 首先,这个立即数必须以8位作为基准(8位位图),如果这个数中‘1’的个数>=2,那么如果

  第一(高位)和最后一个‘1’(低位)之间的距离超过前后8位的范围内(包括它们两个本

  身),那么意味着,这个立即数的位图超过了8位,即不是合法的。如果等于8位,那这个立即

  数也未必一定是合法的。因为如果最后一个‘1’(低位)后面还跟着奇数个‘0’的话,这个

  立即数依然是不合法的(符合右移偶数位的规定)。只有当跟的‘0’的个数为偶数时,才是

  合法的。

  再强调一下公式:shifter_operand = immed_8 Rotate_Right (rotate_imm * 2)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值