s03_04.htm

来自「Programmer s Reference Manual is an impr」· HTM 代码 · 共 546 行 · 第 1/2 页

HTM
546
字号
from the left side of the operand moves to CF before it returns to theoperand as the low-order bit on the next rotation cycle. See <A HREF="#fig3-14">Figure 3-14</A>  .<P><A HREF="RCL.htm">RCR</A> (Rotate Through Carry Right) rotates bits in the byte, word, ordoubleword destination operand right by one or by the number of bitsspecified in the count operand (an immediate value or the value contained inCL).<P>This instruction differs from <A HREF="RCL.htm">ROR</A> in that it treats CF as a low-orderone-bit extension of the destination operand. Each low-order bit that exitsfrom the right side of the operand moves to CF before it returns to theoperand as the high-order bit on the next rotation cycle. See <A HREF="#fig3-15">Figure 3-15</A>  .<P><A NAME="fig3-10"><IMG align=center SRC="fig3-10.gif" border=0><P><HR><P><A NAME="fig3-11"><IMG align=center SRC="fig3-11.gif" border=0><P><HR><P><A NAME="fig3-12"><IMG align=center SRC="fig3-12.gif" border=0><P><HR><P><A NAME="fig3-13"><IMG align=center SRC="fig3-13.gif" border=0><P><HR><P><A NAME="fig3-14"><IMG align=center SRC="fig3-14.gif" border=0><P><HR><P><A NAME="fig3-15"><IMG align=center SRC="fig3-15.gif" border=0><P><H3>3.4.4.4  Fast "BIT BLT" Using Double Shift Instructions</H3>One purpose of the double shifts is to implement a bit string move, witharbitrary misalignment of the bit strings.  This is called a "bit blt" (BITBLock Transfer.)  A simple example is to move a bit string from an arbitraryoffset into a doubleword-aligned byte string.  A left-to-right string ismoved 32 bits at a time if a double shift is used inside the move loop.<PRE>MOV   ESI,ScrAddrMOV   EDI,DestAddrMOV   EBX,WordCntMOV   CL,RelOffset      ; relative offset Dest-SrcMOV   EDX,[ESI]         ; load first word of sourceADD   ESI,4             ; bump source addressBltLoop:LODS                    ; new low order partSHLD  EDX,EAX,CL        ; EDX overwritten with aligned stuffXCHG  EDX,EAS           ; Swap high/low order partsSTOS                    ; Write out next aligned chunkDEC   EBXJA    BltLoop</PRE>This loop is simple yet allows the data to be moved in 32-bit pieces forthe highest possible performance. Without a double shift, the best that canbe achieved is 16 bits per loop iteration by using a 32-bit shift andreplacing the <A HREF="XCHG.htm">XCHG</A> with a <A HREF="RCL.htm">ROR</A> by 16 to swap high and low order parts ofregisters. A more general loop than shown above would require some extramasking on the first doubleword moved (before the main loop), and on thelast doubleword moved (after the main loop), but would have the same basic32-bits per loop iteration as the code above.<H3>3.4.4.5  Fast Bit-String Insert and Extract</H3>The double shift instructions also enable:<UL><LI> Fast insertion of a bit string from a register into an arbitrary bitlocation in a larger bit string in memory without disturbing the bitson either side of the inserted bits.<LI> Fast extraction of a bits string into a register from an arbitrary bitlocation in a larger bit string in memory without disturbing the bitson either side of the extracted bits.</UL>The following coded examples illustrate bit insertion and extraction undervariousconditions:<OL><LI>Bit String Insert into Memory (when bit string is 1-25 bits long,i.e., spans four bytes or less):<PRE>; Insert a right-justified bit string from register into; memory bit string.;; Assumptions:; 1) The base of the string array is dword aligned, and; 2) the length of the bit string is an immediate value;    but the bit offset is held in a register.;; Register ESI holds the right-justified bit string; to be inserted.; Register EDI holds the bit offset of the start of the; substring.; Registers EAX and ECX are also used by this; "insert" operation.;MOV   ECX,EDI      ; preserve original offset for later useSHR   EDI,3        ; signed divide offset by 8 (byte address)AND   CL,7H        ; isolate low three bits of offset in CLMOV   EAX,[EDI]strg_base      ; move string dword into EAXROR   EAX,CL       ; right justify old bit fieldSHRD  EAX,ESI,length          ; bring in new bitsROL   EAX,length   ; right justify new bit fieldROL   EAX,CL       ; bring to final positionMOV   [EDI]strg_base,EAX      ; replace dword in memory</PRE><LI>Bit String Insert into Memory (when bit string is 1-31 bits long, i.e.spans five bytes or less):<PRE>; Insert a right-justified bit string from register into; memory bit string.;; Assumptions:; 1) The base of the string array is dword aligned, and; 2) the length of the bit string is an immediate value;    but the bit offset is held in a register.;; Register ESI holds the right-justified bit string; to be inserted.; Register EDI holds the bit offset of the start of the; substring.; Registers EAX, EBX, ECX, and EDI are also used by; this "insert" operation.;MOV   ECX,EDI     ; temp storage for offsetSHR   EDI,5       ; signed divide offset by 32 (dword address)SHL   EDI,2       ; multiply by 4 (in byte address format)AND   CL,1FH      ; isolate low five bits of offset in CLMOV   EAX,[EDI]strg_base      ; move low string dword into EAXMOV   EDX,[EDI]strg_base+4    ; other string dword into EDXMOV   EBX,EAX     ; temp storage for part of string     + rotateSHRD  EAX,EDX,CL  ; double shift by offset within dword + EDX:EAXSHRD  EAX,EBX,CL  ; double shift by offset within dword + rightSHRD  EAX,ESI,length          ; bring in new bitsROL   EAX,length  ; right justify new bit fieldMOV   EBX,EAX     ; temp storage for part of string         + rotateSHLD  EAX,EDX,CL  ; double shift back by offset within word + EDX:EAXSHLD  EDX,EBX,CL  ; double shift back by offset within word + leftMOV   [EDI]strg_base,EAX      ; replace dword in memoryMOV   [EDI]strg_base+4,EDX    ; replace dword in memory</PRE><LI>Bit String Insert into Memory (when bit string is exactly 32 bitslong, i.e., spans five or four types of memory):<PRE>; Insert right-justified bit string from register into; memory bit string.;; Assumptions:; 1) The base of the string array is dword aligned, and; 2) the length of the bit string is 32;    but the bit offset is held in a register.;; Register ESI holds the 32-bit string to be inserted.; Register EDI holds the bit offset of the start of the; substring.; Registers EAX, EBX, ECX, and EDI are also used by; this "insert" operation.;MOV   EDX,EDI     ; preserve original offset for later useSHR   EDI,5       ; signed divide offset by 32 (dword address)SHL   EDI,2       ; multiply by 4 (in byte address format)AND   CL,1FH      ; isolate low five bits of offset in CLMOV   EAX,[EDI]strg_base      ; move low string dword into EAXMOV   EDX,[EDI]strg_base+4    ; other string dword into EDXMOV   EBX,EAX     ; temp storage for part of string     + rotateSHRD  EAX,EDX     ; double shift by offset within dword + EDX:EAXSHRD  EDX,EBX     ; double shift by offset within dword + rightMOV   EAX,ESI     ; move 32-bit bit field into positionMOV   EBX,EAX     ; temp storage for part of string         + rotateSHLD  EAX,EDX     ; double shift back by offset within word + EDX:EAXSHLD  EDX,EBX     ; double shift back by offset within word + leftMOV   [EDI]strg_base,EAX      ; replace dword in memoryMOV   [EDI]strg_base,+4,EDX   ; replace dword in memory</PRE><LI>Bit String Extract from Memory (when bit string is 1-25 bits long,i.e., spans four bytes or less):<PRE>; Extract a right-justified bit string from memory bit; string into register;; Assumptions:; 1) The base of the string array is dword aligned, and; 2) the length of the bit string is an immediate value;    but the bit offset is held in a register.;; Register EAX holds the right-justified, zero-padded; bit string that was extracted.; Register EDI holds the bit offset of the start of the; substring.; Registers EDI, and ECX are also used by this "extract.";MOV  ECX,EDI      ; temp storage for offsetSHR  EDI,3        ; signed divide offset by 8 (byte address)AND  CL,7H        ; isolate low three bits of offsetMOV  EAX,[EDI]strg_base       ; move string dword into EAXSHR  EAX,CL       ; shift by offset within dwordAND  EAX,mask     ; extracted bit field in EAX</PRE><LI>Bit String Extract from Memory (when bit string is 1-32 bits long,i.e., spans five bytes or less):<PRE>; Extract a right-justified bit string from memory bit; string into register.;; Assumptions:; 1) The base of the string array is dword aligned, and; 2) the length of the bit string is an immediate;    value but the bit offset is held in a register.;; Register EAX holds the right-justified, zero-padded; bit string that was extracted.; Register EDI holds the bit offset of the start of the; substring.; Registers EAX, EBX, and ECX are also used by this "extract."MOV   ECX,EDI     ; temp storage for offsetSHR   EDI,5       ; signed divide offset by 32 (dword address)SHL   EDI,2       ; multiply by 4 (in byte address format)AND   CL,1FH      ; isolate low five bits of offset in CLMOV   EAX,[EDI]strg_base      ; move low string dword into EAXMOV   EDX,[EDI]strg_base+4    ; other string dword into EDXSHRD  EAX,EDX,CL  ; double shift right by offset within dwordAND   EAX,mask    ; extracted bit field in EAX</PRE></OL><H2>3.4.5  Byte-Set-On-Condition Instructions</H2>This group of instructions sets a byte to zero or one depending on any ofthe 16 conditions defined by the status flags. The byte may be in memory ormay be a one-byte general register. These instructions are especially usefulfor implementing Boolean expressions in high-level languages such as Pascal.<P><A HREF="SETcc.htm">SETcc</A> (Set Byte on Condition cc) set a byte to one if condition cc is true;sets the byte to zero otherwise. Refer to Appendix D for a definition ofthe possible conditions.<H2>3.4.6  Test Instruction</H2><A HREF="TEST.htm">TEST</A> (Test) performs the logical "and" of the two operands, clears OF andCF, leaves AF undefined, and updates SF, ZF, and PF. The flags can be testedby conditional control transfer instructions or by the byte-set-on-conditioninstructions. The operands may be doublewords, words, or bytes.<P>The difference between <A HREF="TEST.htm">TEST</A> and <A HREF="AND.htm">AND</A> is that <A HREF="TEST.htm">TEST</A> does not alter the destination operand. <A HREF="TEST.htm">TEST</A> differs from <A HREF="BT.htm">BT</A> in that <A HREF="TEST.htm">TEST</A> is useful for testingthe value of multiple bits in one operations, whereas <A HREF="BT.htm">BT</A> tests a single bit.<P><HR><P><B>up:</B> <A HREF="c03.htm">Chapter 3 -- Applications Instruction Set</A><BR><B>prev:</B> <A HREF="s03_03.htm">3.3  Decimal Arithmetic Instructions</A><BR><B>next:</B> <A HREF="s03_05.htm">3.5  Control Transfer Instructions</A></BODY>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?