(gdb) disas htons
Dump of assembler code for function ntohs:
0x00000037aa4e9470 <ntohs+0>: ror $0x8,%di
0x00000037aa4e9474 <ntohs+4>: movzwl %di,%eax
0x00000037aa4e9477 <ntohs+7>: retq ror和movzwl在这里做什么?
发布于 2011-04-14 10:58:08
ror代表“向右旋转”,movzwl代表“移动,零扩展字到长”(由于历史原因,一直追溯到8086年,在所有x86文档中,“字”只有16位)。
所以:
ror $0x8, %di将寄存器di (在x86-64上,它包含函数的第一个整数参数)中的16位值右移8位;换句话说,交换其高字节和低字节。这是真正完成ntohs工作的部分。
movzwl %di, %eax将di中的16位值复制到eax,将其零扩展为32位值。此指令是必需的,因为函数的整型返回值以eax为单位,如果返回值小于32位,则必须将其扩展到32位。
retq从函数返回。( q只是您使用x86-64的一个线索。)
https://stackoverflow.com/questions/5657989
复制相似问题