1、CHAPTER 6 Program Control Instructions(程序控制指令)(P.183)6-1 THE JUMP GROUPnUnconditional JumpnConditional JumpsnLOOP6-1-1 Unconditional Jump(无条件转移指令)(P.184)nThe unconditional jump instruction,JMP,allows the programmer to skip sections of a program and branch to any part of the memory for the next instr
2、uction.nThree types of unconditional jump instructions are available to the microprocessor:nShort jumpnNear jumpnFar jumpnJumps with Register OperandsnIndirect jumps Using an IndexShort jump(短转移)(P.184)nFormatnJMPSHORT LABELnMachine languageDisplacementEBOpcodenOperationnIPIPDisplacementnNotes:nThe
3、short jump displacement is a distance represented by a one-byte signed number whose value ranges between+127 and 128.nThe short jump is an intrasegment jump that allows branches to memory locations within+127 and 128 bytes from the address following the jump.nShort jump is relative program memory ad
4、dressing.1000A10009100081000710006100051000410003100021000110000MemoryJMP04(Jump to here)CS=1000HIP=0002HFIGURE 6-2 A short jump to four memory locations beyond the address of the next instructionNew IP=IP+04New IP=0006H0000 33DBXORBX,BX0002 B80001START:MOVAX,10005 03C3ADDAX,BX0007 EB17JMPSHORT NEXT
5、0020 8BD8NEXT:MOVBX,AX0022 EBDEJMPSHORT STARTMachine LanguageAssembly LanguageOffsetNear jump(近转移)(P.185)nFormatnJMP(NEAR PTR)LABELnMachine languageDisplacementLowDisplacementHighE9OpcodenOperationnIPIPDisplacementnNotes:nThe near jump displacement is a distance represented by a signed 16-bit number
6、 whose value ranges between+32767 and 32768.nA near jump is an intrasegment jump that can jump to any memory location within the current real mode code segment.nShort jump is relative program memory addressing.1000A10009100081000710006100051000410003100021000110000MemoryJMP02FIGURE 6-3 A near jump t
7、hat adds the displacement(0002H)to the contents of IP00CS=1000HIP=0003HNew IP=IP+0002New IP=0005H(Jump to here)0000 33DBXORBX,BX0002 B80001START:MOVAX,10005 03C3ADDAX,BX0007 E9F601JMPNEXT0200 8BD8NEXT:MOVBX,AX0202 E9FDFDJMPSTARTOffsetMachine LanguageAssembly LanguageFar jump(远转移)(P.186)nFormatnJMPFA
8、R PTR LABELnMachine languageNEW IPHighNEW CSLowNew IPLowNEW CSHighEAOpcodenOperationnIPIP High,IP LownCSCS High,CS LownNotes:nA far jump instruction obtains a new segment and offset address to accomplish the jump.nA far jump is an intersegment jump that accesses any location within the first 1M byte
9、 of memory in the real mode.nA far jump is direct program memory addressing.A3128A3127A31261000410003100021000110000MemoryJMP27FIGURE 6-4 A far jump instruction replaces the contents of both CS and IP with four bytes following the opcode.0100A310005CS=1000HIP=0005HNew IP=0127HNew CS=A300H(Jump to he
10、re)EXTRN UP:FAR0000 33DBXORBX,BX0002 B80001START:MOVAX,10005 03C3ADDAX,BX0007 E9F601JMPNEXT0200 8BD8NEXT:MOVBX,AX0202EA0002-JMPFAR PTR START0207EA0000-JMPUPOffsetMachine LanguageAssembly LanguageJumps with Register Operands(带寄存器操作数的转移)(P.181)nFormatnJMP 16regnOperationnIP 16regnFor example,the JMP A
11、X instruction copies the contents of the AX register into the IP when the jump occurs.nNotes:nThe 16reg represents any 16-bit register except segment registers.nThe address of the jump is in the register specified by the jump instruction.nA jump with register operand is intrasegment jump that allows
12、 a jump to any location within the current code segment.nA jump with register operand is indirect program memory addressing.Indirect jumps Using an Index(使用变址的间接转移)(P.188)nThe indirect jumps using an index use the form of addressing to directly access the jump table.nThe assembler assumes that the j
13、ump is near unless the FAR PTR directive indicates a far jump instruction.TABLE 3-10 Examples of indirect program memory addressingAssembly LanguageOperationJMP NEAR PTRBXJumps to the current code segment location addressed by the contents of the data segment memory location addressed by BXJMP NEAR
14、PTRDI+2Jumps to the current code segment location addressed by the contents of the data segment memory location addressed by DI plus 2JMP TABLEBXJumps to the current code segment location addressed by the contents of the data segment memory location addressed by TABLE plus BXEXAMPLE 6-5.MODELSMALL;s
15、elect SMALL model.DATA;start of DATA segmentTABLEDWONE;lookup tableDWTWODWTHREE.CODE;start of CODE segment.STARTUPTOP:MOVAH,1;read key to ALINT21HMOVSI,0SUBAL,31H;test for below 1JBTOP;if below 1CMPAL,2JATOP;if above 3MOVSI,0MOVAH,0;calculate table addressADDSI,AXJMPTABLESI;jump to ONE,TWO,or THREEO
16、NE:MOVDL,1;load DL with 1JMPBOTTWO:MOVDL,2;load DL with 2JMPBOTTHREE:MOVDL,3;load DL with 3BOT:MOVAH,2;display ONE,TWO,or THREEINT21H.EXIT;exit to DOSEND;end of fileNOTE:TABLE DWONE;lookup tableDWTWODWTHREEJMPTABLE SI;IP(TABLE+SI+1,TABLE+SI)6-1-2 Conditional Jumps(条件转移)(P.189)nThe conditional jump i
17、nstructions test the following flag bits:sign(S),zero(Z),carry(C),parity(P),and overflow(O).If the condition under test is true,a branch to the label associated with the jump instruction occurs.If the condition is false,the next sequential step in the program executes.nFor example,a JC will jump if
18、the carry bit is set.nConditional jump instructions are always short jumps in the 8086 microprocessor.nConditional jump instructions often follow the CMP or TEST instruction.nTable 6-1 lists all the conditional jump instructions with their test conditions.TABLE 6-1 Conditional jump instructionsAssem
19、bly LanguageCondition TestedOperationJAZ=0 and C=0Jump if aboveJAEC=0Jump if above or equalJBC=1Jump if belowJBEZ=1 or C=1Jump if below or equalJCC=1Jump if carry setJE or JZZ=1Jump if equal or jump if zero JGZ=0 and S=OJump if greater thanJGES=OJump if greater than or equalJLSOJump if less thanJLEZ
20、=1 or SOJump if less than or equalTABLE 6-1 Conditional jump instructions(continued)Assembly LanguageCondition TestedOperationJNCC=0Jump if no carryJNE or JNZ Z=0Jump if not equal or jump if not zeroJNOO=0Jump if no overflowJNSS=0Jump if no signJNP or JPO P=0Jump if no parity or jump if parity oddJO
21、O=1Jump if overflow setJP or JPEP=1Jump if parity set or jump if parity evenJSS=1Jump if sign is setJCXZCX=0Jump if CX is zero43210Unsigned numbers00H01H02H03H04H80H81H82H83H84HFEHFFH128129130131132254255-124-125-126-127-128Signed numbers80H81H82H83H84HFEHFFH00H01H02H7EH7FH-2-1+0+1+2+126+127FIGURE 6
22、-5 Signed and unsigned numbers follow different orders6-1-3 LOOP(P.186)nFormatnLOOP LABELnOperationnLOOP decrements CX;nif CX0,it jumps to the address indicated by the label;nIf CX becomes a 0,the next sequential instruction executes.nCompare the following instructions:nLOOPLABELnDECCXJNZLABELnEXAMP
23、LE 6-6;A program that sums the contents of BLOCK1 and BLOCK2;and stores the results over top of the data in BLOCK2.MODEL SMALL;select SMALL model.DATA;start of DATA segmentBLOCK1DW100 DUP(?);100 bytes for BLOCK1 BLOCK2DW100 DUP(?);100 bytes for BLOCK2.CODE;start of CODE segment.STARTUP;start of prog
24、ramMOVAX,DS;overlap DS and ESMOVES,AXCLD;select incrementMOVCX,100;load count of 100MOVSI,OFFSET BLOCK1;address BLOCK1MOVDI,OFFSET BLOCK2;address BLOCK2L1:LODSW;load AX with BLOCK1ADDAX,ES:DI;add BLOCK2 data to AXSTOSW;stare sum in BLOCK2LOOP L1;repeat 100 times.EXIT;exit to DOSEND;end of fileCondit
25、ional LOOPSnLOOPE/LOOPZ(loop while equal)nFormat:LOOPE/LOOPZ LABELnOperation:nLOOP decrements CX;nThe LOOPE/LOOPZ instruction jumps to LABEL if CX0 while an equal condition exists;nIt will exit the loop if the condition is not equal or if the CX register decrements to 0.nLOOPE/LOOPZ(loop while not e
26、qual)nFormat:LOOPNE/LOOPNZ LABELnOperation:nLOOP decrements CX;nThe LOOPNE/LOOPNZ instruction jumps to LABEL if CX0 while an not-equal condition exists;nIt will exit the loop if the condition is equal or if the CX register decrements to 0.6-3 PROCEDUES(过程)(P.200)nThe procedure(subroutine)is a group
27、of instructions that usually performs one task.A procedure is a reusable section of the software that is stored in once,but used as often as necessary.nThe CALL instruction links to the procedure,and the RET instruction returns from the procedure.nThe stack stores the return address(the address of t
28、he instruction following the CALL)whenever a procedure is called during the execution of a program.The CALL instruction pushes the return address on the stack.The RET instruction removes the return address from the stack to the program returns to the instruction following the CALL.Procedure Definiti
29、onnFormatNAMEPROCNEAR/FARRETNAMEENDP6-3-1 CALL(P.201)nNear CALLnFar CALLnCALLs with Register OperandsnCALLs with Indirect Memory AddressesNear CALL(近CALL)(P.201)nFormatnCALL NAMEnMachine LanguageE8DisplacementHighDisplacementLownOperationnSS:SP-1,SS:SP-2=IP;SPSP2nIPIP+DisplacementnNote:nThe procedur
30、e is near type.nThe near CALL is relative intrasegment call.AFFFFAFFFEAFFFD1000410003100021000110000MemoryCALLFFFIGURE 6-6 The effect of a near CALL on the stack and the instruction pointer0FNear CALL0003Stack(Procedure)1100211003IP before CALL=0003HSP before CALL=0FFFFHSS before CALL=0A000HNew SP=0
31、FFFDHNew SS=0A000HNew IP=0003+0FFFH=1002HFar CALL(远CALL)(P.201)nFormatnCALL NAMEnMachine LanguageNew CSLowNew CS HighNew IP High9ANew IPLownOperationnSS:SP-1,SS:SP-2=CS;SPSP2nSS:SP-1,SS:SP-2=IP;SPSP2nCSNew CS;IPNew IPnNote:nThe procedure is far type.nThe near CALL is direct intersegment addressing.A
32、FFFFAFFFEAFFFD1000410003100021000110000MemoryCALL0200Far CALL0005Stack(过程)11002110030011AFFFBAFFFCIP before CALL=0005HCS before CALL=1000HSP before CALL=0FFFFHSS before CALL=0A000H1000FIGURE 6-6 The effect of a far CALL instructionNew SP=0FFFDHNew SS=0A000HNew SP=0FFFBHNew SS=0A000HNew IP=0002HNew C
33、S=1100HCALLs with Register Operands(带有寄存器操作数的CALL)(P.202)nFormatnCALL 16regnOperationnSS:SP-1,SS:SP-2=IP;SPSP2nIP16regnNotenThe 16reg represents any 16-bit register except the segment registers.nThe CALL with register operand is indirect intersegment addressing.nAn example is the CALL BX instruction
34、,which pushes the contents of IP onto the stack.It then jumps to the offset address,located in register BX,in the current code segment.CALLs with Indirect Memory Addresses(用间接存储器寻址的CALL)(P.203)nThe CALLs with Indirect Memory Addresses replace the contents of IP or/and CS with the one word or two wor
35、ds in memory location that can be indirectly accessed with register.nExampleCALLWORD PTR BX;(SP+1,SP)IP,SP SP-2 ;IP(BX+1,BX)CALLDWORD PTR VAR-NAME SI;(SP+1,SP)CS,SP SP-2;(SP+1,SP)IP,SP SP-2;IP(VAR-NAME+SI+1,VAR-NAME+SI);CS(VAR-NAME+SI+3,VAR-NAME+SI+2)6-3-2 RET(过程返回)(P.204)nRET without Immediate Oper
36、andnRET with Immediate Operand RET without Immediate Operand(不带立即操作数的过程返回指令不带立即操作数的过程返回指令)nFormatnRETnOperationnFor the near return(the procedure is near type),IP(SP+1,SP),SPSP2.nFor the far return(the procedure is far type),IP(SP+1,SP),SPSP2;CS(SP+1,SP),SPSP2;nA RET instruction partners with a CALL
37、 instruction.The CALL instruction links to the procedure,and the RET instruction returns from the procedure.IP before CALL=0003HAFFFF1000410003100021000110000MemoryCALLFFFIGURE 6-8 The effect of a near return instruction on the stack and instruction pointer0FNear CALLStackRET1100211003(Return here)A
38、FFFDAFFFEAFFFCSP before CALL=0FFFFHSS before CALL=0A000H0003SP after RET=0FFFFHSS after RET=0A000HSP after CALL=0FFFCHSS after CALL=0A000H11004IP after CALL=1002HIP1 0 0 40 0 0 3RET with Immediate Operand(带立即操作数的过程返回指令)nFormatnRETnnOperationnFor the near return(the procedure is near type),IP(SP+1,SP
39、),SPSP2,SPSPn.nFor the far return(the procedure is far type),IP(SP+1,SP),SPSP2;CS(SP+1,SP),SPSP2 SPSPn;nA RET with immediate operand release n memory locations after RET executes.The immediate operand n must be even.6-4 INTRODUCTION TO INTERRUPTS(中断介绍)(P.205)nAn interrupt is a procedure that interru
40、pts whatever program is currently executing.nWhen an interrupt occur,it is responded by calling an interrupt service procedure.The purpose of Interrupt(中断的目的)(P.458)Keyboard interruptMain programPrinter interruptKeyboard interruptPrinter interruptFIGURE 12-1 A time line that indicates interrupt usag
41、ein a typical systemnThe 8086/8088 microprocessor can process 256 different interrupt that are numbered from 0 to 255.nThere are two interrupt group.nA hardware-generated interrupt is externally derived from a hardware signal.nA software-generated interrupt is internally derived from the execution o
42、f an instruction or by some internal event.Interrupt Vector(中断向量)(P.205)nAn interrupt vector is the starting address of an interrupt service procedure.nAn interrupt vector contain a value for IP and CS that forms the address of the interrupt service procedure.The first two bytes contain the IP,and t
43、he last two bytes contain the CS.nThere are 256 different interrupt vector stored in the first 1024 bytes of the memory(00000H003FFH)(the vector table)when the microprocessor operates in the real mode.Type NumberAddressFunction000000H00003HDivide error(除数为0中断)100004H00007HSingle-step(单步中断)200008H000
44、0BHNMI pin(非屏蔽中断)30000CH0000FHBreakpoint(断点中断)400010H00013HInterrupt on overflow(溢出中断)51FH00014H 0007FHReserved(保留中断(共27个)20H0FFH00080H003FFHUser interrupts(用户中断(共244个)Dedicated interrupts(共5个)TABLE 6-4 Interrupt vector of 8086Note:The address of the interrupt vector is determined by multiplying the
45、 interrupt type number times 4.Interrupt Instructions(中断指令)(P.206)nIn the real mode,each of these instructions fetches a vector from the vector table,and then calls the procedure stored at the location addressed by the vector.nINT nnINT3nINTO INT nnFormatnINTnnNotesnThe numeric operand n is the inte
46、rrupt type number whose range is 0 to 255(00HFFH).nAn INT n instruction is two bytes long.nOperation:nThe contents of the flag register are pushed onto the stack.nBoth the IF and TF are cleared.nThe contents of the CS are pushed onto the stack.Return AddressnThe contents of the IP are pushed onto th
47、e stack.nThe interrupt vector contents are fetched,and then placed into both IP and CS.AFFFDAFFFCAFFFB00203002021100111000Memory0CDH80HINT 80HFLAGSStack segmentSP=0FFFEH0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1ODI T S ZAPCAFFFAAFFF910H00H10H02H00H01H0 000H002000020100H00H10HInterrupt vectorAFFF8AFFF7AFFF6AFFF
48、5AFFF450H10000Code segment55HECH1000110002CS=1000HIP=1002HIP=0000HCS=1000HSPSPSP(Procedure)INT 3nFormatnINT 3nNotesnAn INT 3 instruction is a special software interrupt designed to function as a breakpoint.nAn INT 3 is a one-byte instruction.INTOnFormatnINTOnOperationnIf O=0,the INTO instruction per
49、forms no operation.nIf O=1 and INTO instruction executes,an interrupt occurs via vector type number 4.Interrupt on overflownAn INTO is a one-byte instruction.nThe INTO instruction appears in software that adds or subtracts signed binary numbers.IRET(中断返回)(P.207)nFormatnIRETnOperationnPop stack data
50、back into the IP.nPop stack data back into the CS.nPop stack data back into the flag register.nNotenThe IRET is used only with software or hardware interrupt service procedures.EXAMPLE 6-20INTSPROCFARADDAX,BXADDAX,BPADDAX,DIADDAX,SIIRETINTSENDPAFFFDAFFFCAFFFB1100111000Memory0CDH80HINT 80HStack segme