What's the meaning of W suffix for thumb-2 instruction?

我是研究僧i 提交于 2019-12-20 02:30:38

问题


There is a w suffix for thumb-2 instruction as below, how does it change the semantic of the instruction without it? The search result is very noisy and I didn't get the answer.

addw r0, r1, #0


回答1:


I see ADDW in Cortex-M3 TRM Table 2-5

Data operations with large immediate
ADDW and SUBW have a 12-bit immediate. This means they can replace many from memory literal loads.

It is also mentioned in Quick Reference

add wide T2 ADD Rd, Rn, #<imm12>

Looks like the assembler would recognize the immediate constant <= 12 bits, and do the needful.

In the context where you see it, it is an ordinary "add".




回答2:


Different encodings of an instruction should have distinguishing syntaxes so when you disassemble a binary you should notice which encoding was used. This also helps when you assemble back a disassembled binary, resulting binary should be the one you start with.

In your case using addw instead of add doesn't change the semantic of instruction as it is an add operation. However it certainly forces assembler to produce Encoding T4 (32-bit) of add instruction, as that's dictated by the specification.

Summary when assembling you can use just add mnemonic and assembler would choose the right encoding and you can see that in the object dump.

int f1(int i) {
    asm volatile("add r0, #0");
    asm volatile("add r0, #257");
}

00000000 <f1>:
   0:   f100 0000   add.w   r0, r0, #0
   4:   f200 1001   addw    r0, r0, #257    ; 0x101
   8:   4770        bx  lr
   a:   bf00        nop



回答3:


Simply enough, W means "wide". It is the 32-bit version of the instruction, whereas most Thumb instructions are 16 bits wide. The wide instructions often have bigger immediates or can address more registers.

Edit: Some of the comments seem confused about the difference between addw and add.w. The only essential difference is how the immediate is encoded.

add.w:  imm32 = ThumbExpandImm(i:imm3:imm8);
addw:   imm32 = ZeroExtend(i:imm3:imm8, 32);


来源:https://stackoverflow.com/questions/22203745/whats-the-meaning-of-w-suffix-for-thumb-2-instruction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!