330 likes | 649 Views
Chapter Four 80x86 Instruction Set ( 6 ). String Instructions. The 80x86 supports twelve string instructions: movs (move string) lods (load string element into the accumulator) stos (store accumulator into string element)
E N D
Chapter Four80x86 Instruction Set(6) 2014年9月24日1
String Instructions The 80x86 supports twelve string instructions: • movs (move string) • lods (load string element into the accumulator) • stos (store accumulator into string element) • scas (Scan string and check for match against the value in the accumulator) • cmps (compare two strings) • ins (input a string from an I/O port) • outs (output a string to an I/O port • rep (repeat a string operation) • repz (repeat while zero) • repe (repeat while equal) • repnz (repeat while not zero) • repne (repeat while not equal) 2014年9月24日2
String Instructions • These instructions can operate on strings of bytes, words, or double words. To specify the object size, simply append a b, w, or d to the end of the instruction’s mnemonic, i.e., lodsb, movsw, cmpsd, etc. Of course, the double word forms are only available on 80386 and later processors. 2014年9月24日3
String Instructions • The movs and cmps instructions assume that ds:si contains the segmented address of a source string and that es:di contains the segmented address of a destination string. • Thelods instruction assumes that ds:si points at a source string, the accumulator (al/ax/eax) is the destination location. • The scas and stos instructions assume that es:di points at a destination string and the accumulator contains the source value. 2014年9月24日4
String Instruction Basics • Source DS:SI, Destination ES:DI • You must ensure DS and ES are correct • You must ensure SI and DI are offsets into DS and ES respectively • Direction Flag (0 = Up, 1 = Down) • CLD - Increment addresses (left to right) • STD - Decrement addresses (right to left) 2014年9月24日5
Moving (Copying) • MOVSB, MOVSW • Memory to memory copy of a byte or word • Each execution of this instruction causes • Copy byte/word at DS:SI to ES:DI • Inc/Dec SI and DI by 1 or 2 • If CX contains a repetition factor • REP MOVSB or REP MOVSW will automatically execute the move [CX] times, and CX becomes 0 2014年9月24日6
Example: Copy a String ;Copy array a to b, assume ES=DS, and 10 bytes are to be copied mov cx, 10 ;10 bytes to copy mov di, offset b ;destination mov si, offset a ;source cld ;left to right rep movsb 2014年9月24日7
Example: Memory Shift ;shift bytes of a 3 bytes to right mov cx, 7 ;bytes to copy mov di, offset a+9 ;dest mov si, offset a+9-3 ;source std ;copy from right to left rep movsb SI DI a 2014年9月24日8
Example: Replication pattern db "!@#*" ;duplicate db (100-4) dup (?) ;space mov cx,100-4 ;96 bytes to copy mov si, offset pattern mov di, offset pattern+4 cld ;destructive overlap rep movsb DI SI ! @ # * a 2014年9月24日9
STOSB, STOSW Copy AL or AX into an array of bytes or words destination ES:DI Each repetition Increments or Decrements DI depends on DF Commonly used with REP prefix and number of repetitions in CX The Word version byte reverses AX as usual Store String 2014年9月24日10
Example: Initilializing Storage arr dw 200 dup (?) ;empty words ;to be initialized to A050A050... mov ax,50A0h mov di,offset arr mov cx,200 ;array size cld stosw DI AX 50 A0 A0 50 A0 50 arr 2014年9月24日11
Load String • LODSB, LODSW • Byte or word at DS:SI is copied into AL or AX • SI is increased or decreased by 1 or 2 • This is commonly paired with STOSx in a loop to process each component of an array • There is no reason to use REP with this instruction 2014年9月24日12
Example: Process Array ;array b = toUpper(array a) mov di, offset b ;dest mov si, offset a ;source mov cx,30 ;array size cld ;left to right processing lp: lodsb ;get next byte and al,0DFh ;to upper case stosb ;store at next location loop lp 2014年9月24日13
Scan String SCASB, SCASW • Compares AL or AX with ES:DI and auto increments or decrements DI • This instruction sets the flags register • Flags set according to result of compare • Used in a loop, or with conditional REPs • REPZ, REPE, REPNZ, REPNE 2014年9月24日14
while (CX != 0 ) { do string primitive --CX if (REPNE and ZF == 1) exit loop if (REPE and ZF == 0) exit loop } The test for CX is at the top of the loop Test of zero flag is at the end of the loop. Only CMPS and SCAS instructions can affect the ZF value Conditional Repeats for SCASx and CMPSx 2014年9月24日15
Example: String Search arr db 'abcdefghijklmnopqrstuvwxyz' mov di, offset arr mov cx,26 ; 26 bytes cld ;left to right processing mov al,target ;ch to find repne scasb ;search for match ;make at most cx comparisons jne nomatch ;ZF never set ;match occurred at ES:[di-1] ;di is incremented even if match 2014年9月24日16
Compare String CMPSB, CMPSW • Compares DS:SI to ES:DI, setting flags and auto-increments/decrements SI and DI • Used to compare arrays of words or arrays of bytes • Typically used with conditional REP instruction 2014年9月24日17
Example: String Compare mov si, offset str1 mov di, offset str2 cld ;left to right processing mov cx, 12 ;shorter string repe cmpsb ;cmp til <> or cx=0 jl str1smaller jg str2smaller ;the strings are equal - so far ;if sizes different, shorter string is less 2014年9月24日18
String Instructions • 例410:将数据段首地址为string1的10个字符传送到附加数据段内,并将其从附加数据段内读出显示。 2014年9月24日19
String Instructions • 例411:内存中以BUFFER为首地址的内存单元中有10个非压缩BCD码形式存放的十进制数,将这些数顺序在屏幕上显示。 2014年9月24日20
String Instructions • 例413:从键盘输入一个字符串string1(使用0A号DOS功能调用实现),将该数据块传送到string2,并将小写变成大写,遇到回车符结束。 2014年9月24日21
Program Flow Control Instructions • The instructions discussed thus far execute sequentially; that is, the CPU executes each instruction in the sequence it appears in your program. To write real programs requires several control structures, not just the sequence. Examples include the if statement, loops, and subroutine invocation (a call). • Since compilers reduce all other languages to assembly language, it should come as no surprise that assembly language supports the instructions necessary to implement these control structures. • 80x86 program control instructions belong to three groups: unconditional transfers, conditional transfers, and subroutinecallandreturn instructions. The following sections describe these instructions: 2014年9月24日22
Unconditional Jumps • The jmp (jump) instruction unconditionally transfers control to another point in the program. There are six forms of this instruction: • an intersegment/direct jump, • two intrasegment/direct jumps, • an intersegment/indirect jump, • and two intrasegment/indirect jumps. • Intrasegment jumps are always between statements in the same code segment. • Intersegment jumps can transfer control to a statement in a different code segment. • These instructions generally use the same syntax, it is jmp target 2014年9月24日23
The CALL and RET Instructions • The call and ret instructions handle subroutine calls and returns. There are five different call instructions and six different forms of the return instruction: 2014年9月24日24
The Conditional Jump Instructions • Although the jmp, call, and ret instructions provide transfer of control, they do not allow you to make any serious decisions. The 80x86’s conditional jump instructions handle this task. The conditional jump instructions are the basic tool for creating loops and other conditionally executable statements like the if..then statement. 2014年9月24日25
The Conditional Jump Instructions • The conditional jumps test one or more flags in the flags register to see if they match some particular pattern . If the pattern matches, control transfers to the target location. If the match fails, the CPU ignores the conditional jump and execution continues with the next instruction. Some instructions, for example, test the conditions of the sign, carry, overflow, and zero flags. For example, after the execution of a shift left instruction, you could test the carry flag to determine if it shifted a one out of the H.O. bit of its operand. Likewise, you could test the condition of the zero flag after a test instruction to see if any specified bits were one. Most of the time, however, you will probably execute a conditional jump after a cmp instruction. The cmp instruction sets the flags so that you can test for less than, greater than, equality, etc. 2014年9月24日26
The Conditional Jump Instructions 2014年9月24日27
The Conditional Jump Instructions 2014年9月24日28
The Conditional Jump Instructions 2014年9月24日29
The Conditional Jump Instructions 例417: • 在以DATA1为首地址的内存数据段中,存放了10个8位的无符号数,是将其中的最大数和最小数找出来,并存入max和min单元中。 2014年9月24日30
本章中文作业 • 中文教材149-151 • 第1题 • 第2题 • 第3题 • 第6题 • 第9题 • 第11题 • 第12题 2014年9月24日31
本章英文作业 1. Which flag(s) does the 80x86 use to check for unsigned arithmetic overflow? 2. Which flag(s) let you check for signed overflow?. 3. Which flag(s) does the 80x86 use to test the following unsigned conditions? How must the flags be set for the condition to be true? a) equal b) not equal c) less than d) less than or equal e) greater than f) greater than or equal 4. Repeat the above question for a signed comparison. 5. What instruction is CMP most similar to? 6. What instruction is TEST most similar to? 2014年9月24日32