Chapter7 String Operations

本文详细介绍了80x86架构中用于字符串操作的五种指令:MOVSB、CMPSB、SCASB、STOSB及LODSB,并解释了这些指令如何通过调整ESI和EDI寄存器来实现高效操作。文章还探讨了方向标志DF的作用以及如何使用CLD和STD指令来控制字符串处理的方向。

Five 80x86 instructions are designed for string manipulation:movs, cmps, scas, stos, and lods.

A string in the 80x86 architecture refers to a contiguous collections of bytes, words, doublewords in memory.They are commonly defined in a program's data segment.

尽量拣关键的说吧,这些个指令有一个特点,那便是:每执行一次,ESI 和/或 EDI就会发生变化,从而为下一次操作做好准备。具体怎么变化的呢?跟DF的值有关,DF=1的话,减;DF=0;加。减多少?加多少?由你的string元素决定,byte的话,变1;word的话,变2;doubleword的话,你知道的,4。那你可能还有一个疑问,就是它是加还是减怎么来确定呢?否则,我不知道该怎么操作了,因为可能是从右到左,也可能是从左到右呢。Good question!答案是:我们有两个指令cld和std专门用来reset和set DF的值,当然是在这些操作之前用的哦,亲。相信你已经看出来它们的作用了,cld,不就是clear df吗?std,不就是set df吗?哈哈,没错,一个复位,一个设1。当然,这两条指令都只会影响flag DF值。

接下来,It is time to present all details about the string insructions 了。

1.movs

The source element at address DS:ESI is copied to destination element at address ES:EDI. After the string element is copied, both index registers are changed by element size(1,2,4), incremented if the direction flag DF is 0 or decremented if DF is 1.The movs instruction does affect any falg. It comes in movsb, movsw, and movsd versions. 为什么呢?因为你看我们并没有些源操作数和目的操作数对吧?那么这就会产生一个问题,我移的到底是byte,word,还是doubleword呢?有两种方法:1.补上操作数,当然它们的size必须是明确的,不然没用嘛!2.就是汉字前面的一句话,加后缀。

2.cmps

我们一直在强调学习是需要联想的,那看到这个指令是不是想到了cmp指令呢?好吧,类似的我们可以给出cmps指令就是把两个字符串中的元素相减,然后呢flags are set based on the difference;neither operand is changed.

3.scas

The scan string instruction scas is used to scan a string for the presence or absence of a particular string element.这条指令呢只有一个操作数,也就是目的操作数。如果用 scasb的话,那么the element searched for is the byte in the AL register; 如果用scasw的话,就是AX中的元素了;如果用scasw,你知道的EAX。After the search,the destination index EDI will be one greater than desired since a string instruction always increments index registers whether or not falgs were set.然后呢,如果成功找到和这个元素一样的元素的话,那么EDI里面含的就是那个元素的下一个元素的地址。

4.stos

The store string instruction stos stos copies a byte, a word, or a doubleword from AL, AX, or EAX to an element of a destination string. It affects no flags, so that when it is repeated with rep, it copies the same value into consecutive positions of a string.

5.lods

The load string instrution lods copies a source string element to the AL, AX, or EAX register, depending on the string element size. A lods instruction sets no flag. 你可以用一个rep指令在它的前面但是木有神马太大作用的,除了最后一个值以外其他的都被替换了。但是如果及时把值导出的话,就相当的有用了,对吧?

最后回过头来看下好了,要是我要用这些指令操作整个字符串的元素的话,是不是会很让人纠结啊?得不停地重复!好吧,我表示我们目前能想到的绝大多数问题前辈们都已经想过并给出解决方法了,所以啊,让我们一起来膜拜和学习大神吧!所以以后咱要是也在某些领域做出了卓越贡献的话,要记得Newton老爷子说的一句话:俺们是站在巨人的肩膀上的!-------好吧,就像前面我写了rep一样,我们有rep,repz/repe, repnz/repne这些指令哪。再看一下它们的"loop while"好了,相当于复习下。

rep ECX>0

repz/repe ECX>0 and ZF=1

repnz/repne ECX>0 and ZF=0

就这么多了,O啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值