Chapter8 Bit Manipulation

来源:互联网 发布:淘宝上的hiv试纸准确吗 编辑:程序博客网 时间:2024/05/16 05:12

首先我们来看几条比较熟悉的指令。

and    destination, source(实现某位置0)

or   destination, source(实现某位置1)

xor   destination, source(某位跟1异或取反,跟0异或是自身,本身异或为清0,如xor ax, ax)

not   destination(取反)

1.The results of these operations are put into the destination.

2.The not instruction does not affect any flags.

3.The carry flag CF and the overflow flag OF are both reset to 0.

4.The other flags like SF, ZF, PF are set or reset according to the value of the result of the operation.

test   destination, source

It has the same functions as the and insturction except that destination operand are not changed. That is to say the only job of a test is to set flags.(可联系cmp和sub指令)

它的用处主要有:

1.Examine a particular bit of a byte or a word.比如test  dx, 2000h

就是用来测试寄存器dx  bit13的情况。通常来讲这条指令后面总是跟着一个jz或者jnz指令,而它的结果就是使得往目的地成功跳转如果bit13相应的为0或者1。

2.Get information about a value in a register.For example, 

test    cx, cx    这条指令的结果还是自身,但是它改变了flags的值,从中我们可以得到一些信息。有人说那and cx, cx不也可以实现这么一个功能吗?木有错,但是用test看起来就说明我们此操作的目的就只是测试test不是吗?



接着我们看下移位操作指令,这个如果不弄清楚的话会很容易头晕的。

                        左     右

      logical: shl shr

arithmetic:     sal sar

The source code format of any shift instruction is

s-detination, count

There are three versions of the count operand.

1.Number 1.

2.Another number serving as a byte-size immediate operand.

3.The register specification cl.

看完了基本格式后再看下具体的一些情况。

1.算术左移和逻辑左移是完全一样的。

2.SF, ZF,PF 根据最终得到结果赋值,OF指令只对single-bit才有用,它的值是由你那标志位有木有改变决定的,如果改变了它就为1,否则为0。

3.When a left shift instruction is executed, the bits in the destination slide to the left and 0 bits fill in on the right. The bits that fall of the left are lost except for the very last one shifted off, it is saved in the carry flag CF.

4.算术右移和逻辑右移是不完全一样的。

相同之处在于:With both, the bits in the destination slide to the right and the bits that fall off the right are lost except for the very last one shifted off, which is saved in CF.

不同之处在于:逻辑右移左边那些位直接添0就好了,而算术右移则是添原来标志位sign bit的值。


sh-d   detination, source, cx/immediate

移出操作跟一般的移位操作一样,不同之处在于添的内容是源操作数里的内容(位)。比如说往左移吧,那么往右添的就是源操作数左边相应的那些位;右移则在左边添上源操作数右边相应的那几位。最终结果保留在destination里面,source operand 保持不变。相应标志位的改变跟前面所说的完全一致。


ro- destination, count

循环移位跟single-bit shift基本相同,除了:

1.腾出来的位和CF值均由抛掉的位来填充。

2.影响的标志位仅CF和OF;OF值变化和前面的一致,只对single-bit rotate shift有效。


rc- destination, count     (提醒联系带进位的加法)

它跟ro-的唯一区别就是把CF值值放进了循环,也就是说,移出的位先移向CF,完了CF的原值(就是被移进之前的值)移向腾出的那个位。