MOVZX assembly language instructions and MOVSX

Although MOV instruction can not simply copy a small number of operations to a larger number of operations, but the programmer can think of ways to solve this problem. Suppose you want to count (unsigned 16-bit) to ECX (32 bits) may be first set to 0 ECX, then transferred to a count CX:

 

  .data  count WORD 1  .code  mov ecx,0  mov cx,count

If there is a signed integer -16 to perform the same operation what will happen?

  .data  signedVal SWORD -16      ; FFF0h (-16)  .code  mov ecx,0  mov cx,signedVal         ; ECX = 0000FFF0h(+ 65,52 0)

Value (+65 520) ECX is completely different and -16. However, if the first set ECX FFFFFFFFh, and then copy signedVal to CX, then the final value is entirely correct:

  mov ecx,0FFFFFFFFh  mov cx,signedVal    ;ECX = FFFFFFF0h(-16)

Effective results of this example with the most significant bit of the source operand (1) to fill the high-purpose 16-bit operand ECX, this technique is called sign extension (sign extension). Of course, we can not always assume that the most significant bit is a source operand. Fortunately, Intel's engineers in the design of the instruction set had foreseen this problem, therefore, we set up MOVZX and MOVSX instructions are processed unsigned integers and signed integers.

MOVZX instruction

MOVZX instruction (full zero-extended and transmit) copy the source operand to the destination operand and the destination operand extended 0 to 16 bits or 32 bits. This instruction for unsigned integers only, there are three different forms:

MOVZX reg32,reg/mem8
MOVZX reg32,reg/mem16
MOVZX reg16,reg/mem8

In the three forms, the first operand (register) is a destination operand, the second operand is a source operand. Note that the source operand is not constant. The following example binary number 10001111 full zero-extended and transferred to AX:

  .data  byteVal BYTE 10001111b  .code  movzx ax,byteVal ;AX = 0000000010001111b

The following figure shows how full source operands are zero-extended, and into 16-bit destination operand.

MOVZX assembly language instructions and MOVSX

The following is an example of the operand registers of various sizes:

  mov bx, 0A69Bh  movzx eax, bx     ;EAX = 0000A69Bh  movzx edx, bl     ;EDX = 0000009Bh  movzx cx, bl     ;CX = 009Bh

The following examples of the source operand is a memory operand, an execution result is the same:

  .data  byte1 BYTE  9Bh  word1 WORD 0A69Bh  .code  movzx eax, word1 ;EAX = 0000A69Bh  movzx edx, byte1 ;EDX = 0000009Bh  movzx ex, byte1     ;CX = 009Bh

MOVSX instruction

MOVSX command (transmit and sign extension) to copy the contents of the source operand to the destination operand and destination operand sign extended to 16-bit or 32-bit. This instruction for signed integer only, there are three different forms:

MOVSX reg32, reg/mem8
MOVSX reg32, reg/mem16
MOVSX reg16, reg/mem8

When the operand is sign-extended, repeating the most significant bit smaller operand (copy) the entire length in the extended position of the destination. The following example is the binary number 1000 1111b and transmitted to the sign-extended AX:

  .data  byteVal BYTE 10001111b  .code  movsx ax,byteVal      ;AX = 1111111110001111b

As shown below, the lowest eight copy, while copying the most significant bit of the source operand to each destination operand a high 8 bits.

MOVZX assembly language instructions and MOVSX

If the maximum significant digit of a hexadecimal constant greater than 7, it is the most significant bit is equal to 1. As illustrated, transferred to the hexadecimal value of BX A69B, therefore, the digital "A" means the most significant bit is 1. (A69B 0 foregoing is a convenient representation for preventing mistaken assembler identifier constants.)

4.1  Operand Type
4.2  MOV instructions
4.3  MOVZX to doing it and instructions MOVSX
4.4  LAHF SAHF instruction and
4.5 of 5  XCHG instructions
4.6  immediate offset operands
4.7  assembly language data transfer Example
4.8  addition and subtraction Detailed
4.9  the OFFSET operator
4.10  the ALIGN directive
4.11  the PTR operator
4.12  the TYPE operator
4.13  lengthof operator
4.14  the LABEL directive
4.15  indirect addressing
4.16  the JMP and LOOP instruction
4.17  64 MOV instruction
4.18  64-bit addition and subtraction

Guess you like

Origin blog.csdn.net/Javaxuxuexi/article/details/93401413