jinglexy 发表于 2010-04-15 10:51

请教一段gcc汇编

static inline unsigned short from32to16(unsigned a)
{
      unsigned short b = a >> 16;
      asm("addw %w2,%w0\n\t"
            "adcw $0,%w0\n"
            : "=r" (b)
            : "0" (b), "r" (a));
      return b;
}里面的%w0是什么意思?查了一些gcc内嵌汇编的文章,都没有看到这个。
谢谢

EricFisher 发表于 2010-04-15 13:23

回复 1# jinglexy    1. static inline unsigned short from32to16(unsigned a)
   2. {
   3.         unsigned short b = a >> 16;
   4.         asm("addw %w2,%w0\n\t"
   5.             "adcw $0,%w0\n"
   6.             : "=r" (b)
   7.             : "0" (b), "r" (a));
   8.         return b;
   9. }%0是指第0个操作数(从0开始计数),这里指b。

至于w,参见http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gccint/Output-Template.html#Output-Template

`%' followed by a letter and a digit says to output an operand in an alternate fashion. Four letters have standard, built-in meanings described below. The machine description macro PRINT_OPERAND can define additional letters with nonstandard meanings.

所以,具体的含义还需要查看相应的代码

superfight 发表于 2010-04-15 23:15

我觉得编译出来之后objdump一下~ 看看编译出来的机器码就知道了~ = =a~

albcamus 发表于 2010-04-27 10:45

%w0,表示取%0的16比特。

w, 2字节(word), 如ax
b, 1字节, 如al
k, 4字节, 如eax
q, 8字节, 如rax

h, 高8位, 例如ah

还有一个z, 返回一个字符, 就是上头w/b/k/q/h之中的一个,也就是取operand size。

注意,这些东西,只能应用于 byte-addressable registers。 也就是像rax这样的,本身是64位的寄存器,但是你可以单独只访问它的低32位, 也可以只访问其低16位。
页: [1]
查看完整版本: 请教一段gcc汇编