2 String MPL
2 String MPL
Experiments No:2
Name of Student……………………………………………….. Roll No.:…… Batch:…….
4 4 4 4 4 20
Problem Statement: Write an X86/64 ALP to accept a string and to display its length.
Objective: To learn and understand the operation of accept and display string.
Hardware Requirement: NA
Theory Contents :
The 80x86 String Instructions: All members of the 80 x 86 families support five different string
instructions: MOVS, CMPS, SCAS, LODS and STOS. They are the string primitives since you
can build most other string operations from these five instructions.
How the String Instructions Operate:The string instructions operate on blocks (contiguous
linear arrays) of memory. For example, the MOVS instruction moves a sequence of bytes from
one memory location to another. The CMPS instruction compares two blocks of memory. The
SCAS instruction scans a block of memory for a particular value. These string instructions often
require three operands a destination block address a source block address and (optionally) an
element count. For example, when using the MOVS instruction to copy a string you need a source
address a destination address and a count (the number of string elements to move).
Unlike other instructions which operate on memory the string instructions are single-byte
instructions which don't have any explicit operands. The operands for the string instructions
include
For example one variant of the MOVS (move string) instruction copies a string from the source
address specified by DS:SI to the destination address sspecified by ES:DI of length CX. Likewise,
the CMPS instruction compares the string pointed at by DS:SI of length CX to the string pointed
at by ES: DI.
Not all instructions have source and destination operands (only MOVS and CMPS support them).
For example, the SCAS instruction (scan a string) compares the value in the accumulator to
values in memory. Despite their differences the 80x86's string instructions all have one thing in
common - using them requires that you deal with two segments the data segment and the extra
segment.
The string instructions by themselves do not operate on strings of data. The MOVS instruction for
example will move a single byte word or double word. When executed by itself the MOVS
instruction ignores the value in the CX register. The repeat prefixes tell the 80x86 to do a
multi-byte string operation. The syntax for the repeat prefix is:
For MOVS:
REP MOVS
For CMPS:
For SCAS:
REPE SCAS
REPZ SCAS
REPNE SCAS
REPNZ SCAS
For STOS:
You don't normally use the repeat prefixes with the LODS instruction.
As you can see the presence of the repeat prefixes introduces a new field in the source line
- the repeat prefix field. This field appears only on source lines containing string instructions. In
your source file:
The repeat field should begin at the first tab stop and
When specifying the repeat prefix before a string instruction the string instruction repeats CX
times. Without the repeat prefix the instruction operates only on a single byte word or double
word.
You can use repeat prefixes to process entire strings with a single instruction. You can use the
string instructions without the repeat prefix as string primitive operations to synthesize more
powerful string operations.
The operand field is optional. If present MASM simply uses it to determine the size of the string to
operate on. If the operand field is the name of a byte variable the string instruction operates on
bytes. If the operand is a word address the instruction operates on words. Likewise for double
words. If the operand field is not present you must append a "B" "W" or "D" to the end of the
string instruction to denote the size e.g. MOVSB MOVSW or MOVSD.
Besides the SI, DI and ax registers one other register controls the 80x86's string instructions - the
flags register. Specifically, the direction flag in the flags register controls how the CPU processes
strings.
If the direction flag is clear the CPU increments SI and DI after operating upon each string
element. For example if the direction flag is clear then executing MOVS will move the byte word
or double word at DS:SI to ES:DI and will increment SI and DI by one two or four. When
specifying the REP prefix before this instruction the CPU increments SI and DI for each element
in the string. At completion the SI and DI registers will be pointing at the first item beyond the
string.
If the direction flag is set, then the 80x86 decrements si and di after processing each string
element. After a repeated string operation, the si and di registers will be pointing at the first byte or
word before the strings if the direction flag was set.
The direction flag may be set or cleared using the cld (clear direction flag) and std (set direction
flag) instructions. When using these instructions inside a procedure keep in mind that they modify
the machine state. Therefore you may need to save the direction flag during the execution of that
procedure.
Output : The output shows us actual length of string without using inbuilt string Function.
Conclusion:In this way we studied about accepting and displaying strings using 80386
microprocessors.
1 1 What is a MacroProcessor?
Sign of Student