How does the assembly instruction of MOVSX work?

How the MOVSX build instruction works in the following example:

MOVSX ECX,BYTE PTR DS:[EDX]

In this case, here is the state of the registers:
ECX = 0000000F
EDX = 0012FD9F

From what I thought, it takes the last bytes [EDX] = 9F, moves it to ECX, and then signs it to match 16 bits = 0000009F. However, the actual result is 00000016. Can someone help explain where I am wrong?

+5
source share
2 answers

This is partially correct. But:

BYTE PTR DS:[EDX] receives the byte located at the address contained in the EDX . This byte is copied to the least significant byte in ECX , and the rest is filled with a byte.

For your unexpected result, this means that memory address 1 0x12FD9F contains byte 0x16 .


Notes:

  • DS: segment override prefix DS: not needed here. [EDX] automatically refers to DS .

1 "memory address" refers to virtual or physical memory here

+5
source

Many Intel / AMD x86 instructions are available in the β€œ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 of the instruction encoding byte and, possibly, subsequent bytes commands such as sib (scaled index byte) and immediate read / write offset. And also with the help of a possible segment prefix byte.

This is usually reg, reg / mem form instructions

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

However, in the x86 assembly code there are no separate operation / command codes for reg, reg and reg, mem forms of these instructions. Whether the operand is defined in a register or in a memory cell, in assembler, using assembly syntax.

In this case, your build code

MOVSX ECX, BYTE PTR DS: [EDX]

The command operation code is MOVSX.

The destination operand is the ECX register.

The source operand is "BYTE PTR DS: [EDX]". The fact that this is a reference to memory is indicated by several things: (1) square brackets around "[EDX]" - square brackets are short for Memory [... address ...]. (2) the prefix "DS:", which indicates that it is in the data segment. Register operands do not have such a segment prefix. (3) β€œBYTE PTR,” which states: β€œTake the memory address indicated byβ€œ DS: [EDX] ”and interpret it as a reference to an 8-bit byte in memory.”

I suspect you really want

 MOVSX ECX,DL 

"DL" is the name for the lower 8 bits of the 32-bit EDX register. That is, DL = EDX.bits [7: 0]. Unfortunately, x86 assemblers usually do not accept syntax like "EDX.bits [7: 0]" (unless I wrote them), so you need to know the historical names of the subregisters:

 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 

etc .: BL, CL, DL, DI, ...

+1
source

Source: https://habr.com/ru/post/1234225/


All Articles