简体   繁体   English

MOVSX装配说明如何工作?

[英]How does MOVSX assembly instruction work?

How does the assembly instruction MOVSX work in this following example: 装配指令MOVSX如何在以下示例中工作:

MOVSX ECX,BYTE PTR DS:[EDX]

In this case, here are the state of the registers: 在这种情况下,这是寄存器的状态:

ECX = 0000000F   
EDX = 0012FD9F 

From what I thought, it takes last bytes of [EDX] = 9F, moves it to ECX and then sign extends it to match 16 bits = 0000009F. 根据我的想法,它需要[EDX] = 9F的最后一个字节,将其移动到ECX然后符号扩展它以匹配16位= 0000009F。 However, the actual result is 00000016. Can someone help explain where I'm wrong? 但是,实际结果是00000016.有人可以帮我解释我错在哪里吗?

That's partially correct. 这部分是正确的。 However: 然而:

BYTE PTR DS:[EDX] obtains the byte located at the address held in EDX . BYTE PTR DS:[EDX]获得位于EDX中保存的地址字节 This byte is copied to ECX into the least significant byte and the rest is filled with the sign of the byte. 该字节被复制到ECX到最低有效字节,其余字节用符号填充。

For your unexpected result, this means that at the memory address 1 0x12FD9F the byte 0x16 is located. 对于意外结果,这意味着在存储器地址1 0x12FD9F处找到字节0x16


Notes: 笔记:

  • the Segment Override Prefix DS: isn't necessary here. 此处不需要段覆盖前缀DS: [EDX] automatically refers to DS . [EDX]自动指DS

1 "memory address" refers to either virtual or physical memory here 1 “内存地址”指的是虚拟或物理内存

Many Intel/AMD x86 instructions are available in "modrm" format - they have two operands, one of which must be a register, the other of which may be a register, or a memory reference, whose address is determined by the modrm byte of the instruction encoding, and possibly by subsequent bytes of the instruction, such as the sib (scaled index byte), and the immediate constant / memory offset. 许多Intel / AMD x86指令以“modrm”格式提供 - 它们有两个操作数,其中一个必须是寄存器,另一个可以是寄存器或存储器引用,其地址由modrm字节确定。指令编码,可能还有指令的后续字节,例如sib(缩放索引字节)和立即常量/存储器偏移。 And also by a possible segment prefix byte. 还有一个可能的段前缀字节。

Usually these are reg,reg/mem instructions, of the form 通常这些是表单的reg,reg / mem指令

   rsrcdst += rsrc
or
   rsrcdst += Memory[ ... addressessing mode ...]

But x86 assembly code does not have separate opcodes / instruction mnemonics for the reg,reg and reg,mem forms of these instructions. 但是x86汇编代码没有针对reg,reg和reg,这些指令的mem形式的单独的操作码/指令助记符。 Whether an operand is a register or a memory location is indicated, in the assembler, by assembly syntax. 在汇编程序中,通过汇编语法指示操作数是寄存器还是存储器位置。

In this case, your assembly code is 在这种情况下,您的汇编代码是

MOVSX ECX,BYTE PTR DS:[EDX] MOVSX ECX,BYTE PTR DS:[EDX]

The instruction opcode is MOVSX. 指令操作码是MOVSX。

The destination operand is register ECX. 目标操作数是寄存器ECX。

The source operand is "BYTE PTR DS:[EDX]". 源操作数是“BYTE PTR DS:[EDX]”。 That this is a memory reference is indicated by several things: (1) the square brackets around "[EDX]" - square brackets are a shorthand for Memory[...address...]. 这是一个内存引用由几个东西表示:(1)“[EDX]”周围的方括号 - 方括号是Memory [... address ...]的简写。 (2) the "DS:" prefix, which indicates that it is in the data segment. (2)“DS:”前缀,表示它在数据段中。 Register operands do not have such a segment prefix. 寄存器操作数没有这样的段前缀。 (3) the "BYTE PTR" - which says "take the memory address specified by 'DS:[EDX]', and interpret it as referencing an 8-bit byte in memory". (3)“BYTE PTR” - 表示“取'DS:[EDX]'指定的存储器地址,并将其解释为引用存储器中的8位字节”。

I suspect that what you really want is 我怀疑你真正想要的是什么

MOVSX ECX,DL

"DL" is a name for the low 8 bits of 32-bit register EDX. “DL”是32位寄存器EDX的低8位的名称。 Ie DL=EDX.bits[7:0]. 即DL = EDX.bits [7:0]。 Unfortunately, x86 assemblers usually don;t accept syntax like "EDX.bits[7:0]" (unless I wrote them), so you have to know the historical names of the sub registers: 不幸的是,x86汇编程序通常不接受“EDX.bits [7:0]”之类的语法(除非我编写它们),所以你必须知道子寄存器的历史名称:

AL = EAX.bits[7:0]
AH = EAX.bits[15:8]
AX = EAX.bits[15:0]
EAX = 32 bit register that "covers" all of the above

and so on: BL, CL, DL, DI, ... 等等:BL,CL,DL,DI,......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM