x86-64汇编中movzwl与movslq指令含义及地址计算咨询
movzwl and movslq First, let's walk through the address calculations from the preceding lea instructions—since those set up the registers used by the two instructions you're curious about:
Step 1: Calculate initial rax value
lea (%rsi,%rsi,4),%rax
LEA (Load Effective Address) doesn't access memory—it just computes an address and stores it in the destination register. The formula here is base + index*scale + displacement, so:rax = rsi + (rsi * 4) = rsi * 5
Step 2: Update rsi using the new rax
lea (%r9,%rax,2),%rsi
Using the rax from step 1, this computes:rsi = r9 + (rax * 2) = r9 + (rsi_old * 5 * 2) = r9 + (rsi_old * 10)
Step 3: Calculate a new rax value
lea (%r8,%rdx,4),%rax
Another LEA calculation:rax = r8 + (rdx * 4)
Now let's dive into the two instructions you're asking about
1. movzwl 0x402ac0(%rax,%rax,1),%eax
Let's break this into two parts: address calculation, then the data transfer logic.
Address calculation: The memory address is computed as
0x402ac0 + rax + (rax * 1) = 0x402ac0 + (rax * 2)Instruction meaning:
movzwlstands for move zero-extended word to longword. Here's what that does:- It reads a 16-bit (word) value from the computed memory address.
- It zero-extends this value to 32 bits: all the upper 16 bits of the result are set to 0, regardless of the input's bit pattern.
- The final 32-bit value is stored in
%eax(the 32-bit subset of the 64-bit%raxregister).
Example: If the memory at
0x402ac0 + rax*2holds0xABCD,%eaxwill become0x0000ABCD.
2. movslq 0x402740(,%rsi,4),%rdx
Again, split into address calculation and transfer logic:
Address calculation: The memory address here is
0x402740 + 0 + (rsi * 4) = 0x402740 + (rsi * 4)(the empty first field in the parentheses means there's no base register used here).Instruction meaning:
movslqstands for move sign-extended longword to quadword. Here's the breakdown:- It reads a 32-bit (longword) value from the computed memory address.
- It sign-extends this value to 64 bits: the upper 32 bits are set to the same value as the highest bit (sign bit) of the input 32-bit number. If the input's sign bit is 1 (negative in two's complement), the upper bits are all 1; if it's 0 (positive), the upper bits are all 0.
- The final 64-bit value is stored in
%rdx(a full 64-bit register).
Example: If the memory at
0x402740 + rsi*4holds0xFFFFFFFF(a 32-bit -1),%rdxwill become0xFFFFFFFFFFFFFFFF(the 64-bit -1). If the input is0x7FFFFFFF(max 32-bit positive),%rdxbecomes0x000000007FFFFFFF.
内容的提问来源于stack exchange,提问作者kyandy

