新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > ARM匯編中的立即數

        ARM匯編中的立即數

        作者: 時間:2016-11-09 來源:網絡 收藏
        同事遇到這樣一個問題:
        在eVC編譯出的匯編代碼中我看到這樣的語句:
        mov r2, #0xFF, 28 和 orr r2, r2, #0xB
        這樣得到的結果時 r2=#0xffb ,
        他試圖更直接一點優化成一句:MOV r2,#0xffb
        但是這樣之后編譯就出了問題:error A0092: no immediate rotate operand can be created: 4091

        ------------------------------------我是無辜的分割線--------------------------------

        本文引用地址:http://www.104case.com/article/201611/317884.htm

        在 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頁中提到
        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 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 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 byusing the syntax: #,

        所以,綜上所述,首先解釋清楚了 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_8和rotate_imm,可以參考上面給出的valid constants和invalid constants,簡而言之,如果該立即數可以分解成一個8-bit的二進制數循環右移偶數位,那么這個立即數是有效的,反之無效。

        在上面的例子中,想要得到 r2 = 0xFFB 那么在匯編里就必須走兩步了,一步是無論如何無法到達的。




        關鍵詞: ARM匯編立即

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 晴隆县| 广饶县| 寿宁县| 乡城县| 金川县| 山阴县| 台山市| 苏尼特左旗| 比如县| 荃湾区| 安阳市| 灵璧县| 河南省| 天全县| 邵东县| 登封市| 阿拉善左旗| 武山县| 宜兰县| 都江堰市| 遂宁市| 大埔县| 滨海县| 宁河县| 兴仁县| 嵊泗县| 安顺市| 珲春市| 剑河县| 涞源县| 乳山市| 青海省| 阿拉尔市| 阳朔县| 水富县| 呼图壁县| 报价| 萍乡市| 玉山县| 钟祥市| 天峨县|