You are on page 1of 6

String instructions:

MOVS/MOVSB/MOVSD Instruction:
This instruction copies a byte or word or double word from a location
in the data segment to a location in the extra segment. The offset of the source
byte or word in the data segment must be in the (E)SI register. The offset of
the destination in the extra segment must be contained in the (E)DI register.
for multiple byte or multiple word or multiple double word moves a number of
elements to be moved is put in the CX register so that it can function as a
counter. After the byte or word or double word is moved (E)SI and (E)DI are
automatically adjusted to point to the next source and the next destination. If
the direction flag is 0, then (E)SI and (E)DI will be incremented by : ! for byte
move, 4 for double word move. If the DF is a 1,then (E)SI and (E)DI will be
decremented by :1 for byte move,2 for word move,4 for double word move,
MOVS affects no flags.

The way to tell the assembler whether to code the instruction for a byte
or word or double word move is to add a “B” or a “W” or “D” to the MOVS
mnemonic. MOVSB, for example,says move a string as byte.MOVSW says move
a string as word.

Examples:
CLD ;Clear Direction Flag to autoincrement SI and DI

MOV AX,00H

MOV DS,AX ;Initialize data segment register to 0

MOV ES,AX ;Initialize extra segment register to 0

MOV SI,2000H ;Load offset of start of destination into SI

MOV DI,2400H ;Load offset of start of destination into DI

MOV CX,04H ;Load length of string in CX as counter

REP MOVSB ;Decrement CX and MOVSB until CX will be 0.

After move SI will be one greater than offset of last in source string. DI will be
one greater than offset of last byte of destination string. CX will be 0.

1|Pa ge
INS/INSB/INSW Instruction : Input String from I/O Port
The Ins instruction transfers a byte or, a word or a double word, data from an
input port, designated by the DX register into the memory location pointed by
ES:(E)DI.

The way to tell the assembler whether to code the instruction for a byte or
word or double word transfer is to add a “B” or a “W” or a “D” to the INS
mnemonic. INSB, for example, says transfer a string as byte as byte. INSW says
transfer a string as word.

After the byte or word or double word is transferred (E)DI is automatically


adjusted to point to the next source and the next destination. If the direction
flag is 0,then (E)DI will be incremented by : 1 for byte transfer,2 for word
transfer,4 for double word transfer. If the DF is a 1, then (E)DI will be
decremented by : 1 for byte transfer, 2 for word transfer, 4 for double word
transfer.

It is important to note that INS instruction does not permit the port number
to be specified as an immediate value. The port number cam be addressed
through the DX register.

Examples:
;Point (E)DI at destination string

MOV(E)DI, OFFSET S_STRING

CLD ;DF cleared so (E)DI will

;auto increment after transfer.

INSB ;Transfer byte from port DX to ES:(E)DI

INSW ;Transfer word from port DX to ES:(E)DI

INSD ;Transfer double word from port DX to ES(E)DI

OUTS/OUTB/OUTSW:OUTPUT STRING FROM I/O PORT


The OUTS instruction transfers a byte or, a word or a double word, data to an
output port, designated by the DX register from the memory location pointed
by ES:(E)SI.

2|Pa ge
The way to tell the assembler whether to code the instruction for a byte or
word or double word transfer is to add a “B” or a “W” or a “D” to the OUTS
mnemonic. OUTSB, for example, says transfer a string as byte. OUTSW says
transfer a string as word.

After the byte or word or double word is transferred (E)SI is automatically


adjusted to point to the next source and the next destination. If the direction
flag is 0,then (E)SI will be incremented by : 1 for byte transfer,2 for word
transfer, 4 for double word transfer. If the DF is a 1, then (E)SI will be
decremented by : 1 for byte transfer, 2 for word transfer, 4 for double word
transfer.

It is important to note that OUTS instruction does not permit the port
number to the specified as an immediate value. The port number can be
addressed through the DX register.

Examples:
;Point (E)DI at destination string

MOV(E)DI, OFFSET S_STRING

CLD ;DF cleared so (E)DI will

;auto increment after transfer.

OUTSB ;Transfer byte from port DX to ES:(E)DI

OUTSW ;Transfer word from port DX to ES:(E)DI

OUTSD ;Transfer double word from port DX to ES(E)DI

CMPS/CMPSB/CMPSW/CMPSD Instruction:

A string is a series of the same type of data items in sequential memory


