Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

DATA MOVEMENT INSTRUCTIONS

Lecture 5

Prepared by: Beimnet G.


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

You might also like