MOV Instruction
The MOV instruction copies data from source operand to a destination operand.
Known as a data transfer instruction
MOV destination,source ; dest = source
• Both operands must be the same size. ;MOV dx,bl ==invalid
• Both operands cannot be memory operands. ;MOV [source_address], [destination_address]
==invalid
• The instruction pointer register (IP, EIP, or RIP) cannot be a destination operand.
-----------------------------------------------------------------------------------------------------
MOVZX Instruction
The MOVZX instruction (move with zero-extend) This instruction is only used
with unsigned integers.
Using MOVZX to copy a byte into a 16-bit destination
The following examples use registers for all operands, showing all the size variations:
mov bx,0A69Bh
movzx eax,bx ; EAX = 0000A69Bh
movzx edx,bl ; EDX = 0000009Bh
movzx cx,bl ; CX = 009Bh
----------------------------------------------------------------------------------
MOVSX Instruction
The MOVSX instruction (move with sign-extend)
This instruction is only used with signed integers
data
byteVal BYTE 10001111b
.code
movsx ax,byteVal ; AX = 1111111110001111b
----------------------------------------------------------------------------------------
ADD destination,source ;dest += source or dest = dest + source;
-------------------------------------------------------------------------------------
SUB destination,source ;dest -= source or dest = dest - source;
-------------------------------------------------------------------------------------
INC operand ; operand +=1
The operand can be a register or a memory location.
------------------------------------------------------------------------------------
DEC operand ; operand -=1
-----------------------------------------------------------------------------------
NEG operand ; used to negate a value. It performs two's complement negation
-------------------------------------------------------------------------------------
flag register
Overflow OV is set when the result of an signed arithmetic operation is out of range for
the destination operand.
Direction UP
Interrupt EI
Sign PL is set when the outcome of an arithmetic operation is negative.
Zero Z
Aux Carry AC
Parity PE indicates whether or not an even number of 1 bits occurs
Carry CY is set when the result of an unsigned arithmetic operation is too large for the
destination operand.
--------------------------------------------------------------------------------------------------
The LAHF
(load status flags into AH) instruction copies the low byte of the EFLAGS register
into AH.
.data
saveflags BYTE ?
.code
lahf ; load flags into AH
mov saveflags,ah ; save them in a variable
------------------------------------------------------------------------------------------------
The SAHF (store AH into status flags) instruction copies AH into the low byte of the
EFLAGS (or RFLAGS) register.
mov ah,saveflags ; load saved flags into AH
sahf ; copy into Flags register
------------------------------------------------------------------------------------------------
XCHG Instruction
The XCHG (exchange data) instruction exchanges the contents of two operands.
XCHG reg,reg
XCHG reg,mem
XCHG mem,reg
-----------------------------------------------------------------------------------------------
Direct-Offset Operands
adding a displacement to the name of a variable, creating a direct-offset operand. This lets
you access memory locations that may not have explicit labels
Let’s begin with an array of
bytes named arrayB:
.data
arrayB BYTE 10h,20h,30h,40h,50h
.code
mov al,arrayB ; AL = 10h
mov al,[arrayB+1] ; AL = 20h
mov al,[arrayB+2] ; AL = 30h
(+1 == one byte ofset only )
mov al,[arrayB+20] ; AL = ??
-----------------------------------
Word and Doubleword Arrays
.data
arrayW WORD 100h,200h,300h
.code
mov ax,arrayW ; AX = 100h
mov ax,[arrayW+2] ; AX = 200h
-------------------------
.data
arrayD DWORD 10000h,20000h
.code
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
--------------------------------------------------------------
OFFSET Examples
In the next example, we declare three different types of variables:
.data
bVal BYTE ?
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
If bVal were located at offset 00404000 (hexadecimal), the OFFSET operator would return the
following values:
.code
mov esi,OFFSET bVal ; ESI = 00404000h
mov esi,OFFSET wVal ; ESI = 00404001h
mov esi,OFFSET dVal ; ESI = 00404003h
mov esi,OFFSET dVal2 ; ESI = 00404007h
===============================================================================
=================
Data Transfer Examples (Moves.asm)
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
val1 WORD 1000h
val2 WORD 2000h
arrayB BYTE 10h,20h,30h,40h,50h
arrayW WORD 100h,200h,300h
arrayD DWORD 10000h,20000h
.code
main PROC
; Demonstrating MOVZX instruction:
mov bx,0A69Bh
movzx eax,bx ; EAX = 0000A69Bh
movzx edx,bl ; EDX = 0000009Bh
movzx cx,bl ; CX = 009Bh
; Demonstrating MOVSX instruction:
mov bx,0A69Bh
movsx eax,bx ; EAX = FFFFA69Bh
movsx edx,bl ; EDX = FFFFFF9Bh
mov bl,7Bh
movsx cx,bl ; CX = 007Bh
; Memory-to-memory exchange:
mov ax,val1 ; AX = 1000h
xchg ax,val2 ; AX=2000h, val2=1000h
mov val1,ax ; val1 = 2000h
; Direct-Offset Addressing (byte array):
mov al,arrayB ; AL = 10h
mov al,[arrayB+1] ; AL = 20h
mov al,[arrayB+2] ; AL = 30h
; Direct-Offset Addressing (word array):
mov ax,arrayW ; AX = 100h
mov ax,[arrayW+2] ; AX = 200h
; Direct-Offset Addressing (doubleword array):
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
mov eax,[arrayD+4] ; EAX = 20000h
INVOKE ExitProcess,0
main ENDP
END main
===============================================================================
=====
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
value1 DWORD 10 ; Define a DWORD-sized variable with initial value 10
value2 DWORD 20 ; Define another DWORD-sized variable with initial value 20
result DWORD ? ; Define a variable to store the result
.code
main PROC
mov eax, value1 ; Move value from memory location value1 to EAX register
mov ebx , value2 ; Move value from memory location value2 to the value in EBX register
add ebx, eax ; add value from eax to ebx in ebx
sub ebx,eax ; ebx= total- value1= value2
INC ebx ; ebx= value2 + 1
DEC ebx ; ebx= ebx -1 = value2 +1 - 1
NEG eax ; eax=-eax
mov result,eax ; Store the result back into memory location result
; Exit the program
invoke ExitProcess, 0 ; Terminate the program with exit code 0
main ENDP
END main