2023年4月19日 星期三 1
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Chapter Four
80x86 Instruction Set( 6 )
2023年4月19日 星期三 2
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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 accumul
ator)• 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)
2023年4月19日 星期三 3
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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.
2023年4月19日 星期三 4
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
String Instructions
• The movsmovs and cmps 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.
• The lodslods instruction assumes that ds:si points at a source string, the accumulator (al/ax/eax) is the destination location.
• The scasscas and stosstos instructions assume that es:di points at a destination string and the accumulator contains the source value.
2023年4月19日 星期三 5
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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)
2023年4月19日 星期三 6
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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
2023年4月19日 星期三 7
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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
2023年4月19日 星期三 8
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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 DI
a
SI
2023年4月19日 星期三 9
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Example: Replication
pattern db "!@#*" ;duplicatedb (100-4) dup (?) ;space mov cx,100-4 ;96 bytes to copymov
si, offset patternmov di, offset pattern+4cld ;destructive overlaprep movsb
! @ # *
DI
a
SI
2023年4月19日 星期三 10
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Store String
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
2023年4月19日 星期三 11
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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
stoswA0 50 A0 50
arr
50 A0AXDI
2023年4月19日 星期三 12
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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
2023年4月19日 星期三 13
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Example: Process Array
;array b = toUpper(array a)mov di, offset b ;destmov si, offset a ;sourcemov cx,30 ;array size cld
;left to right processinglp:lodsb ;get next byteand al,0DFh ;to upper casestosb ;store at next locationlooplp
2023年4月19日 星期三 14
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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
2023年4月19日 星期三 15
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Conditional Repeats for SCASx and CMPSx
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
2023年4月19日 星期三 16
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Example: String Search
arr db 'abcdefghijklmnopqrstuvwxyz'mov di, offset arrmov cx,26 ; 26 bytescld ;left to right processingmov al,target ;ch to findrepne scasb ;search for match
;make at most cx comparisonsjne nomatch ;ZF never set
;match occurred at ES:[di-1];di is incremented even if match
2023年4月19日 星期三 17
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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
2023年4月19日 星期三 18
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Example: String Compare
mov si, offset str1mov di, offset str2cld ;left to right processingmov cx, 12 ;shorter stringrepe cmpsb ;cmp til <> or cx=0jl str1smallerjg str2smaller
;the strings are equal - so far;if sizes different, shorter string is less
2023年4月19日 星期三 19
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
String Instructions
• 例 410:将数据段首地址为 string1的 10个字符传送到附加数据段内,并将其从附加数据段内读出显示。
2023年4月19日 星期三 20
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
String Instructions
• 例 411:内存中以 BUFFER为首地址的内存单元中有 10个非压缩 BCD码形式存放的十进制数,将这些数顺序在屏幕上显示。
2023年4月19日 星期三 21
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
String Instructions
• 例 413:从键盘输入一个字符串 string1(使用 0A号 DOS功能调用实现),将该数据块传送到 string2,并将小写变成大写,遇到回车符结束。
2023年4月19日 星期三 22
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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 transfersunconditional transfers, conditional transfersconditional transfers, and subroutinesubroutine callcall andand return instructionsreturn instructions. The following sections describe these instructions:
2023年4月19日 星期三 23
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
Unconditional Jumps• The jmp (jump) instruction unconditionally transfers control to anothe
r 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 s
egment.• Intersegment jumps can transfer control to a statement in a different co
de segment.• These instructions generally use the same syntax, it is jmp targetjmp target
2023年4月19日 星期三 24
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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:
2023年4月19日 星期三 25
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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.
2023年4月19日 星期三 26
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
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.
2023年4月19日 星期三 27
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
The Conditional Jump Instructions
2023年4月19日 星期三 28
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
The Conditional Jump Instructions
2023年4月19日 星期三 29
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
The Conditional Jump Instructions
2023年4月19日 星期三 30
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
The Conditional Jump Instructions
例 417:• 在以 DATA1为首地址的内存数据段中,存放了 10个 8位的无符号数,是将其中的最大数和最小数找出来,并存入 max和 min单元中。
2023年4月19日 星期三 31
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
本章中文作业• 中文教材 149-151• 第 1题• 第 2题• 第 3题• 第 6题• 第 9题• 第 11题• 第 12题
2023年4月19日 星期三 32
Chapter Four-- The 80x86 Instruction Set Principles of Microcomputers
本章英文作业1. Which flag(s) does the 80x86 use to check for unsigned ari
thmetic overflow?2. Which flag(s) let you check for signed overflow?.3. Which flag(s) does the 80x86 use to test the following unsi
gned 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 equal4. Repeat the above question for a signed comparison.5. What instruction is CMP most similar to?6. What instruction is TEST most similar to?