(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
What does ror and movzwl do here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
rorstands for “rotate right”, andmovzwlstands for “move, zero-extending word to long” (for historical reasons dating all the way back to the 8086, in all x86 documentation “words” are only 16 bits).So:
Rotate the 16-bit value in register
di(which, on x86-64, contains the first integer argument to a function) right by eight bits; in other words, exchange its high and low bytes. This is the piece that actually does the job ofntohs.Copy the 16-bit value in
ditoeax, zero-extending it to a 32-bit value. This instruction is necessary because a function’s integer return value goes ineax, and if it’s shorter than 32 bits, must be extended to 32 bits.Return from the function. (The
qis just a clue that you’re on x86-64.)