locations. The CMPS instruction can be used to compare a byte in one string
with a byte in another string or to compare a double word in one string with a
double word in another string. (E)SI is used to hold the offset of a byte or word
in the source string and (E)DI is used to hold the offset of a byte or word or a
double word in the other string. The comparison is done by subtracting the
byte or word pointed to by (E)DI from the byte or word double word pointed
to by (E)si. The AF,CF,OF,PF,SF, and ZF flags are affected by the comparison,
but neither operand is affected.
3|Pa ge
Examples:
;Point (E)SI at source string.

;Point (E)DI at destination string

MOV (E)SI,OFFSET F_STRING

MOV (E)DI,OFFSET S_STRING

CLD ;DF cleared so (E)SI and (E)DI will

;auto increment after compare

CMPS F_STRING, S_STRING ;The assembler uses names to determine whether

;string were declared as type byte or as type word.

MOV CX,100 ;Put number of string elements in CX, Point (E)SI at

;source of string and (E)DI at destination of string

MOV(E)SI,OFFSET F_STRING

MOV(E)DI,OFFSET S_STRING

STD ;DF set so (E)SI and (E)DI will auto increment after

;compare

REPE CMPSB ;repeat the comparison of string bytes until end

;of string or until compared bytes are not equal.

After the comparison (E)SI and (E)DI will be automatically incremented or


decremented according to direction flag to point to the next element in the
two stings (if DF=0,(E)SI and (E)DI ↑ otherwise ↓ ) CX functions as a counter
which is decremented after each comparison. This will go on until CX=0.

SCAS/SCASB/SCASW/SCASD Instruction:
SCAS compares a string byte with a byte in AL or a string word with word in
AX or a string double word with double word in EAX. The instruction affects
the flags, but it does not change either the operand in AL or AX or EAX or the
operand in the string. The string to be scanned must be in the extra segment

4|Pa ge
and (E)DI must contain the offset of the byte or the word or the double word
to be compared.

After the comparison (E)DI will be automatically incremented or


decremented according to direction flag to point to the next element in the
two strings (if DF=0,(E)SI and (E)DI↑ otherwise ↓) CX functions as a counter
which is decremented after each comparison. this will go on until CX =0. SCAS
affects the AF,CF,OF,PF,SF and ZF flags.

Examples:
; Scan a text string of 80 characters

; for a carriage return

MOV AL,0DH ; Byte to be scanned for into AL

MOV (E)DI,OFFSET TEXT_STRING ; Offset of string to (E)DI

MOV CX,80 ; CX used as element counter

CLD ; Clear DF, so (E)DI auto increments

REPNE SCAS TEXT_STRING ; Compare byte in string with byte in AL.

LODS/LODSB/LODSW/LODSD Instruction:
This instruction copies a byte from a string location pointed to by SI to AL,or
a word from a string location pointed to by SI to AX or a double word from a
string location pointed to by (E)SI to AX.. LODS does not affects any flags.

Examples:
CLD ; Clear direction flag so (E)SI is auto incremented

MOV (E)SI,OFFSET S_STRING ; Point (E)SI at string

LODS S_STRING.

STOS/STOSB/STOSW/STOSD Instruction:
The STOS instruction copies a byte from AL or a word from AX or a double
word from EAX to a memory location in the extra segment. (E)DI is used to
hold the offset of the memory location in the extra segment. After the copy,
(E)DI is automatically incremented or decremented to point to the next string

5|Pa ge
element in memory. If the direction flag, DF, is cleared, then (E)DI will
automatically be incremented by one for a byte string or incremented by two
for a word string or incremented by four for a double word string. If the
direction flag is set, (E)DI will be automatically decremented by one for a byte
string or decremented by two for a word string or decremented by four for a
double word string. STOS does not affect any flags.
Examples:

MOV (E)DI,OFFSET D_STRING ; Point (E)DI at destination string

; assembler uses string name to determine whether string

; is of type byte or type word. If byte string, then string

; byte replaced with contents of AL. If word string,

; then string word replaced with contents of AX.

MOV (E)DI, OFFSET D_STRING ; Point (E)DI at destination string

STOSB ; “B” added to STOS mnemonic directly tells assembler

; to replace byte in string with byte from AL.STOSW

; would tell assembler directly to replace a word

; in the string with a word from AX

REP/REPE/REPZ/REPNE/REPNZ Instruction:
REP is a prefix which is written before one of the string instructions. These
instructions repeat until specified condition exits.

Instruction code Condition for Exit


REP (E)CX=0
REPE/REPZ (E)CX=0 OR ZF=0
REPNE/REPNZ (E)CX=0 OR ZF=1

Examples:

REPZ CMP SB ; Compare string bytes until (E)CX=0

; or until string bytes not equal.

6|Pa ge

You might also like