Data Movement Instructions
Reference
The Intel Microprocessors
By Barry B. Brey
Microprocessors and Interfacing
by Douglas V Hall
Microprocessor and Microcomputer based system design
by Md. Rafiquzzaman
Introduction
This chapter concentrates on the data movement
instructions.
The data movement instructions include MOV, PUSH,
POP, LEA, LDS, LES, etc.
String instructions: LODS, STOS, MOVS, etc.
Miscellaneous data transfer instructions: XCHG, LAHF,
SAHF, XLAT, IN, OUT, etc.
4
LEA
Loads any 16-bit register with the offset address of the
data specified by the operand.
Example: LEA BX, [DI] loads the offset address specified
by [DI] (contents of DI) into BX register.
Whereas, MOV BX, [DI] loads the data stored at the
memory location addressed by [DI] into BX register.
LEA and MOV with OFFSET instructions are both the
same length (3 bytes).
5
LEA…
• LEA loads the offset address, whereas MOV loads the data
stored at the memory location.
• Microprocessor takes longer to execute the LEA BX,LIST
instruction than the MOV BX,OFFSET LIST.
• For example, 8086 MP requires two clock to execute LEA
BX,LIST and only one clock to execute MOV BX,OFFSET
LIST. The reason that, MOV BX,OFFSET LIST instruction
executes faster because the assembler calculates the offset
address of LIST, whereas the MP calculates the address for
LEA instruction.
• The MOV BX,OFFSET LIST instruction is actually assembled
as a move immediate instruction and is more efficient.
• LEA BX, [DI] and MOV BX, DI instructions do the same result
6
LDS
LDS instruction load any 16-bit register with an offset
address and the DS segment register with a segment
address.
This instructions use any memory-addressing modes
to access a 32-bit memory section that contain both
segment and offset address. Higher 16-bit loads in DS and
lower 16-bit loads in the destination register.
Example: LDS BX, [DI] instruction transfers the 32-bit
number addressed by DI in the data segment into
the BX and DS registers.
7
LDS…
DS memory map
MOV BX, 2001H FF 2000
LDS DI, [BX] CD 2001
22 2002
55 2003
After perform LDS, 66 2004
DS=6655H 77 2005
DI=22CDH
8
LES
LES instruction load any 16-bit register with an offset
address and the ES segment register with a segment
address.
Example: LES BX, [DI] instruction transfers the 32-bit
number addressed by DI in the data segment into
the BX and ES registers.
9
LES…
DS memory map
MOV BX, 2001H FF 2000
LES DI, [BX] CD 2001
After perform LES, 22 2002
ES= ? 55 2003
DI= ? 66 2004
77 2005
ES=6655H
DI=22CDH
10
String data transfers
String data transfer instructions: LODS, STOS, MOVS,
etc.
Before the string instructions are presented, the
operation of the D (direction) flag-bit, DI, and SI must be
understood as they apply to the string instructions.
11
The Direction Flag
The D (direction) flag selects the auto-increment or the
auto-decrement operation for the DI and SI registers
during string operations.
The CLD instruction clears the D flag and the STD
instruction sets it .
CLD instruction selects the increment mode (D=0) and
STD selects the decrement mode (D=1).
12
DI and SI
During execution of string instruction, memory accesses
occur through DI and SI registers.
DI offset address accesses data in the extra segment
(ES) for all string instructions that use it.
SI offset address accesses data by default
in the data segment (DS).
13
LODS
Loads AL or AX with data stored at the data segment
offset address indexed by the SI register.
LODS B AL=DS:[SI];
LODS W SI=SI1
AX=DS:[SI]; SI=SI2
After loading AL with a byte or AX with a word, the
contents of SI increment if D=0 or decrement id D=1.
A ‘1’ is added to or subtracted from SI for a byte-sized
LODS.
A ‘2’ is added or subtracted for a word-sized LODS.
14
LODS…
Given, D=1, LIST=word size
DS memory
MOV SI, 2003H 11 2000
LODS LIST 12 2001
13 2002
Find the result after last instruction.
14 2003
After perform LODS LIST, 15 2004
AX=1413H 16 2005
SI=SI-2=2003-2=2001H
17 2006
LODS B 18 2007
After perform LODS B,
AL=12H
SI=SI-1=2001-1=2000H
finally, AX=1412H 15
STOS
Stores AL or AX at the extra segment memory location
addressed by the DI register.
STOS B ES:[DI]=AL;
STOS W DI=DI1
ES:[DI]=AX; DI=DI2
STOSB (stores a byte) stores the byte in AL at the extra
segment memory location addressed by DI.
STOSW (stores a word) stores AX in the memory
location addressed by DI.
After the byte (AL) or word (AX) is stored, contents of DI
increment or decrement.
16
STOS…
Given, D=1
ES memory
MOV DI, 2004H 11 2000
MOV AX, 5566H 12 2001
STOS B 13 66 2002
STOS W 2003
14 55
Perform these instructions and 15 66 2004
draw the change in memory map. 16 2005
After perform STOS B, 17 2006
DI=DI-1=2004-1=2003H
18 2007
After perform STOS W,
DI=DI-2=2003-2=2001H
17
STOS…
18
MOVS
MOVS instruction transfers a byte or word from the data
segment (DS) location addressed by SI to the extra
segment (ES) location addressed by SI.
Its only instruction that allows memory to memory
transfer.
SI & DI are incremented or decremented by depending
on the D (direction) flag.
MOVS B ES:[DI]=DS:[SI]; SI=SI
MOVS W DI=DI1; 1
ES:[DI]=DS:[SI]; DI=DI2; SI=SI2
19
MOVS…
Given, D=0. Perform the following instructions and draw the
change in memory map.
DS map ES map
MOV SI, 2003H
2000 11 F1 2000
MOV DI, 2004H
MOVS B 2001 12 F2 2001
MOVS W 2002 13 F3 2002
2003 14 F4 2003
After perform MOVS B, 2004 15 F5 14 2004
SI=SI+1=2004H 2005 16 F6 15 2005
DI=DI+1=2005H 2006 17 2006
F7 16
After perform MOVS W, 2007 18 F8 2007
SI=SI+2=2006H
DI=DI+2=2007H
20
XCHG
Exchanges contents of a register with the contents of
any other register or memory location.
XCHG instruction cannot exchange segment registers or
memory-to-memory data.
Exchanges are byte- or word-sized.
XCHG using the 16-bit AX register with another 16-bit
register, is most efficient exchange.
Example: XCHG AL, CL – instruction exchanges the
contents of AL with CL.
After performing XCHG, AL values goes to CL and CL
values to AL; not AL=CL.
XCHG AL,77H; XCHG AL,BX; XCHG DS,CS;
XCHG [BX],[AX] not possible, but XCHG AL, [BX] is
possible. –
21
LAHF and SAHF
LAHF instruction transfers the rightmost 8-bits of the flag
register into the AH register. Simply, Load AH from F
SAHF instruction transfers the AH register into the
rightmost 8-bits of the flag register. Simply, Store AH into F.
MOV FL, 0F00H MOV AX, 5566H
LAHF SAHF
After perform LAHF, After perform SAHF,
AH=00H FL=0F55H
22
XLAT
XLAT (translate) instruction converts the contents of the
AL register into a number stored in a memory table.
first adds the contents of AL to BX to form a memory
address within the data segment.
copies the contents of this address into AL
[AL+BX]AL
It is the only instruction that adds an 8-bit to a 16-bit
number.
DS map
0E 2010
MOV AX, 1111H
0F 2011
MOV BX, 2000H
XLAT 55 2012
66 2013
After perform
XLAT,
AL=0FH 23
IN and OUT
IN & OUT instructions perform I/O operations.
an IN instruction transfers data from an external I/O device
into AL or AX. For I/O devices READ operation.
an OUT transfers data from AL or AX to an external I/O
device. For I/O devices WRITE operation.
Two forms of I/O device (port) addressing exist for IN
and OUT: fixed port and variable port.
1. Fixed-port addressing allows data transfer between AL
or AX using an 8-bit I/O port address.
2. Variable-port addressing allows data transfers between
AL or AX and a 16-bit port address.
It is called the Variable-port addressing because the I/O
port number is stored in register DX, which can be
changed (varied) during the execution of a program.
24
IN and OUT…
Example:
IN AL, p8 – Instruction means 8-bits are input to AL from
I/O port p8.
IN AL, DX – Instruction means 8-bits are input to AL from
I/O port DX.
IN AX, DX – Instruction means 16-bits are input to AX
from I/O port DX.
OUT p8, AL – Instruction means 8-bits are output to I/O
port p8 from AL.
OUT DX, AL – Instruction means 8-bits are output to I/O
port DX from AL.
OUT DX, AX – Instruction means 8-bits are output to I/O
port DX from AX.
***Table 4-7 to 4-17: Self study*** 25