DATA MOVEMENT INSTRUCTION
1
INTRODUCTION
The data movement instructions include MOV, MOVSX,
MOVZX, PUSH, POP, BSWAP, XCHG, XLAT, IN, OUT, LEA, LDS,
LES, LFS, LGS, LSS, LAHF, SAHF.
String instructions: MOVS, LODS, STOS, INS, and OUTS.
The microprocessor requires an assembler program, which
generates machine language, because machine language
instructions are too complex to efficiently generate by hand.
In this chapter, the move instruction is used to introduce
machine language instruction.
2
MACHINE LANGUAGE
Native binary code microprocessor uses as its instructions to control its
operation.
Instructions vary in length
Over 100,000 variations of machine language instructions.
Format for the 8086 microprocessor
3
THE OPCODE
Selects the operation (addition, subtraction, etc.,) performed by the
microprocessor.
either 1 or 2 bytes long for most instructions
For the first byte, the first 6 bits are the binary opcode and the remaining 2
bits indicate the direction (D) of the data flow, and indicate whether the data
is a
byte or a word (W)
4
…THE OPCODE
The second byte of the opcode is a follows
The OPCODE
5
MOD FIELD
Specifies addressing mode (MOD) and whether a displacement is present with
the selected type.
If MOD field contains an 11, it selects the register-addressing mode
Register addressing specifies a register instead of a memory location, using
the R/M field
If the MOD field contains a 00, 01, or 10, the R/M field selects one of the
data memory-addressing modes.
6
MOD FIELD
All 8-bit displacements are sign-extended into 16-bit
displacements when the processor executes the instruction.
To sign-extend a number, its sign-bit is copied to the next higher-
order byte, which generates either a 00H or an FFH in the next
higher-order byte.
If the 8-bit displacement is 00H–7FH (positive), it is sign extended
to 0000H–007FH before adding to the offset address. If the 8-bit
displacement is 80H–FFH (negative), it is sign-extended to FF80H–
FFFFH.
7
REGISTER ASSIGNMENTS
REG stands for register selected.
Suppose a 2-byte instruction, 8BECH, appears in a machine language
program.
In 16-bit mode, this instruction is converted to binary and placed in the
instruction format of bytes 1 and 2, as follows
REG field contains 101, indicating register BP, so
the MOV instruction moves data into register BP
8
…REGISTER ASSIGNMENTS
Because the MOD field contains a 11, the R/M field also indicates a
register: SP; therefore, this instruction moves data from SP into BP. That is:
MOV BP,SP
9
…REGISTER ASSIGNMENTS
Register selection code
10
R/M MEMORY ADDRESSING
11
R/M MEMORY ADDRESSING
Example: instruction 8A15H
=> MOV DL, [DI]
12
R/M MEMORY ADDRESSING
If the instruction changes to MOV DL, [DI+1], the MOD field
changes to 01 for 8-bit displacement
first 2 bytes of the instruction remain the same
instruction now becomes 8A1501H instead of 8A15H
If the instruction is again changed to MOV DL, [DI+1000H] , the
machine language form becomes 8A750010H. Here, the 16-bit
displacement of 1000H (coded as 0010H) appends the opcode.
13
SPECIAL ADDRESSING MODE
A special addressing mode occurs when memory data are
referenced by only the displacement mode of addressing for 16-
bit instructions.
Examples are the MOV [1000H],DL and MOV NUMB,DL
instructions.
First instruction moves contents of register DL into data segment
memory location 1000H and second moves register DL into
symbolic data segment memory location NUMB
14
SPECIAL ADDRESSING MODE
When an instruction has only a displacement, MOD field is always
00; R/M field always 110.
You cannot actually use addressing mode [BP] without a
displacement in machine language
If the individual translating this symbolic instruction into machine
language does not know about the special addressing mode, the
instruction would incorrectly translate to a MOV [BP], DL instruction.
15
SPECIAL ADDRESSING MODE
MOV [1000H],DL
16
SPECIAL ADDRESSING MODE
MOV [1000H],DL
17
AN IMMEDIATE INSTRUCTION
An example of a 16-bit instruction using immediate
addressing.
MOV WORD PTR [BX+1000H] ,1234H moves a 1234H
into a word-sized memory location addressed by sum of
1000H, BX, and DS x 10H
6-byte instruction
2 bytes for the opcode; 2 bytes are the data of 1234H; 2
bytes are the displacement of 1000H
18
AN IMMEDIATE INSTRUCTION
MOV WORD PTR [BX+1000H] 1234H
19
SEGMENT MOV INSTRUCTIONS
If the contents of a segment register are moved by the MOV, PUSH, or POP
instructions, a special set of register bits (REG field) selects the segment register.
The opcode for this type of MOV instruction is different for the prior MOV
instructions
20
SEGMENT MOV INSTRUCTIONS
An immediate segment register MOV is not available in the
instruction set. To load a segment register with immediate data,
first load another register with the data and move it to a segment
register.
MOV BX,CS
21
DATA MOVEMENT INSTRUCTIONS
Lecture 4
Prepared by: Beimnet G.
STACK DATA TRANSFER
23
PUSH/POP
Instructions that store and retrieve data from the LIFO (last-in, first-
out) stack memory.
Always transfers 2 bytes of data to the stack.
The microprocessor has six forms of the PUSH and POP instructions:
register, memory, immediate, segment register, flags, and all
registers.
Register addressing allows contents of any 16-bit register to
transfer to & from the stack.
Memory-addressing PUSH and POP instructions store contents of a
16 bit memory location on the stack or stack data into a memory
location.
PUSH/POP
Immediate addressing allows immediate data to be pushed onto
the stack, but not popped off the stack.
Segment register addressing allows contents of any segment
register to be pushed onto the stack or removed from the stack.
The flags may be pushed or popped from the stack.
Contents of all registers may be pushed or popped.
PUSH
PUSHA (push all) instruction copies contents of the internal register
set, except the segment registers, to the stack.
PUSHA instruction copies the registers to the stack in the following
order: AX, CX, DX, BX, SP, BP, SI, and DI. After all registers are
pushed, the contents of the SP register are decremented by 16.
PUSHF (push flags) instruction copies the contents of the flag
register to the stack.
POP
Performs the inverse operation of PUSH.
POP removes data from the stack and places it in a 16-bit
register, segment register, or a 16-bit memory location.
POPF (pop flags) removes a 16-bit number from the stack and
places it in the flag register;
POPA (pop all) removes 16 bytes of data from the stack and
places them into the following registers, in the order: DI, SI, BP, SP,
BX, DX, CX, and AX; a reverse order from placement on the stack
by PUSHA instruction, causing the same data to return to the same
registers.
INITIALIZING THE STACK
When the stack area is initialized, load both the stack segment
(SS) register and the stack pointer (SP) register.
All segments are cyclic in nature; the top location of a segment is
contiguous with the bottom location of the segment.
INITIALIZING THE STACK
Ex: PUSH CX
INITIALIZING THE STACK
The assembler and linker programs place the correct starting
address in the stack segment register(SS) and place the length of
the segment (top of stack) into the stack pointer register (SP).
There is no need to load these registers in your program, unless
you wish to change the initial values for some reason.
Several methods to define the stack segment in our programs.
In our assembler, the .STACK statement, followed by the number of
bytes allocated to the stack, defines the stack area.
.STACK 200H
INITIALIZING THE STACK
If the stack is not specified, a warning will appear when the
program is linked. If the stack is 128 bytes or fewer, this warning
may be ignored.
The systems assigns at least 128 bytes of memory to the stack
automatically. This memory section is located in the program
segment prefix (PSP), appended to the beginning of each program
file.
If you use more memory for the stack than 128 bytes, you will
erase information in the PSP that is critical to the operation of your
program and the computer.
LEA- LOAD EFFECTIVE ADDRESS
Loads a 16-register with the offset address of the data specified
by the operand.
LEA BX,[DI] loads the offset address specified by [DI] (contents of
DI) into the BX register.
Compared to a MOV BX, [DI] , this instruction loads the data
stored at the memory location addressed by [DI] into register BX.
This same operation can be achieved by using the OFFSET
directive with the mov instruction if the operand is a displacement.
Eg: MOV BX, OFFSET LIST == LEA BX, LIST
Both instructions load the offset address of memory location LIST
into the BX register.
LEA VARIATIONS
LEA instruction loads any 16-bit register with the offset address
determined by the addressing mode selected.
The LDS and LES instructions load any 16-bit or 32-bit register
with an offset address, and the DS or ES segment register with a
segment address.
In 80386 and above, LFS, LGS, and LSS are added to the
instruction set.
These sets of instructions are often used to fetch a new far address
that is stored in memory.
LEA VARIATIONS
Example: LDS BX,[DI]
LEA VARIATIONS
This format is used for storing all 32-bit memory addresses.
In the 80386 and above, an LDS EBX,[DI] instruction loads EBX
from the 4-byte section of memory addressed by DI in the data
segment. Following this 4-byte offset is a word (2 bytes) that is
loaded to the DS register. (Addresses a 48 bit section of memory
in total).
The most useful of the load instructions is the LSS instruction. (only
available on 80386 and above)
After executing some instructions, the old stack area is reactivated
by loading both SS and SP with the LSS instruction.
DATA MOVEMENT INSTRUCTIONS- 3
Lecture 5
Prepared by: Beimnet G.
STRING DATA TRANSFERS
STRING DATA TRANSFER
5 different string data transfer instructions.
LODS, STOS, MOVS, INS, and OUTS
Before the string instructions are used, the operation of the D flag-
bit (direction), DI, and SI must be understood as they apply to the
string instructions.
THE DIRECTION FLAG (D)
The direction flag (D, located in the flag register) selects the auto-
increment (D=0) or the auto-decrement (D=1) operation for the DI
and SI registers during string operations.
Only used with string instructions.
The CLD instruction clears the D flag: D=0
The STD instruction sets the D flag: D=1
If the instruction transfers a byte DI and SI are incremented or
decremented by 1 depending on whether or not the D flag is set.
If the instruction transfers a word DI and SI are incremented or
decremented by 2.
DI AND SI
During the execution of a string instruction, memory is accessed
through either or both of the DI and SI registers.
The DI offset address accesses data in the extra segment for all string
instructions that use it.
The SI offset address accesses data in the data segment by default.
The segment assignment of SI may be changed with a segment
override prefix. However, the DI segment assignment cannot be
changed.
The reason that one pointer addresses data in the extra segment and
the other in the data segment is so that the MOVS instruction can move
64K bytes of data from one segment of memory to another.
LODS
Loads AL, AX, or EAX with data at the data segment offset address
indexed by the SI register.
After loading AL,AX, or EAX, the contents of SI is either decremented
or incremented depending on the whether or not the direction flag is
set.
Note: 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.
LODSB :- AL=DS:[SI] ; SI=SI±1
LODSW :- AX=DS:[SI] ; SI=SI±2
LODSD :- EAX=DS:[SI] ; SI=SI±4
LODS D1 :- AL=DS:[SI] ; SI=SI±1 (If D1 is a byte)
LODS
E.g. - LODSW
STOS
The STOS instruction stores AL, AX, or EAX at the extra segment
memory location addressed by the DI register.
After the byte (AL), word (AX), or doubleword (EAX) is stored,
contents of DI increment or decrement depending on the
whether or not the direction flag is set.
STOSB ES:[DI]= AL; DI= DI ± 1
STOSW ES:[DI]= AX; DI= DI ± 2
STOSD ES:[DI]= EAX; DI= DI ± 4
STOS D2 ES:[DI]= AX; DI=DI ± 2 (if D2 is a word)
STOS WITH A REP
A repeat prefix (REP) can be added to any string data transfer
instruction, except the LODS instruction.
The REP prefix causes CX (counter register) to decrement by 1
each time the string instruction executes; after CX decrements, the
string instruction repeats. The instruction continues until CX=0.
If CX is loaded with 10 and a REP STOSB instruction executes, the
microprocessor automatically repeats the STOSB 10 times. Because
the DI register is automatically incremented or decremented
(depending on the value of D) after each execution AL will be
copied into a the memory 10 times.
MOVS
The only memory- to- memory move instruction allowed.
Transfers a byte, word, or doubleword from the data segment addressed by SI to
extra segment location addressed by DI, the pointers then are incremented or
decremented, as dictated by the direction flag.
MOVSB ES:[DI]=DS:[SI]; SI=SI ± 1 and DI=DI ± 1
MOVSW ES:[DI]=DS:[SI]; SI=SI ± 2 and DI=DI ± 2
MOVSD ES:[DI]=DS:[SI]; SI=SI ± 4 and DI=DI ± 4
MOVS D1,D2 ES:[DI]=DS:[SI]; SI=SI ± 1 and DI=DI ± 1 (if D1 & D2 are
bytes)
INS AND OUTS
INS inputs data from an I/O device addressed by DX and stores it in
the memory location addressed by DI. The OUTS instruction outputs
the contents of the memory location addressed by SI and sends it to
the I/O device addressed by DX.
INS transfers a byte, word, or doubleword of data from an I/O
device into the extra segment memory location addressed by the DI
register. I/O address is contained in the DX register. Useful for
inputting a block of data from an external I/O device directly into the
memory.
OUTS transfers a byte, word, or doubleword of data from the data
segment memory location address by SI to an I/O device. I/O device
addressed by the DX register as with the INS instruction.
MISCELLANEOUS DATA TRANSFER INSTRUCTIONS
XCHG, XLAT, IN, OUT, BSWAP, MOVSX, MOVZX, and CMOV
Not used as often the MOV instruction
XCHG
The XCHG (exchange) instruction exchanges the contents of a
register with the contents of any other register or memory location.
The XCHG instruction cannot exchange segment registers or
memory-to-memory data.
The operands cannot be immediate data.
XCHG AX,BX
XCHG DL, [DI]
XLAT
Converts the contents of the AL register into a number stored in a memory table.
Performs the direct table lookup technique often used to convert one code to
another
An XLAT instruction first adds the contents of AL to BX to form a memory address
within the data segment.
Then it copies the contents of this address into AL
The only instruction that adds an 8-bit to a 16-bit number
DS: TABLE DB 3FH, 06H, 5BH, 4FH ;lookup table
DB 66H, 6DH, 7DH, 27H
DB 7FH, 6FH
CS: LOOK: MOV AL,5 ;load AL with 5 (a test number)
MOV BX,OFFSET TABLE ;address lookup table
XLAT ;convert (translate)
XLAT
E.g.: MOV AL,5
LEA BX, TABLE
XLAT
IN AND OUT
IN & OUT instructions perform I/O operations.
Contents of AL, AX, or EAX are transferred only between I/O device
and microprocessor.
oan IN instruction transfers data from an external I/O device into AL,
AX, or EAX
oan OUT transfers data from AL, AX, or EAX to an external I/O device
Two forms of I/O device (port) addressing exist for IN and OUT: fixed
port and variable port.
IN AND OUT
Fixed-port addressing allows data transfer between AL, AX, or
EAX using an 8-bit I/O port address. It is called fixed-port
addressing because the port number follows the instruction’s
opcode, just as it did with immediate addressing.
Often, instructions are stored in ROM. A fixed-port instruction
stored in ROM has its port number permanently fixed because of
the nature of read-only memory. A fixed-port address stored in
RAM can be modified, but such a modification does not conform to
good programming practices.
The port address appears on the address bus (A0-A15) during an
I/O operation.
IN AND OUT
Variable-port addressing allows data transfers between AL, AX, or EAX and a 16-bit
port address.
the I/O port number is stored in register DX, which can be changed during the
execution of a program.
The 16-bit I/O port address appears on the address bus pin connections A0–A15.
IN AL, p8 ; p8 is an 8 bit I/O port number; a byte input stored in AL from port p8
IN AX, p8 ; a word input stored in AX from port p8
IN AX, DX ; DX contains a 16 bit I/O number; a byte is copied into AX from DX
addresses
OUT p8, AL ; a byte is outputted to p8 from AL
OUT DX, AL ; a byte is outputted to the port addressed by DX
IN AND OUT
E.g. OUT 19H, AX
OTHER DATA TRANSFER COMMANDS
MOVSX and MOVZX- used to move and sign extend and move
and zero extend. Found in the 80386 and later instruction sets.
BSWAP- (byte swap) This instruction takes the contents of any 32-
bit register and swaps the first byte with the fourth, and the second
with the third. Only in 80486–Pentium 4 microprocessors.
This instruction is used to convert data between the big and little
endian forms.
BSWAP EAX ; if EAX= 00112233H EAX=33221100H
CMOV- (conditional move) This instructions move the data only if
the condition is true. Has many variations: CMOVZ, CMOVL,
CMOVO…etc. Not available in the 8086 microprocessor.
SEGMENT OVERRIDE PREFIX
The segment override prefix, which may be added to almost any
instruction in any memory addressing mode, allows the programmer
to deviate from the default segment.
Example: MOV AX,[DI] by default accesses the data
segment
The segment override prefix ES can be added to this instruction to
change the segment addressed,
MOV AX, ES:[DI] accesses the extra segment
ASSEMBLER DETAILS
DIRECTIVES
Pseudo-operations that control the assembly process.
Directives indicate how an operand or section of a program is to
be processed by the assembler.
Some directives generate and store information in the memory;
others do not. The DB (define byte) directive stores bytes of data in
the memory, whereas the BYTE PTR directive indicates the size of
the data referenced by a pointer or index register.
Other directives: .286, .386, .CODE, .DATA, .STACK, ASSUME,
BYTE, DB, DW, END, ENDP, NEAR, FAR, PTR …
STORING DATA IN MEMORY SEGMENT
DB (define byte), DW (define word), and DD (define doubleword) are most
often used to define and store memory data.
These directives label a memory location with a symbolic name and indicate its
size.
E.g. D1 DB 3AH
D2 DW ‘C’,C
Memory is reserved for use in the future by using a question mark (?) as an
operand for a DB, DW, or DD directive.
E.g. D3 DB ?
The DUP (duplicate) directive can be used to create an array
E.g. D4 DB 10 DUP (4)
D5 DW 10 DUP (?)
DEFINE DIRECTIVES
EQU, ORG AND ASSUME
The equate directive (EQU) equates a numeric, ASCII, or label to another label.
Equates make a program clearer and simplify debugging.
E.g.
EQU, ORG AND ASSUME
The ORG (origin) statement changes the starting offset address of
the data in the data segment to a certain location.
E.g. ORG 300H ; Offset=0300
Used when the origin of the data or code should be set to an
absolute offset address.
ASSUME tells the assembler what names have been chosen for the
code, data, extra, and stack segments
E.g. ASSUME CS:CODE_SEG, DS:DATA_SEG
PROC AND ENDP
The PROC and ENDP directives indicate the start and end of a
procedure (subroutine).
PROC_NAME PROC FAR/NEAR
.
.
.
PROC_NAME ENDP
MEMORY ORGANIZATION
The assembler uses two basic formats for developing software:
one method uses models; the other uses full-segment definitions
The models are easier to use for simple tasks. (and will be used in
labs for this course)
The full-segment definitions offer better control over the assembly
language task and are recommended for complex programs.
MODELS
There are many models available to the MASM or TASM assembler, ranging from tiny
to huge.
To designate a model, use the .MODEL statement followed by the size of the memory
system (TINY, SMALL, HUGE…)
Special directives such as @DATA are used to identify various segments.
MODELS
FULL- SEGMENT DEFINITIONS
NEXT: ARITHMETIC AND LOGIC INSTRUCTIONS