Win32 compilation [29]-string commands: movs *, CMPs *, SCAs *, lods *, rep, repe, repne, etc.

Source: Internet
Author: User
Here the "string" is not a single string, including all consecutive data (such as arrays); the string command is only used for memory operations.
 
Move string commands: movsb, movsw, and movsd; from ESI-> EDI; after execution, the ESI and EDI address move the corresponding unit comparison string commands: cmpsb, cmpsw, cmpsd; compare ESI and EDI. After execution, the ESI and EDI addresses move the corresponding unit scan string commands: scasb, scasw, scasd; based on the data in Al/ax/eax, scan the data pointed to by EDI. After execution, EDI automatically changes the storage string commands: stosb, stosw, and stosd; store data in Al/ax/eax to the address given by EDI. After execution, EDI automatically loads data in a string of commands: lodsb, lodsw, and lodsd; load data directed by ESI to Al/ax/eax. After execution, ESI automatically changes B, W, and D to byte, word, and DWORD, respectively, the data size unit of each operation. the preceding command can have a repeated Prefix: When rep ECx> 0, repe (or repz) ECx> 0 and ZF = 1, repne (or repnz) ECx> 0 and ZF = 0; repeated prefixes can automatically decrease ECx by Unit (1, 2, 4)

  

Movsb: Move string

; Test29_1.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data szsource dB 'delphi 2010 ', 0 Len equ $-szsource-1 szdest dB Len DUP (?), 0. codemain proc Lea ESI, szsource Lea EDI, szdest mov ECx, Len CLD; reset sign register direction sign to let the string address from low to high rep movsb printstring szdest; Delphi 2010 retmain endpend main

  

In the above example, if you do not use the duplicate prefix...

; Test29_2.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data szsource dB 'delphi 2010 ', 0 Len equ $-szsource-1 szdest dB Len DUP (?), 0. codemain proc Lea ESI, szsource Lea EDI, szdest mov ECx, Len CLD: movsb dec ECx jnz @ B printstring szdest retmain endpend main

  

Movsd example:

; Export flat, stdcallinclude windows. incinclude unzip masm32.incinclude Debug. incincludelib unzip Debug. Lib. Data ddsource DD 11 h, 22 h, 33 H dddest dd lengthof ddsource DUP (?). Codemain proc Lea ESI, ddsource Lea EDI, dddest mov ECx, lengthof ddsource CLD rep movsd dumpmem offset dddest, sizeof dddest; 11 00 00 00-22 00 00 00-33 00 00 00 retmain endpend main

  

Movsw example:

; Mongoflat, stdcallinclude windows. incinclude unzip masm32.incinclude Debug. incincludelib unzip Debug. Lib. Data ddsource DW 11 h, 22 h, 33 H dddest DW lengthof ddsource DUP (?). Codemain proc Lea ESI, ddsource Lea EDI, dddest mov ECx, lengthof ddsource CLD rep movsw dumpmem offset dddest, sizeof dddest; 11 00 22 00-33 00 00 00 retmain endpend main

  

Cmpsd example:

 
; Test29_5.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data ddval1 dd 1234 H ddval dd 5678h. codemain proc Lea ESI, ddval1 Lea EDI, ddval cmpsd je L1 printtext 'two numbers are not equal to 'jmp l2l1: printtext' two numbers are equal to 'l2: retmain endpend main

  

Cmpsw example:

 
; Test29_6.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data dwarr1 DW 1, 2, 4, 5 dwarr2 DW 1, 3, 5, 7, 9. codemain proc Lea ESI, dwarr1 Lea EDI, dwarr2 mov ECx, lengthof dwarr1 CLD repe cmpsw je L1 printtext 'Two arrays are not equal to 'jmp l2l1: printtext' two arrays are equal 'l2: retmain endpend main

  

When comparing arrays, if the array length is inconsistent...

; Test29_7.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data dwarr1 DW 1, 2, 3, 4, 5 dwarr2 DW 1, 2, 3, 4, 5, 6. codemain proc Lea ESI, dwarr1 Lea EDI, dwarr2 mov ECx, lengthof dwarr1 CMP ECx, lengthof dwarr2 JNE L1 CLD repe cmpsw JNE L1 printtext 'two arrays are equal to 'jmp l2l1: printtext 'two arrays unequal 'l2: retmain endpend main

  

If the string ended with 0 is compared, you do not need to consider the inconsistent length.

; Test29_8.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data sztext1 dB 'delphi 100', 0 sztext2 dB 'delphi 100', 0. codemain proc Lea ESI, sztext1 Lea EDI, sztext2 mov ECx, lengthof sztext1 CLD repe cmpsb je L1 printtext 'strings are different 'jmp l2l1: text' strings are the same 'l2: retmain endpend main

  

Scasb example:

; Test29_9.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data sztext dB 'abcdefgh', 0. codemain proc Lea EDI, sztext mov Al, 'F' mov ECx, lengthof sztext-1 CLD repne scasb je L1 printtext' not found 'jmp l2l1: Sub ECx, lengthof sztext-1 neg ECx printdec ECx; if you can find it, the following characters are displayed: 6l2: retmain endpend main.

  

Stosb example:

; Test29_10.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data Len = 31 sztext dB Len DUP (0), 0. codemain proc Lea EDI, sztext mov Al, 'x' mov ECx, Len CLD rep stosb printstring sztext; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx retmain endpend main

  

Lodsw example: array summation

 
; Test29_11.asm.20.p.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. lib. data dwarr DW 1, 2, 4, 5, 6, 7, 8, 9, 10. codemain proc Lea ESI, dwarr mov ECx, lengthof dwarr XOR edX, edx xor eax, eax @: lodsw add edX, eax loop @ B printdec edX; 55 retmain endpend main

  

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.