Volume 1 Basic Architecture (794100), страница 47
Текст из файла (страница 47)
See “Jump if zero instructions” on page 7-25 for moreinformation about these instructions.Loop instructions — The LOOP, LOOPE (loop while equal), LOOPZ (loop while zero),LOOPNE (loop while not equal), and LOOPNZ (loop while not zero) instructions areconditional jump instructions that use the value of the ECX register as a count for thenumber of times to execute a loop. All the loop instructions decrement the count inthe ECX register each time they are executed and terminate a loop when zero isreached.
The LOOPE, LOOPZ, LOOPNE, and LOOPNZ instructions also accept the ZFflag as a condition for terminating the loop before the count reaches zero.The LOOP instruction decrements the contents of the ECX register (or the CX register,if the address-size attribute is 16), then tests the register for the loop-terminationcondition. If the count in the ECX register is non-zero, program control is transferredto the instruction address specified by the destination operand.
The destinationoperand is a relative address (that is, an offset relative to the contents of the EIPregister), and it generally points to the first instruction in the block of code that is tobe executed in the loop. When the count in the ECX register reaches zero, programcontrol is transferred to the instruction immediately following the LOOP instruction, which terminates the loop. If the count in the ECX register is zero when theLOOP instruction is first executed, the register is pre-decremented to FFFFFFFFH,causing the loop to be executed 232 times.The LOOPE and LOOPZ instructions perform the same operation (they aremnemonics for the same instruction).
These instructions operate the same as theLOOP instruction, except that they also test the ZF flag.If the count in the ECX register is not zero and the ZF flag is set, program control istransferred to the destination operand. When the count reaches zero or the ZF flag isclear, the loop is terminated by transferring program control to the instruction immediately following the LOOPE/LOOPZ instruction.7-24 Vol. 1PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONSThe LOOPNE and LOOPNZ instructions (mnemonics for the same instruction) operatethe same as the LOOPE/LOOPPZ instructions, except that they terminate the loop ifthe ZF flag is set.Jump if zero instructions — The JECXZ (jump if ECX zero) instruction jumps to thelocation specified in the destination operand if the ECX register contains the valuezero.
This instruction can be used in combination with a loop instruction (LOOP,LOOPE, LOOPZ, LOOPNE, or LOOPNZ) to test the ECX register prior to beginning aloop. As described in “Loop instructions on page 7-24, the loop instructions decrement the contents of the ECX register before testing for zero. If the value in the ECXregister is zero initially, it will be decremented to FFFFFFFFH on the first loop instruction, causing the loop to be executed 232 times. To prevent this problem, a JECXZinstruction can be inserted at the beginning of the code block for the loop, causing ajump out the loop if the EAX register count is initially zero.
When used with repeatedstring scan and compare instructions, the JECXZ instruction can determine whetherthe loop terminated because the count reached zero or because the scan or compareconditions were satisfied.The JCXZ (jump if CX is zero) instruction operates the same as the JECXZ instructionwhen the 16-bit address-size attribute is used. Here, the CX register is tested forzero.7.3.8.3Control Transfer Instructions in 64-Bit ModeIn 64-bit mode, the operand size for all near branches (CALL, RET, JCC, JCXZ, JMP,and LOOP) is forced to 64 bits. The listed instructions update the 64-bit RIP withoutneed for a REX operand-size prefix.Near branches in the following operations are forced to 64-bits (regardless ofoperand size prefixes):••••Truncation of the size of the instruction pointerSize of a stack pop or push, due to CALL or RETSize of a stack-pointer increment or decrement, due to CALL or RETIndirect-branch operand sizeNote that the displacement field for relative branches is still limited to 32 bits and theaddress size for near branches is not forced.Address size determines the register size (CX/ECX/RCX) used for JCXZ and LOOP.
Italso impacts the address calculation for memory indirect branches. Addresses size is64 bits by default, although it can be over-ridden to 32 bits (using a prefix).7.3.8.4Software Interrupt InstructionsThe INT n (software interrupt), INTO (interrupt on overflow), and BOUND (detectvalue out of range) instructions allow a program to explicitly raise a specified interrupt or exception, which in turn causes the handler routine for the interrupt or exception to be called.Vol. 1 7-25PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONSThe INT n instruction can raise any of the processor’s interrupts or exceptions byencoding the vector number or the interrupt or exception in the instruction.
Thisinstruction can be used to support software generated interrupts or to test the operation of interrupt and exception handlers.The IRET (return from interrupt) instruction returns program control from an interrupt handler to the interrupted procedure.
The IRET instruction performs a similaroperation to the RET instruction.The CALL (call procedure) and RET (return from procedure) instructions allow a jumpfrom one procedure to another and a subsequent return to the calling procedure.EFLAGS register contents are automatically stored on the stack along with the returninstruction pointer when the processor services an interrupt.The INTO instruction raises the overflow exception if the OF flag is set.
If the flag isclear, execution continues without raising the exception. This instruction allows software to access the overflow exception handler explicitly to check for overflow conditions.The BOUND instruction compares a signed value against upper and lower bounds,and raises the “BOUND range exceeded” exception if the value is less than the lowerbound or greater than the upper bound. This instruction is useful for operations suchas checking an array index to make sure it falls within the range defined for the array.7.3.8.5Software Interrupt Instructions in 64-bit Mode and CompatibilityModeIn 64-bit mode, the stack size is 8 bytes wide. IRET must pop 8-byte items off thestack.
SS:RSP pops unconditionally. BOUND is not supported.In compatibility mode, SS:RSP is popped only if the CPL changes.7.3.9String OperationsThe MOVS (Move String), CMPS (Compare string), SCAS (Scan string), LODS (Loadstring), and STOS (Store string) instructions permit large data structures, such asalphanumeric character strings, to be moved and examined in memory. Theseinstructions operate on individual elements in a string, which can be a byte, word, ordoubleword. The string elements to be operated on are identified with the ESI(source string element) and EDI (destination string element) registers.
Both of theseregisters contain absolute addresses (offsets into a segment) that point to a stringelement.By default, the ESI register addresses the segment identified with the DS segmentregister. A segment-override prefix allows the ESI register to be associated with theCS, SS, ES, FS, or GS segment register. The EDI register addresses the segmentidentified with the ES segment register; no segment override is allowed for the EDIregister.
The use of two different segment registers in the string instructions permitsoperations to be performed on strings located in different segments. Or by associating the ESI register with the ES segment register, both the source and destination7-26 Vol. 1PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONSstrings can be located in the same segment. (This latter condition can also beachieved by loading the DS and ES segment registers with the same segmentselector and allowing the ESI register to default to the DS register.)The MOVS instruction moves the string element addressed by the ESI register to thelocation addressed by the EDI register.
The assembler recognizes three “short forms”of this instruction, which specify the size of the string to be moved: MOVSB (movebyte string), MOVSW (move word string), and MOVSD (move doubleword string).The CMPS instruction subtracts the destination string element from the source stringelement and updates the status flags (CF, ZF, OF, SF, PF, and AF) in the EFLAGSregister according to the results. Neither string element is written back to memory.The assembler recognizes three “short forms” of the CMPS instruction: CMPSB(compare byte strings), CMPSW (compare word strings), and CMPSD (comparedoubleword strings).The SCAS instruction subtracts the destination string element from the contents ofthe EAX, AX, or AL register (depending on operand length) and updates the statusflags according to the results.
The string element and register contents are not modified. The following “short forms” of the SCAS instruction specify the operand length:SCASB (scan byte string), SCASW (scan word string), and SCASD (scan doublewordstring).The LODS instruction loads the source string element identified by the ESI registerinto the EAX register (for a doubleword string), the AX register (for a word string), orthe AL register (for a byte string). The “short forms” for this instruction are LODSB(load byte string), LODSW (load word string), and LODSD (load doubleword string).This instruction is usually used in a loop, where other instructions process eachelement of the string after they are loaded into the target register.The STOS instruction stores the source string element from the EAX (doublewordstring), AX (word string), or AL (byte string) register into the memory location identified with the EDI register.
The “short forms” for this instruction are STOSB (storebyte string), STOSW (store word string), and STOSD (store doubleword string). Thisinstruction is also normally used in a loop. Here a string is commonly loaded intothe register with a LODS instruction, operated on by other instructions, and thenstored again in memory with a STOS instruction.The I/O instructions (see Section 7.3.11, “I/O Instructions”) also perform operationson strings in memory.7.3.9.1Repeating String OperationsThe string instructions described in Section 7.3.9, “String Operations”, perform oneiteration of a string operation. To operate strings longer than a doubleword, thestring instructions can be combined with a repeat prefix (REP) to create a repeatinginstruction or be placed in a loop.When used in string instructions, the ESI and EDI registers are automatically incremented or decremented after each iteration of an instruction to point to the nextelement (byte, word, or doubleword) in the string. String operations can thus beginVol.