A piece of assembly code learning CLD lodsb stosw

Source: Internet
Author: User

CLD and STD are used to operate the Direction Flag DF (Direction Flag ). CLD resets DF, that is, df = 0, and STD places DF, that is, df = 1. It is used in string operation commands.

In assembly language, the string operation command lodsb/lodsw is a block loading command. The specific operation is to read the storage unit pointed to by Si into the accumulators, where lodsb is read into Al and lodsw is read into ax, then Si automatically increases or decreases by 1 or 2 bits. when the Direction Flag D is 0, the Si increases automatically; when D is 1, the SI decreases automatically.

STOs
Stosb
Stosw
Stosd

Store string data)

Store the content of the accumulators to the memory address addressed by ES: E (DI). If STOs is used, the destination operand must be specified.

Stosb copies Al to memory, stosw copies ax to memory, and stosd copies eax to memory.

Intel instruction sets contain five groups of instructions that process bytes, words, and double-word groups, which are called Basic string commands, but their usage is not

Limited by character arrays, stosw and lodsb in LZ belong to these five groups (the other three groups are movsb, cmpsb, scasb)

The following code is reproduced in Luo yunbin's Win32 Compilation

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>

Find a line of data in the buffer, process line breaks, and save UNIX text file-> PC text file
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>
_ Formattext proc uses ESI _ lpdata, _ dwsize, _ hfile
Local @ szbuffer [128]: byte, @ dwbyteswrite

MoV ESI, _ lpdata
MoV ECx, _ dwsize
Lea EDI, @ szbuffer
XOR edX, EDX
ClD
_ Loopbegin:
Or ECX, ECx
JZ _ writeline
Lodsb
Dec ECx
CMP Al, 0dh; discarded when 0dh is encountered
JZ _ loopbegin
CMP Al, 0ah; When 0ah is encountered, it is extended to 0dh, 0ah
JZ _ lineend
Stosb
INC edX
CMP edX, sizeof @ szBuffer-2
Jae _ writeline; save if the row buffer is full
JMP _ loopbegin
_ Lineend:
MoV ax, 0a0dh; pay attention to this place after saving Al to 0dh ah to 0ah
Stosw
INC edX
INC edX
_ Writeline:
Push ECx
. If edX
Invoke writefile, _ hfile, ADDR @ szbuffer, EDX, ADDR @ dwbyteswrite, null
. Endif
Lea EDI, @ szbuffer
XOR edX, EDX
Pop ECx
Or ECX, ECx
Jnz _ loopbegin
RET

_ Formattext endp

This subroutine puts the string to be filtered into ESI, stores the string to be stored in EDI, and uses CLD to set the growth direction of esi edi to add a positive direction.

After lodsb is used to retrieve the data from ESI to Al, it adds the required execution stosw ESI to 1, and then copies ax to EDI to implement string processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.