Linux Platform x86 compilation (12): string storage and loading

Source: Internet
Author: User

"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
Storing and loading strings refers to loading the value of a string into a register and passing it back to the memory location.        It uses directives lods directives and stos directives. The lods instruction is used to transfer in-memory string values into the EAX register, which has three different formats: LODSB (1 bytes), LODSW (2 bytes), LODSL (4 bytes). The lods instruction uses the ESI register as the implied source operand.         The ESI register must contain the memory address where the string to be loaded resides. After you have stored the string value in the EAX register using the lods instruction, you can use the STOs directive to store it in another memory location. STOs also has 3 formats: STOSB, STOSW, Stosl. The stos instruction uses the EDI register as the implied target operand, and when the stos instruction is executed, the value of the EDI register is incremented or decremented by length. The best use of the STOs directive is to work with the rep directive, copying a string value into a large string value several times, such as copying a space character to a 256-byte buffer area. The following example:
# stos.s.section. DataSpace:    . ASCII "". Section. BSS    . Lcomm buffer, 256.section. Text.globl _start_start:    NOP    Leal Space,%esi    Leal buffer,%edi    movl $256,%ecx    cld    LODSB    Rep STOSB    Movl $,%eax< C10/>MOVL $,%ebx    int $0x80
After make debug run as follows:
Breakpoint 1, _start () at stos.s:1111    NOP (gdb) print/x $eax $ = 0x0 (gdb) x/16b &buffer0x80490a0 <buffer>: 0 x00 0x00 0x00 0x00 0x00 0x00 0x00 0x000x80490a8 <buffer+8>: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 (gdb) s12    Lea L Space,%esi (GDB) S13    Leal Buffer,%edi (gdb) S14    movl $256,%ecx (gdb) s16    CLD (gdb) s17 LODSB    (gdb) s18
   rep STOSB (gdb) print/x $eax $4 = 0x20 (gdb) S20 MOVL $    ,%eax (gdb) S21    movl $,%ebx (GDB) x/16b &buffer0x80490 A0 <buffer>: 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x200x80490a8 <buffer+8>: 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0 X20 (GDB)
The output shows that the program has reached our expectation that after the STOSB instruction is executed, the memory location buffer contains spaces.

Linux Platform x86 compilation (12): string storage and loading

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.