[Turn] assembly language: Movsb,movsw,movsd

Source: Internet
Author: User

Assembly language: Movsb,movsw,movsd

Transferred from: http://blog.csdn.net/zhenyongyuan123/article/details/8364011

Currently, the 80386 series of processors provide several sets of instructions for handling byte, Word, and double-word values, although these directives become basic string directives, but their usage is not limited to character arrays.

Instructions:
MOVSB, MOVSW, Movsd

Describe:
Moves the string data, copying the data at the memory address addressed by the ESI register to the memory address of the EDI addressing.


In the protected mode program, which is our 80386 default startup mode, ESI automatically acts as an offset within the segment that is addressed by DS, and EDI is automatically offset within the segment that is addressed by ES, and Es and DS always have the same values and cannot change them. (In the field site mode (8086) mode es and DS are manipulated by our programmers).


Let's take a look at these three instructions: MOVSB, MOVSW, Movsd, we can find that they are different from the last letter, but the last one is the size of the data byte we move each time. The first movsb is one byte at a time, followed by a word, followed by a double word. We can see it from its ' B ', ' W ', ' D '. b represents byte,w for Word, and D for DWORD.

The above 3 string manipulation instructions themselves can handle only one or a pair of values at a time, but if you add a repeating prefix, the instruction uses the ECX register as the counter to repeat. A repeating prefix can handle an entire array with just one instruction.

Repeat prefix Rep: repeat when ecx>0 (similar to our loop loop).


Looking at the above theory, let's have a demonstration.

CLD; direction sign
mov esi, memory address 1;(the memory address where the string is stored)
mov edi, memory address 2;(to be moved to the address at the destination)
mov ecx, the number to be moved;
Rep MOVSB

CLD is the incremental way to control the ESI and EDI when repeated moves.   CLD is the direction flag bit DF set to 0, each time the rep cycles, ESI and EDI automatically +1. STD is the direction flag bit DF is set to 1, each time the rep cycle, ESI and EDI auto-1.
The ECX register is reduced by 1 each time the rep loop prefix executes the loop. The same as our loop loop.

When we are doing string manipulation, we must remember to set the direction flag first, otherwise unpredictable results will occur.

[Turn] assembly language: Movsb,movsw,movsd

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.