Assembly Basics 2 Strings and Arrays

Most of the article examples involved are valid for testing

string concept

Basic string manipulation instructions:

 

CMPSB has a defect that the comparison string must be the same length, and the difference is 0 padding.
There is also a CMPS note
. An example of CMPSB:

 

The SCASB, SCASW and SCASD instructions compare the value in ALAX/EAX with the word or double word in the target memory addressed by EDI.
, these instructions are especially useful when looking up a value in a long string or array. If the REPE (or REP) prefix is ​​used, when ECX > 0
and AL/AX/EAX matches the value in memory, the instructions continue to scan the string or The array REPNE prefix causes the instruction to scan the string
until AL/AX/EAX matches a value in memory or stops when ECX=0.

example:

 

Character deletion is the same as this except that the REPE directive is equal to stop

The STOSB, STOSW and STOSD instructions store the contents of ALAX/EAX in the memory unit pointed to by
EDI, and the value of EDI increases or decreases according to the value of the direction flag. When used in conjunction with the REP prefix, this set of instructions requires the specified characters for
example: padding strings

 

The LODSB, LODSW and LODSD instructions load a value into AL/AX/EAX from the memory location pointed to by
ESI, and the value of the same ESI increases or decreases according to the value of the direction flag.
We rarely use the REP prefix with the LODS instruction, because
instead of loading to accumulation, generally only the LODS instruction is used to load a value. For example, each new value in the LODSB
overwrites the previous value, and the
instruction can replace the following two instructions (assuming the direction flag is cleared):
moval,[esi]; transfer a byte to AL
in esi; point down a byte

 


Each of the above instructions implicitly uses the ESI and EDI registers. It is critical
. In protected mode, the offset within the segment where ESI is DS is EDI. The offset within the segment
with ES is always the same as the value of DS and ES.
Repeat prefixes are used in strings. Very important, because the string can be manipulated multiple times with repeated prefixes

Example: Copy string.
In the following example, the MOVSB ​​instruction moves 10 bytes from string1 to string2. The repeat prefix first tests whether ECX is greater than 0 before executing the MOVSB ​​instruction . If ECX is equal to 0, the next instruction in the program is executed. If ECX>0, then decrement ECX by 1 and repeat the instruction:

The direction flag is very important: STD CLD
direction flag: The string command increases or decreases ESI and EDI according to the state of the direction flag (see Table 9.2). The direction flag can be explicitly modified by the CLD and STD commands:
CLD ;Clear the direction flag
STD ; Setting the direction flag
Forgetting to set the direction flag before executing the string operation instruction may cause very headaches.
Due to the uncertainty of the direction flag value during execution, the results of multiple executions may be very inconsistent.

 

 

Two-dimensional array

 

Strings and arrays are actually the same, except that the strings are in
the form of 0-terminated two-dimensional arrays.
From the perspective of an assembly language programmer, a two-dimensional array is a high-level abstraction of a one-dimensional array.
For the storage of rows and columns of two-dimensional arrays in memory, high-level languages ​​generally use the following two methods: row-majororder and column-majororder.
When using row-major order storage (most commonly used) , the first line is placed at the beginning of the memory block, the last element of the first line is followed by the first element of the second line. When using column-major sequence storage,
the elements of the first column are placed at the beginning of the memory block, and the last element of the first column is followed by the first element of the second column.
Row-majororder (row-majororder): that is, to calculate the address of a row, and then add the position of the column, remember that the index of the column must be multiplied by the type of the array, that is, WORD or DWORD or other

 

 

Base indexing operation:
The base-index operand adds the values ​​of two registers to obtain an offset address.
The two registers are called the base address (base) and the index (index). , in the following format: Square brackets in the format
[base + index] are required.
In 32-bit mode, the base and index sections can use any 32-bit general-purpose register; in
16-bit mode, the base register must be BX or BP (unless it is addressing data on the stack, it should be avoided as much as possible BP or EBP as the base register), the index register must be SI or DI.

The so-called base address indexing operation: that is, the base is the array offset + row offset, and the index is the column offset.
Relative base address indexing operation:
is it just an array variable name, but base is only a row offset, index is the column offset
format as follows:

 

 

In fact, the one-dimensional array of
array[row offset + column index multiplied by TYPE] is a bubble sort written in assembly language
of array[index*TYPE]

 

 

binary search

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325499583&siteId=291194637