program control instructions in microprocessor … change can be through a condition or sometimes...

46
Fayez 1 Prof. Fayez F. M. El-Sousy Prof. Fayez F. M. El Prof. Fayez F. M. El- Sousy Sousy Department of Electrical Engineering Department of Electrical Engineering College of Engineering College of Engineering Salman Salman bin bin Abdulaziz Abdulaziz University University Al Al- Kharj Kharj , Saudi Arabia , Saudi Arabia Program Control Program Control Instructions Instructions in in Microprocessor 8086/8088 Microprocessor 8086/8088 Prof. Fayez F. M. El-Sousy Objectives of Objectives of Program Control Program Control Instructions Instructions in Microprocessor 8086/8088 in Microprocessor 8086/8088 Upon completion of this chapter, you will be able to: Upon completion of this chapter, you will be able to: Use both conditional and unconditional jump instructions Use both conditional and unconditional jump instructions to control the flow of a program. to control the flow of a program. Use both conditional and unconditional loop instructions Use both conditional and unconditional loop instructions to control the flow of a program. to control the flow of a program. Use the call and return instructions to include procedures Use the call and return instructions to include procedures in the program structure. in the program structure. Use machine control instructions (Flag Control) to modify Use machine control instructions (Flag Control) to modify the flag bits. the flag bits. Use the string instructions to reduce the size of the Use the string instructions to reduce the size of the program. program. Explain the operation of the interrupts and interrupt Explain the operation of the interrupts and interrupt control instructions. control instructions.

Upload: trinhdiep

Post on 13-Mar-2018

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez1

Prof. Fayez F. M. El-Sousy

Prof. Fayez F. M. ElProf. Fayez F. M. El--SousySousy

Department of Electrical EngineeringDepartment of Electrical Engineering

College of EngineeringCollege of Engineering

SalmanSalman bin bin AbdulazizAbdulaziz UniversityUniversity

AlAl--KharjKharj, Saudi Arabia, Saudi Arabia

Program Control Program Control

InstructionsInstructions in in

Microprocessor 8086/8088Microprocessor 8086/8088

Prof. Fayez F. M. El-Sousy

Objectives of Objectives of Program Control Program Control

InstructionsInstructions in Microprocessor 8086/8088in Microprocessor 8086/8088

�� Upon completion of this chapter, you will be able to:Upon completion of this chapter, you will be able to:

�� Use both conditional and unconditional jump instructions Use both conditional and unconditional jump instructions

to control the flow of a program.to control the flow of a program.

�� Use both conditional and unconditional loop instructions Use both conditional and unconditional loop instructions

to control the flow of a program.to control the flow of a program.

�� Use the call and return instructions to include procedures Use the call and return instructions to include procedures

in the program structure. in the program structure.

�� Use machine control instructions (Flag Control) to modify Use machine control instructions (Flag Control) to modify

the flag bits.the flag bits.

�� Use the string instructions to reduce the size of the Use the string instructions to reduce the size of the

program.program.

�� Explain the operation of the interrupts and interrupt Explain the operation of the interrupts and interrupt

control instructions.control instructions.

Page 2: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez2

Prof. Fayez F. M. El-Sousy

�� IntroductionIntroduction

�� These instructions cause change in the sequence These instructions cause change in the sequence

of the execution of instruction.of the execution of instruction.

�� This change can be through a condition or This change can be through a condition or

sometimes unconditional.sometimes unconditional.

�� The conditions are represented by flags.The conditions are represented by flags.

Program Control InstructionsProgram Control Instructions in in

Microprocessor 8086/8088Microprocessor 8086/8088

Prof. Fayez F. M. El-Sousy

Program Control InstructionsProgram Control Instructions in in

Microprocessor 8086/8088Microprocessor 8086/8088

�� IntroductionIntroduction

�� This chapter explains the following:This chapter explains the following:

�� FlagFlag--Control InstructionsControl Instructions

�� Control Flow and the Jump Instructions , Control Flow and the Jump Instructions ,

including the jumps, calls, returns, interrupts, including the jumps, calls, returns, interrupts,

and machine control instructionsand machine control instructions

�� Subroutine and SubroutineSubroutine and Subroutine--Handling Handling

InstructionsInstructions

�� Loop and LoopLoop and Loop--Handling InstructionsHandling Instructions

�� String and StringString and String--Handling InstructionHandling Instruction

Page 3: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez3

Prof. Fayez F. M. El-Sousy

�� The microprocessor has a set of flags that either The microprocessor has a set of flags that either

monitors the state of executing instructions or monitors the state of executing instructions or

controls options available in its operation.controls options available in its operation.

�� CLC:CLC: Clear Carry Flag Clear Carry Flag -- It clears the carry flag to It clears the carry flag to

0.0.

�� STC:STC: Set Carry Flag Set Carry Flag -- It sets the carry flag to 1.It sets the carry flag to 1.

�� CMC:CMC: Complement Carry Flag Complement Carry Flag -- It complements It complements

the carry flag.the carry flag.

�� CLD: CLD: Clear Direction FlagClear Direction Flag -- It clears the direction It clears the direction

flag to 0. flag to 0. If it is reset or cleared, the string bytes are If it is reset or cleared, the string bytes are

accessed from lower memory address to higher accessed from lower memory address to higher

memory address.memory address.

FlagFlag--Control InstructionsControl Instructions

CLC, STC, CMC, CLD, STD, CLI, STICLC, STC, CMC, CLD, STD, CLI, STI

Prof. Fayez F. M. El-Sousy

�� STD:STD: Set Direction FlagSet Direction Flag -- It sets the direction flag to It sets the direction flag to

1. 1. If it is set, string bytes are accessed from higher If it is set, string bytes are accessed from higher

memory address to lower memory address.memory address to lower memory address.

�� CLI:CLI: Clear Interrupt Flag Clear Interrupt Flag –– It clears the interrupt It clears the interrupt

flag to 0.flag to 0.

�� STI:STI: Set Interrupt Flag Set Interrupt Flag –– It sets the interrupt flag to It sets the interrupt flag to

1.1.

�� These instructions control the processor itself.These instructions control the processor itself.

�� 8086 allows to control certain control flags that:8086 allows to control certain control flags that:

�� Causes the processing in a certain directionCauses the processing in a certain direction

�� Processor synchronization if more than one Processor synchronization if more than one

microprocessor attached.microprocessor attached.

FlagFlag--Control InstructionsControl Instructions

CLC, STC, CMC, CLD, STD, CLI, STICLC, STC, CMC, CLD, STD, CLI, STI

Page 4: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez4

Prof. Fayez F. M. El-Sousy

FlagFlag--Control InstructionsControl Instructions

CLC, STC, CMC, CLD, STD, CLI, STICLC, STC, CMC, CLD, STD, CLI, STI

IFIF(IF) (IF) ←←←←←←←←11Set interrupt flagSet interrupt flagSTISTI

IFIF(IF) (IF) ←←←←←←←←00Clear interrupt flagClear interrupt flagCLICLI

DFDF(DF) (DF) ←←←←←←←←11Set direction flagSet direction flagSTDSTD

DFDF(DF) (DF) ←←←←←←←←00Clear direction flagClear direction flagCLDCLD

CFCF(CF) (CF) ←←←←←←←← ((CFCF))Complement carry flagComplement carry flagCMC CMC

CFCF(CF) (CF) ←←←←←←←←00Clear carry flagClear carry flagCLCCLC

CFCF(CF) (CF) ←←←←←←←←11Set carry flagSet carry flagSTCSTC

Flags affectedFlags affectedOperationOperationMeaningMeaningMnemonicMnemonic

FlagFlag--Control InstructionsControl Instructions

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

��Allows programmer to skip program sections Allows programmer to skip program sections

and branch to any part of memory for theand branch to any part of memory for the

next instruction. next instruction.

��A conditional jump instruction allows decisions A conditional jump instruction allows decisions

based upon numerical tests. based upon numerical tests.

�� results are held in the flag bits, then tested by results are held in the flag bits, then tested by

conditional jump instructionsconditional jump instructions

��LOOP and conditional LOOP are also formsLOOP and conditional LOOP are also forms

of the jump instruction.of the jump instruction.

Page 5: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez5

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

��JMP Des: JMP Des:

�� This instruction is used for unconditional jump from This instruction is used for unconditional jump from

one place to another.one place to another.

�� Two types:Two types: short jump and near jump (8086/8088).short jump and near jump (8086/8088).

�� Short jumpShort jump is a 2is a 2--byte instruction that allows jumps byte instruction that allows jumps

or branches to memory locations within +127 and or branches to memory locations within +127 and ––

128 bytes.128 bytes.

�� from the address following the jumpfrom the address following the jump

�� Near jumpNear jump is a 3is a 3--byte instruction that allows a branch byte instruction that allows a branch

or jump within or jump within ±±±±±±±±32K bytes from the instruction in the 32K bytes from the instruction in the

current code segment.current code segment. of the jump instruction.of the jump instruction.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

The two main forms of the JMP instruction. Note that The two main forms of the JMP instruction. Note that DispDisp is is

either an 8either an 8-- or 16or 16--bit signed displacement or distancebit signed displacement or distance

Page 6: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez6

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

��Short Jump: Short Jump:

��Called relative jumps because they can be moved, Called relative jumps because they can be moved,

with related software, to any location in the with related software, to any location in the

current code segment without a change. current code segment without a change.

�� jump address is not stored with the jump address is not stored with the opcodeopcode

�� a distance, or displacement, follows the a distance, or displacement, follows the opcodeopcode

��The short jump displacement is a distance The short jump displacement is a distance

represented by a 1represented by a 1--byte signed number whose byte signed number whose

value ranges between +127 and value ranges between +127 and ––128.128.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

A short jump to four memory locations A short jump to four memory locations

beyond the address of the next instruction.beyond the address of the next instruction.

�� Short Jump: Short Jump:

�� When the microprocessor When the microprocessor

executes a short jump, the executes a short jump, the

displacement is signdisplacement is sign--

extended and added to the extended and added to the

instruction pointer (IP) to instruction pointer (IP) to

generate the jump addressgenerate the jump address

within the current code within the current code

segmentsegment

�� The instruction branches to The instruction branches to

this new address forthis new address for

the next instructionthe next instruction

in the program. in the program.

Page 7: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez7

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Short Jump: Short Jump:

�� When a jump references an address, a label normally When a jump references an address, a label normally

identifies the address. identifies the address.

�� The JMP NEXT instruction is an example.The JMP NEXT instruction is an example.

�� It jumps to label NEXT for the next instruction It jumps to label NEXT for the next instruction

�� Very rare to use an actual hexadecimal address with Very rare to use an actual hexadecimal address with

any jump instructionany jump instruction

�� The label NEXT must be followed by a colon (NEXT:) to The label NEXT must be followed by a colon (NEXT:) to

allow an instruction to reference itallow an instruction to reference it

�� if a colon does not follow, you cannot jump to itif a colon does not follow, you cannot jump to it

�� The only time a colon is used is when the label is used The only time a colon is used is when the label is used

with a jump or call instruction. with a jump or call instruction.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Short Jump: Short Jump:

�� When a jump references an address, a label normally When a jump references an address, a label normally

identifies the address. identifies the address.

�� The JMP NEXT instruction is an example.The JMP NEXT instruction is an example.

Example:Example:

0000 33 DB XOR BX, BX0000 33 DB XOR BX, BX

0002 B8 0001 start: MOV AX, 10002 B8 0001 start: MOV AX, 1

0005 03 C3 ADD AX, BX0005 03 C3 ADD AX, BX

0007 EB 17 JMP short next0007 EB 17 JMP short next

<skipped memory locations><skipped memory locations>

0020 8B D8 next: MOV BX, AX0020 8B D8 next: MOV BX, AX

0022 EB DE JMP start0022 EB DE JMP start

�� The next instruction The next instruction

(0009H) is added to (0009H) is added to

0017H.0017H.

�� The address of the next The address of the next

is located at is located at

0017+0009=0020H.0017+0009=0020H.

Page 8: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez8

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Near Jump: Near Jump:

�� A A near jumpnear jump passes control to an instruction in passes control to an instruction in

the current code segment located within the current code segment located within ±±±±±±±±32K 32K

bytes from the near jump instruction.bytes from the near jump instruction.

�� Near jumpNear jump is a 3is a 3--byte instruction with byte instruction with opcodeopcode

followed by a signed 16followed by a signed 16--bit displacement. bit displacement.

�� Signed displacement adds to the instruction Signed displacement adds to the instruction

pointer (IP) to generate the jump address. pointer (IP) to generate the jump address.

�� because signed displacement is because signed displacement is ±±±±±±±±32K, a near 32K, a near

jump can jump to any memory location withinjump can jump to any memory location within

the current real mode code segment the current real mode code segment

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

A near jump that adds the displacement A near jump that adds the displacement

(0002H) to the contents of IP.(0002H) to the contents of IP.

Page 9: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez9

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Near Jump: Near Jump:

�� When a jump references an address, a label normally When a jump references an address, a label normally

identifies the address. identifies the address.

�� The JMP NEXT instruction is an example.The JMP NEXT instruction is an example.

Example:Example:

0000 33 DB XOR BX, BX0000 33 DB XOR BX, BX

0002 B8 0001 start: MOV AX, 10002 B8 0001 start: MOV AX, 1

0005 03 C3 ADD AX, BX0005 03 C3 ADD AX, BX

0007 E9 0200 R JMP next0007 E9 0200 R JMP next

<skipped memory locations><skipped memory locations>

0200 8B D8 next: MOV BX, AX0200 8B D8 next: MOV BX, AX

0202 E9 0002 R JMP start0202 E9 0002 R JMP start

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Near Jump: Near Jump:

�� It is the default, It is the default,

�� It has the formatIt has the format ““ JMP JMP label label ““ .

�� This is a near jump (within the current code segment) This is a near jump (within the current code segment)

and has the and has the opcodeopcode E9.E9.

�� The target address can be any of the addressing modes of The target address can be any of the addressing modes of

direct, register, register indirect, or memory indirectdirect, register, register indirect, or memory indirect..

�� It is divided into:It is divided into:

�� Direct JMP Direct JMP

�� Register indirect JMP Register indirect JMP

�� Memory indirect JMPMemory indirect JMP

Page 10: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez10

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Direct Jump: Direct Jump:

�� is exactly like the short jump explained earlier,is exactly like the short jump explained earlier,

�� except that the target address can be anywhere in except that the target address can be anywhere in

the segment within the range +32767 to the segment within the range +32767 to --32768 of the 32768 of the

current IP.current IP.

�� Register Indirect Jump:Register Indirect Jump:

�� the target address is in a register. the target address is in a register.

�� For example, in "For example, in "JMP BXJMP BX", ", IPIP takes the value takes the value BXBX..

�� Unlike displacement associated with the near jump, Unlike displacement associated with the near jump,

register contents are transferred directly into the register contents are transferred directly into the

instruction pointer instruction pointer IPIP..

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump, JMP DesUnconditional Jump, JMP Des

�� Memory Indirect Jump:Memory Indirect Jump:

�� the target address is the contents of two memory the target address is the contents of two memory

locations pointed at by the register. locations pointed at by the register.

�� Example:Example: ““JMP [DI]JMP [DI]”” will replace the will replace the IPIP with the with the

contents of memory locations pointed at by contents of memory locations pointed at by DIDI and and

DI+1DI+1..

Page 11: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez11

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump InstructionsUnconditional Jump Instructions

�� Unconditional Jump (JMP) : SUMMARYUnconditional Jump (JMP) : SUMMARY

�� The unconditional jump can take the following forms:The unconditional jump can take the following forms:

�� SHORT JUMPSHORT JUMP, which is specified by format, which is specified by format ““JMP JMP

SHORT labelSHORT label””. . This is a jump in which the address of This is a jump in which the address of

the target location is within the target location is within --128 to +127128 to +127 bytes of bytes of

memory relative to the address of the current memory relative to the address of the current IPIP. The . The

target address can be just direct addressing mode.target address can be just direct addressing mode.

�� NEAR JUMPNEAR JUMP, , which is the defaultwhich is the default, has the format, has the format

““JMP labelJMP label””.. The target address can be any of the The target address can be any of the

addressing modes of direct, register indirect, or addressing modes of direct, register indirect, or

memory indirect:memory indirect:

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Unconditional Jump InstructionsUnconditional Jump Instructions

�� Unconditional Jump (JMP) : SUMMARYUnconditional Jump (JMP) : SUMMARY

�� Direct JUMPDirect JUMP is exactly like the is exactly like the short jumpshort jump explained explained

earlier, except that the target address can be earlier, except that the target address can be

anywhere in the segment within the range anywhere in the segment within the range +32767 to +32767 to --

3276832768 of the current of the current IPIP. .

�� Register Indirect JUMPRegister Indirect JUMP; the target address is in ; the target address is in

register. For example, in register. For example, in ““JUMP BXJUMP BX””, , IPIP takes the takes the

value of value of BXBX..

�� Memory Indirect JUMPMemory Indirect JUMP; Example, ; Example, ““JUMP [DI]JUMP [DI]”” will will

replace the replace the IPIP with the contents of memory locations with the contents of memory locations

pointed by pointed by DIDI and and DI+1DI+1..

�� NOTENOTE: Most of current assemblers choose the best form : Most of current assemblers choose the best form

of the of the JMPJMP instruction (i.e., either short or near). instruction (i.e., either short or near).

Page 12: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez12

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

��JxxJxx Des (Conditional jump): Des (Conditional jump):

��All the conditional jumps follow some conditional All the conditional jumps follow some conditional

statements or any instruction that affects the flag.statements or any instruction that affects the flag.

�� In the conditional jump, control is transferred to a In the conditional jump, control is transferred to a new location if a certain condition is met.new location if a certain condition is met.

��Always short jumps in 8086.Always short jumps in 8086.

�� limits range to within +127 and limits range to within +127 and ––128 bytes 128 bytes

from the location following the conditional from the location following the conditional

jumpjump

��Allows a conditional jump to any location within Allows a conditional jump to any location within

the current code segment.the current code segment.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

��JxxJxx Des (Conditional jump): Des (Conditional jump):

�� Conditional jump instructions test flag bits:Conditional jump instructions test flag bits:

�� sign (S), zero (Z), carry (C)sign (S), zero (Z), carry (C)

�� parity (P), overflow (O) parity (P), overflow (O)

�� If the condition under test is true, a branch to the label If the condition under test is true, a branch to the label

associated with the jump instruction occurs. associated with the jump instruction occurs.

�� if false, next sequential step in program executes if false, next sequential step in program executes

�� for example, a JC will jump if the carry bit is setfor example, a JC will jump if the carry bit is set

�� Most conditional jump instructions are Most conditional jump instructions are

straightforward as they often test one flag bit.straightforward as they often test one flag bit.

�� although some test more than onealthough some test more than one

Page 13: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez13

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

��JxxJxx Des (Conditional jump): Des (Conditional jump):

��Because both Because both signed signed and and unsignedunsigned numbers are numbers are

used in programming.used in programming.

��Because the order of these numbers is different, Because the order of these numbers is different,

there are two sets of conditional jump instructions there are two sets of conditional jump instructions

for magnitude comparisons. for magnitude comparisons.

��1616--bit numbers follow the same order as 8bit numbers follow the same order as 8--bit bit

numbers, except that they are larger. numbers, except that they are larger.

��The following Figure shows the order of both The following Figure shows the order of both

signedsigned and and unsignedunsigned 88--bit numbers.bit numbers.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

Signed and unsigned numbers follow different ordersSigned and unsigned numbers follow different orders

Page 14: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez14

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

�� JxxJxx Des (Conditional jump): Des (Conditional jump):

�� When signed numbers are compared, use the JG, When signed numbers are compared, use the JG,

JL, JGE, JLE, JE, and JNE instructions.JL, JGE, JLE, JE, and JNE instructions.

�� terms greater than and less than refer to signed terms greater than and less than refer to signed

numbers numbers

�� When unsigned numbers are compared, use the JA, When unsigned numbers are compared, use the JA,

JB, JAB, JBE, JE, and JNE instructions. JB, JAB, JBE, JE, and JNE instructions.

�� terms above and below refer to unsigned terms above and below refer to unsigned

numbersnumbers

�� Remaining conditional jumps test individual flag Remaining conditional jumps test individual flag

bits, such as overflow and parity.bits, such as overflow and parity.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

��JxxJxx Des (Conditional jump): Des (Conditional jump):

��Remaining conditional jumps test individual flag Remaining conditional jumps test individual flag

bits, such as overflow and parity. bits, such as overflow and parity.

�� notice that JE has an alternative notice that JE has an alternative opcodeopcode JZJZ

��All instructions have alternates, but many arenAll instructions have alternates, but many aren’’t t

used in programming because they donused in programming because they don’’t usually t usually

fit the condition under test.fit the condition under test.

Page 15: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez15

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump, Conditional Jump, JxxJxx DesDes

�� Conditional jumps, have mnemonics such as JNZ (jump Conditional jumps, have mnemonics such as JNZ (jump not zero) and JC (jump if carry).not zero) and JC (jump if carry).

�� In the conditional jump, control is transferred to a new In the conditional jump, control is transferred to a new location if a certain condition is met.location if a certain condition is met.

�� The flag register is the one that indicates the current The flag register is the one that indicates the current condition. condition.

�� For example, with "JNZ label", For example, with "JNZ label",

�� the processor looks at the zero flag to see if it is the processor looks at the zero flag to see if it is

raised.raised.

�� If not, the CPU starts to fetch and execute instructions If not, the CPU starts to fetch and execute instructions

from the address of the label.from the address of the label.

�� If ZF = 1, it will not jump but will execute the next If ZF = 1, it will not jump but will execute the next

instruction below the JNZ.instruction below the JNZ.

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

�� JAE/JNB/JNC JAE/JNB/JNC (jump if above or equal / jump if not below/ jump (jump if above or equal / jump if not below/ jump if no carry)if no carry)

�� If, after a compare or some other instructions which affect If, after a compare or some other instructions which affect

flags, the carry flag is 0, this instruction will cause executioflags, the carry flag is 0, this instruction will cause execution n

to jump to a label given in the instruction. If CF is 1, the to jump to a label given in the instruction. If CF is 1, the

instruction will have no effect on program execution.instruction will have no effect on program execution.

�� Example:Example:

CMP AX, 4371HCMP AX, 4371H ; Compare (AX; Compare (AX–– 4371H)4371H)

JAE NEXTJAE NEXT ; Jump to label NEXT if AX above or ; Jump to label NEXT if AX above or

equal 4371Hequal 4371H

CMP AX, 4371HCMP AX, 4371H ; Compare (AX ; Compare (AX –– 4371H)4371H)

JNB NEXTJNB NEXT ; Jump to label NEXT if AX not below 4371H; Jump to label NEXT if AX not below 4371H

ADD AL, BLADD AL, BL ; Add two bytes; Add two bytes

JNC NEXTJNC NEXT ; If the result with in acceptable range,; If the result with in acceptable range,

continuecontinue

Page 16: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez16

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

not carrynot carryCF=0CF=0JNCJNC

less or equal/not greaterless or equal/not greater((SF ((SF xorxor OF) or ZF) = 1OF) or ZF) = 1JLE/JNGJLE/JNG

less/not greater nor equalless/not greater nor equal(SF (SF xorxor OF ) = 1OF ) = 1JL/JNGEJL/JNGE

greater or equal/not lessgreater or equal/not less(SF (SF xorxor OF) = 0OF) = 0JGE/JNLJGE/JNL

greater/not less nor equalgreater/not less nor equal((SF ((SF xorxor OF) or ZF ) = 0OF) or ZF ) = 0JG/JNLEJG/JNLE

equal/zeroequal/zeroZF=1ZF=1JE/JZJE/JZ

CarryCarryCF=1CF=1JCJC

below or equal/not abovebelow or equal/not above((CForZFCForZF)=1)=1JBE/JNAJBE/JNA

below/not above nor equalbelow/not above nor equalCF=1CF=1JB/JNAEJB/JNAE

above or equal/not belowabove or equal/not belowCF=0CF=0JAE/JNBJAE/JNB

above/not below nor equalabove/not below nor equal(CF or ZF) = 0(CF or ZF) = 0JA / JNBEJA / JNBE

Jump if:Jump if:Condition TestedCondition TestedMnemonicMnemonic

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

signsignSF=1SF=1JSJS

parity/parity equalparity/parity equalPF=1PF=1JP/TPEJP/TPE

OverflowOverflowOF=1OF=1JOJO

not signnot signSF=0SF=0JNSJNS

not parity/parity oddnot parity/parity oddPF=0PF=0JNP/JPOJNP/JPO

not overflownot overflowOF=0OF=0JNOJNO

not equal/not zeronot equal/not zeroZF=0ZF=0JNE/JNZJNE/JNZ

Jump if:Jump if:Condition TestedCondition TestedMnemonicMnemonic

NotesNotes::

1.1. AboveAbove and and BelowBelow are used with are used with unsigned valuesunsigned values

2.2. GreaterGreater and and LessLess are used with are used with signed valuessigned values

Page 17: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez17

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

Conditional Conditional JumpsJumps Based on EqualityBased on Equality

�� The conditional jump instructions can be divided into four The conditional jump instructions can be divided into four groups:groups:

�� Jumps based on equality between operands or the value Jumps based on equality between operands or the value

of CX of CX

�� Jumps based on specific flag valuesJumps based on specific flag values

�� Jumps based on comparisons of unsigned operandsJumps based on comparisons of unsigned operands

�� Jumps based on comparisons of signed operandsJumps based on comparisons of signed operands

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

Conditional Conditional JumpsJumps Based on Specific Flag ValuesBased on Specific Flag Values

Page 18: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez18

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

Conditional Conditional JumpsJumps Based on Unsigned ComparisonsBased on Unsigned Comparisons

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

Conditional Conditional JumpsJumps Based on Signed ComparisonsBased on Signed Comparisons

Page 19: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez19

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

�� JA/JNBEJA/JNBE (jump if above/jump if not below or equal)(jump if above/jump if not below or equal)

�� If, after a compare or some other instructions which If, after a compare or some other instructions which

affect flags, the zero flag and the carry flag both are 0, affect flags, the zero flag and the carry flag both are 0,

this instruction will cause execution to jump to a label this instruction will cause execution to jump to a label

given in the instruction. If CF and ZF are not both 0, the given in the instruction. If CF and ZF are not both 0, the

instruction will have no effect on program execution.instruction will have no effect on program execution.

��Example:Example:CMP AX, 4371HCMP AX, 4371H ; Compare by subtracting 4371H from AX; Compare by subtracting 4371H from AX

JA NEXTJA NEXT ; Jump to label NEXT if AX above 4371H; Jump to label NEXT if AX above 4371H

CMP AX, 4371HCMP AX, 4371H ; Compare (AX ; Compare (AX –– 4371H)4371H)

JNBE NEXTJNBE NEXT ; Jump to label NEXT if AX not below or; Jump to label NEXT if AX not below or

equal to 4371Hequal to 4371H

Prof. Fayez F. M. El-Sousy

The Jump Group InstructionsThe Jump Group Instructions

Conditional Jump InstructionsConditional Jump Instructions

�� JAE/JNB/JNC JAE/JNB/JNC (jump if above or equal / jump if not below/ jump (jump if above or equal / jump if not below/ jump if no carry)if no carry)

�� If, after a compare or some other instructions which affect If, after a compare or some other instructions which affect

flags, the carry flag is 0, this instruction will cause executioflags, the carry flag is 0, this instruction will cause execution n

to jump to a label given in the instruction. If CF is 1, the to jump to a label given in the instruction. If CF is 1, the

instruction will have no effect on program execution.instruction will have no effect on program execution.

�� Example:Example:

CMP AX, 4371HCMP AX, 4371H ; Compare (AX; Compare (AX–– 4371H)4371H)

JAE NEXTJAE NEXT ; Jump to label NEXT if AX above or ; Jump to label NEXT if AX above or

equal 4371Hequal 4371H

CMP AX, 4371HCMP AX, 4371H ; Compare (AX ; Compare (AX –– 4371H)4371H)

JNB NEXTJNB NEXT ; Jump to label NEXT if AX not below 4371H; Jump to label NEXT if AX not below 4371H

ADD AL, BLADD AL, BL ; Add two bytes; Add two bytes

JNC NEXTJNC NEXT ; If the result with in acceptable range,; If the result with in acceptable range,

continuecontinue

Page 20: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez20

Prof. Fayez F. M. El-Sousy

The Loop InstructionsThe Loop Instructions

LOOP InstructionsLOOP Instructions

Prof. Fayez F. M. El-Sousy

The Loop InstructionsThe Loop Instructions

Unconditional LOOP InstructionUnconditional LOOP Instruction

�� Loop Des: Loop Des:

�� LOOPLOOP (jump to specified label if (jump to specified label if CXCX≠≠≠≠≠≠≠≠ 00, after auto decrement), after auto decrement)

�� This instruction is used to repeat a series of instructions someThis instruction is used to repeat a series of instructions somenumber of times. The number of times the instruction sequence number of times. The number of times the instruction sequence is to be repeated is loaded into is to be repeated is loaded into CXCX. Each time the . Each time the LOOPLOOPinstruction executes, instruction executes, CXCX is automatically decremented by 1.is automatically decremented by 1.

�� If If CX is not 0CX is not 0, execution will jump to a destination specified by , execution will jump to a destination specified by a label in the instruction. a label in the instruction.

�� If If CX = 0CX = 0 after the auto decrement, execution will simply go on after the auto decrement, execution will simply go on to the next instruction after to the next instruction after LOOPLOOP..

�� The destination address for the jump must be in the range of The destination address for the jump must be in the range of ––128 bytes to +127 bytes128 bytes to +127 bytes from the address of the instruction from the address of the instruction

after the after the LOOPLOOP instruction. This instruction does not affect any instruction. This instruction does not affect any flag. flag.

Page 21: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez21

Prof. Fayez F. M. El-Sousy

�� Loop Des: Loop Des:

�� This is a This is a loopinglooping instruction. instruction.

�� The number of times looping is required is placed in theThe number of times looping is required is placed in theCXCX register. register.

�� With each iteration, the contents ofWith each iteration, the contents of CXCX are decremented.are decremented.

�� ZFZF is checked whether to is checked whether to looploop again or not.again or not.

�� A combination of a decrement A combination of a decrement CXCX and the and the JNZJNZ conditional conditional

jump.jump.

�� In 8086 In 8086 LOOPLOOP decrements decrements CXCX..

�� if CXif CX≠≠≠≠≠≠≠≠ 0, it jumps to the address indicated0, it jumps to the address indicated

by the labelby the label

�� If CX becomes 0, the next sequential instruction If CX becomes 0, the next sequential instruction

executesexecutes

The Loop InstructionsThe Loop Instructions

Unconditional LOOP InstructionUnconditional LOOP Instruction

Prof. Fayez F. M. El-Sousy

�� Example:Example:

MOV BX, OFFSET ; MOV BX, OFFSET ; Point BX at first element inPoint BX at first element in

arrayarray

MOV CX, 40 ; MOV CX, 40 ; Load CX with number of Load CX with number of

elements ielements in arrayn array

NEXT: MOV AL, [BX] ; NEXT: MOV AL, [BX] ; Get element from arrayGet element from array

INC AL ; INC AL ; Increment the content of ALIncrement the content of AL

MOV [BX], AL ; MOV [BX], AL ; Put result back in arrayPut result back in array

INC BX ; INC BX ; Increment BX to point toIncrement BX to point to

next locatnext locationion

LOOP NEXT ; LOOP NEXT ; Repeat until all elementsRepeat until all elements

adjustedadjusted

The Loop InstructionsThe Loop Instructions

Unconditional LOOP InstructionUnconditional LOOP Instruction

Page 22: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez22

Prof. Fayez F. M. El-Sousy

The Loop InstructionsThe Loop Instructions

Conditional LOOP InstructionConditional LOOP Instruction

��LOOPLOOP instruction also has conditional forms: instruction also has conditional forms:

LOOPELOOPE and and LOOPNELOOPNE

��LOOPELOOPE (loop while equal) instruction jumps if (loop while equal) instruction jumps if

CXCX≠≠≠≠≠≠≠≠ 00 while an equal condition exists.while an equal condition exists.

�� will exit loop if the condition is not equal or thewill exit loop if the condition is not equal or the

CX register decrements to 0. CX register decrements to 0.

��LOOPNELOOPNE (loop while not equal) jumps if (loop while not equal) jumps if CX CX ≠≠≠≠≠≠≠≠ 00

while a notwhile a not--equal condition exists.equal condition exists.

�� will exit loop if the condition is equal or the CX will exit loop if the condition is equal or the CX

register decrements to 0.register decrements to 0.

Prof. Fayez F. M. El-Sousy

��Alternates exist for LOOPE and LOOPNE.Alternates exist for LOOPE and LOOPNE.

�� LOOPE same as LOOPZLOOPE same as LOOPZ

�� LOOPNE instruction is the same as LOOPNZLOOPNE instruction is the same as LOOPNZ

��In most programs, only the In most programs, only the LOOPELOOPE and and LOOPNELOOPNE

apply.apply.

The Loop InstructionsThe Loop Instructions

Conditional LOOP InstructionConditional LOOP Instruction

Page 23: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez23

Prof. Fayez F. M. El-Sousy

�� LOOPE/LOOPZLOOPE/LOOPZ (loop while (loop while CX CX ≠≠≠≠≠≠≠≠ 00 AND AND ZF=1ZF=1))

�� This instruction is used to repeat a group of instructions This instruction is used to repeat a group of instructions

some number of times, or until the zero flag becomes 0. The some number of times, or until the zero flag becomes 0. The

number of times the instruction sequence is to be repeated number of times the instruction sequence is to be repeated

is loaded into is loaded into CXCX. Each time the . Each time the LOOPLOOP instruction instruction

executes, executes, CXCX is automatically decremented by 1. is automatically decremented by 1.

�� If If CX CX ≠≠≠≠≠≠≠≠ 00 and and ZF = 1ZF = 1, execution will jump to a destination , execution will jump to a destination

specified by a label in the instruction. If specified by a label in the instruction. If CX = 0CX = 0, execution , execution

simply go on the next instruction after simply go on the next instruction after LOOPE/LOOPZLOOPE/LOOPZ. In . In

other words, the two ways to exit the loop are other words, the two ways to exit the loop are CX = 0CX = 0 or or ZF ZF

= 0= 0. The destination address for the jump must be in the . The destination address for the jump must be in the

range of range of ––128 bytes to +127 bytes128 bytes to +127 bytes from the address of the from the address of the

instruction after the instruction after the LOOPE/LOOPZLOOPE/LOOPZ instruction. This instruction. This

instruction does not affect any flag. instruction does not affect any flag.

The Loop InstructionsThe Loop Instructions

Conditional LOOP InstructionConditional LOOP Instruction

Prof. Fayez F. M. El-Sousy

�� LOOPE/LOOPZLOOPE/LOOPZ (loop while (loop while CX CX ≠≠≠≠≠≠≠≠ 00 AND AND ZF=1ZF=1))

�� Example:Example:

MOV BX, MOV BX, OFFSET ARRAYOFFSET ARRAY ; ; Point BX to address of Point BX to address of

ARRAY befoARRAY before start of arrayre start of array

DEC BX ; DEC BX ; Decrement BXDecrement BX

MOV CX, 100 ; MOV CX, 100 ; Load CX with number of Load CX with number of

elements ielements in arrayn array

NEXT: INC BX ; NEXT: INC BX ; Point to next element in arrayPoint to next element in array

CMP [BX], 0FFH ; CMP [BX], 0FFH ; Compare array element withCompare array element with

FFHFFH

LOOPE NEXT ; LOOPE NEXT ; Repeat until all elementsRepeat until all elements

adjustedadjusted

The Loop InstructionsThe Loop Instructions

Conditional LOOP InstructionConditional LOOP Instruction

Page 24: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez24

Prof. Fayez F. M. El-Sousy

�� LOOPNE/LOOPNZLOOPNE/LOOPNZ (loop while (loop while CX CX ≠≠≠≠≠≠≠≠ 00 AND AND ZF=0ZF=0))

�� This instruction is used to repeat a group of instructions some This instruction is used to repeat a group of instructions some

number of times, or until the number of times, or until the zero flagzero flag becomes a 1. The number becomes a 1. The number

of times the instruction sequence is to be repeated is loaded inof times the instruction sequence is to be repeated is loaded into to

the count register the count register CXCX. Each time the . Each time the LOOPNE/LOOPNZLOOPNE/LOOPNZ

instruction executes, instruction executes, CXCX is automatically decremented by 1.is automatically decremented by 1.

�� If If CX CX ≠≠≠≠≠≠≠≠ 00 and and ZF = 0ZF = 0, execution will jump to a destination , execution will jump to a destination

specified by a specified by a labellabel in the instruction. If in the instruction. If CX = 0CX = 0, after the auto , after the auto

decrement or if decrement or if ZF = 1ZF = 1, execution simply go on the next , execution simply go on the next

instruction after instruction after LOOPNE/LOOPNZLOOPNE/LOOPNZ. In other words, the two . In other words, the two

ways to exit the loop are ways to exit the loop are CX = 0CX = 0 or or ZF = 1ZF = 1. The destination . The destination

address for the jump must be in the range of address for the jump must be in the range of ––128 bytes to +127128 bytes to +127

bytes from the address of the instruction after the bytes from the address of the instruction after the

LOOPNE/LOOPZLOOPNE/LOOPZ instruction. This instruction does not affect instruction. This instruction does not affect

any flags.any flags.

The Loop InstructionsThe Loop Instructions

Conditional LOOP InstructionConditional LOOP Instruction

Prof. Fayez F. M. El-Sousy

�� LOOPNE/LOOPNZLOOPNE/LOOPNZ (loop while (loop while CX CX ≠≠≠≠≠≠≠≠ 00 AND AND ZF=0ZF=0))

�� Example:Example:

MOV BX, MOV BX, OFFSET ARRAYOFFSET ARRAY ; ; Point BX to address of Point BX to address of

ARRAY befoARRAY before start of arrayre start of array

DEC BX ; DEC BX ; Decrement BXDecrement BX

MOV CX, 100 ; MOV CX, 100 ; Load CX with number of Load CX with number of

elements ielements in arrayn array

NEXT: INC BX ; NEXT: INC BX ; Point to next element in arrayPoint to next element in array

CMP [BX], 0DH ; CMP [BX], 0DH ; Compare array element withCompare array element with

0DH0DH

LOOPNZ NEXT ; LOOPNZ NEXT ; Repeat until all elementsRepeat until all elements

adjustedadjusted

The Loop InstructionsThe Loop Instructions

Conditional LOOP InstructionConditional LOOP Instruction

Page 25: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez25

Prof. Fayez F. M. El-Sousy

�� A A procedureprocedure is a group of instructions that usually is a group of instructions that usually

performs one task. performs one task.

�� subroutine, method, or function is an important subroutine, method, or function is an important

part of any systempart of any system’’s architectures architecture

�� A A procedureprocedure is a reusable section of the software is a reusable section of the software

stored in memory once, used as often as necessary. stored in memory once, used as often as necessary.

�� saves memory space and makes it easier to develop saves memory space and makes it easier to develop

software software

�� Disadvantage of Disadvantage of procedureprocedure is time it takes the is time it takes the

computer to link to, and return from it. computer to link to, and return from it.

�� CALLCALL links to the procedure; the links to the procedure; the RETRET (return)(return)

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Prof. Fayez F. M. El-Sousy

�� CALLCALL pushes the address of the instruction following pushes the address of the instruction following

the the CALLCALL (return address) on the stack.(return address) on the stack.

�� the stack stores the return address when a the stack stores the return address when a

procedure is called during a programprocedure is called during a program

�� RETRET instruction removes an address from the stack so instruction removes an address from the stack so

the program returns to the instruction following the the program returns to the instruction following the

CALLCALL..

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Page 26: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez26

Prof. Fayez F. M. El-Sousy

�� CALL Des: CALL Des:

�� This instruction is used to This instruction is used to callcall a subroutine or function a subroutine or function

or procedure. or procedure.

�� The address of next instruction after The address of next instruction after CALLCALL is saved is saved

onto stack. onto stack.

�� RET: RET:

�� It returns the control from procedure to calling It returns the control from procedure to calling

program.program.

�� Every Every CALLCALL instruction should have a instruction should have a RETRET. .

�� the address of the instruction following the the address of the instruction following the CALLCALL

(return address) on the stack.(return address) on the stack.

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Prof. Fayez F. M. El-Sousy

��CALL Des: CALL Des:

��Transfers the flow of the program to the procedure. Transfers the flow of the program to the procedure.

��CALLCALL instruction differs from the jump instruction instruction differs from the jump instruction

because a because a CALLCALL saves a return address on the stack. saves a return address on the stack.

��The return address returns control to the instruction The return address returns control to the instruction

that immediately follows thethat immediately follows the

CALLCALL in a program when a in a program when a RETRET instruction instruction

executes.executes.

��CALLsCALLs to procedures are used to perform tasks that to procedures are used to perform tasks that

need to be performed frequently need to be performed frequently

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Page 27: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez27

Prof. Fayez F. M. El-Sousy

��CALL Des: CALL Des:

��This makes a program more structured This makes a program more structured

��The target address could be:The target address could be:

�� in the current segment, in which case it will be a in the current segment, in which case it will be a

NEARNEAR CALLCALL

�� or outside the current or outside the current CSCS segment, which is a segment, which is a FARFAR

CALLCALL

�� in the in the NEARNEAR call only the call only the IPIP is saved on the stack, is saved on the stack,

�� in a in a FARFAR call both call both CSCS and and IPIP are saved.are saved.

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Prof. Fayez F. M. El-Sousy

��Near CALL Near CALL

��Near CALLNear CALL is a 3 bytes long.is a 3 bytes long.

�� the first byte contains the the first byte contains the opcodeopcode; the second; the second

and third bytes contain the displacementand third bytes contain the displacement

��When the When the nearnear CALLCALL executes, it first pushes the executes, it first pushes the

offset address of the next instruction onto the stack.offset address of the next instruction onto the stack.

�� offset address of the next instruction appears in offset address of the next instruction appears in

the instruction pointer (IP)the instruction pointer (IP)

�� It then adds displacement from bytes 2 & 3It then adds displacement from bytes 2 & 3

to the to the IPIP to transfer control to the procedure. to transfer control to the procedure.

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Page 28: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez28

Prof. Fayez F. M. El-Sousy

��Near CALL Near CALL

��Why save the Why save the IPIP on the stack? on the stack?

�� the instruction pointer always points to thethe instruction pointer always points to the

next instruction in the program.next instruction in the program.

�� For the For the CALLCALL instruction, the contents of instruction, the contents of IPIP are are

pushed onto the stack.pushed onto the stack.

�� program control passes to the instruction program control passes to the instruction

following the following the CALLCALL after a procedure ends.after a procedure ends.

��The following Figure shows the return address (The following Figure shows the return address (IPIP) )

stored on the stack and the call to the procedure.stored on the stack and the call to the procedure.

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Prof. Fayez F. M. El-Sousy

��Near CALL Near CALL

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

The effect of a The effect of a nearnear CALLCALL on the on the stackstack and and

the instruction pointer the instruction pointer IPIP..

SS:[SPSS:[SP--1] 1] ←←←←←←←← most significant byte (IP).most significant byte (IP).

SS:[SPSS:[SP--2] 2] ←←←←←←←← least significant byte (IP). least significant byte (IP).

A000:FFFE A000:FFFE ←←←←←←←← 0000

A000:FFFD A000:FFFD ←←←←←←←← 03 03

Page 29: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez29

Prof. Fayez F. M. El-Sousy

�� Far CALL Far CALL

�� Far CALLFar CALL is a 5 bytes instruction contains an is a 5 bytes instruction contains an opcodeopcode

followed by the next value for the IP and CS registers.followed by the next value for the IP and CS registers.

�� bytes 2 and 3 contain new contents of the IPbytes 2 and 3 contain new contents of the IP

�� bytes 4 and 5 contain the new contents for CSbytes 4 and 5 contain the new contents for CS

�� Far CALLFar CALL places the contents of both places the contents of both IPIP and and CSCS on on

the stack before jumping to the address indicated by the stack before jumping to the address indicated by

bytes 2 through 5.bytes 2 through 5.

�� This allows This allows far CALLfar CALL to call a procedure located to call a procedure located

anywhere in the memory and return from that anywhere in the memory and return from that

procedure.procedure.

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Prof. Fayez F. M. El-Sousy

�� Far CALL Far CALL

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

The effect of a The effect of a farfar CALLCALL on the on the stackstack and and

the instruction pointer the instruction pointer IPIP..

SS:[SPSS:[SP--1] 1] ←←←←←←←← most significant byte (CS).most significant byte (CS).

SS:[SPSS:[SP--2] 2] ←←←←←←←← least significant byte (CS). least significant byte (CS).

SS:[SPSS:[SP--3] 3] ←←←←←←←← most significant byte (IP).most significant byte (IP).

SS:[SPSS:[SP--4] 4] ←←←←←←←← least significant byte (IP).least significant byte (IP).

A000:FFFE A000:FFFE ←←←←←←←← 1010

A000:FFFD A000:FFFD ←←←←←←←← 0000

A000:FFFC A000:FFFC ←←←←←←←← 0000

A000:FFFB A000:FFFB ←←←←←←←← 0505

1100:0002 / CS:IP1100:0002 / CS:IP

1000:0005 / CS:IP1000:0005 / CS:IP

Page 30: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez30

Prof. Fayez F. M. El-Sousy

�� Indirect RegisterIndirect Register CALL:CALL:

�� An example An example CALL BXCALL BX, which pushes the contents of IP onto the , which pushes the contents of IP onto the

stack.stack.

�� then jumps to the offset address, located in register then jumps to the offset address, located in register BXBX, in the , in the

current code segmentcurrent code segment

�� Always uses a 16Always uses a 16--bit offset address, stored in any 16bit offset address, stored in any 16--bit register bit register

except segment registers.except segment registers.

�� Indirect MemoryIndirect Memory CALL:CALL:

�� Particularly useful when different subroutines need to be chosenParticularly useful when different subroutines need to be chosen

in a program.in a program.

�� selection process is often keyed with a number that addresses a selection process is often keyed with a number that addresses a

CALLCALL address in a lookup tableaddress in a lookup table

�� Essentially the same as the indirect jump that used a lookup tabEssentially the same as the indirect jump that used a lookup table le

for a jump address.for a jump address.

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

Prof. Fayez F. M. El-Sousy

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

�� RET: RET:

�� The last instruction of the called subroutine must be a The last instruction of the called subroutine must be a

RETRET instruction.instruction.

�� which directs the which directs the CPUCPU to to POPPOP the top 2 bytes of the the top 2 bytes of the

stack into the stack into the IP.IP.

�� Note:Note: the number of the number of PUSHPUSH and and POPPOP instructions (which instructions (which

alter the alter the SPSP) must match.) must match.

�� In other words, for every In other words, for every PUSHPUSH there must be a there must be a POPPOP..

�� Near RET: Near RET: Removes aRemoves a 1616--bit bit number (number (near returnnear return) from ) from

the the stackstack placing it in placing it in IPIP..

�� Far RET: Far RET: Removes a Removes a 3232--bitbit number (number (far returnfar return) from ) from

the the stackstack and places it in and places it in IP & CSIP & CS..

Page 31: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez31

Prof. Fayez F. M. El-Sousy

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

�� RET: RET:

�� The following Figure shows how the The following Figure shows how the CALLCALL instruction links to a instruction links to a

procedure and how procedure and how RETRET returns in the 8086returns in the 8086––Core2 operating in the Core2 operating in the

real mode.real mode.

Prof. Fayez F. M. El-Sousy

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

�� Example: Example:

�� Assume that SP = FFFE H and the following code is a Assume that SP = FFFE H and the following code is a

portion of the program:portion of the program:

12B0:0200 BB1295 MOV BX , 951212B0:0200 BB1295 MOV BX , 9512

12B0:0203 E8FA00 CALL 030012B0:0203 E8FA00 CALL 0300

12B0:0206 B82F14 MOV AX, 142F12B0:0206 B82F14 MOV AX, 142F

12B0: 030012B0: 0300 PUSH BX ; SUBROUTINEPUSH BX ; SUBROUTINE

……..

……..

12B0: 030912B0: 0309 POP BXPOP BX

12B0: 030A12B0: 030A RETRET

Page 32: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez32

Prof. Fayez F. M. El-Sousy

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

�� Since the Since the CALLCALL instruction is a instruction is a

NEARNEAR call,call,

�� meaning that it is in the meaning that it is in the

same code segment (different same code segment (different

IPIP, same , same CSCS), ),

�� only only IPIP is saved on the stack.is saved on the stack.

�� In this case, the In this case, the IPIP address address

of the instruction after the of the instruction after the

call is saved on the stack as call is saved on the stack as

shown in the Figure.shown in the Figure.

�� That That IPIP will be 0206,will be 0206,

�� which belongs to the "which belongs to the "MOV MOV

AX, 142FAX, 142F" instruction." instruction.

FFFC

FFFD

FFFE

Prof. Fayez F. M. El-Sousy

The ProceduresThe Procedures

CALL/RET InstructionsCALL/RET Instructions

FFFC

FFFD

FFFE

0202

0606IPIP

BXBX

9595

1212FFFA

FFFB

Page 33: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez33

Prof. Fayez F. M. El-Sousy

�� String in assembly language is just a sequentially String in assembly language is just a sequentially

stored stored bytesbytes or or wordswords. .

�� By using these string instructions, the size of the By using these string instructions, the size of the

program is considerably reduced. program is considerably reduced.

�� There are There are fivefive basic string instructions in the basic string instructions in the

instruction set of the instruction set of the 8086/80888086/8088..

�� MoveMove byte or word string (byte or word string (MOVSB/MOVSWMOVSB/MOVSW).).

�� CompareCompare byte or word string (byte or word string (CMPSB/CMPSWCMPSB/CMPSW).).

�� ScanScan byte or word string (byte or word string (SCASB/SCASWSCASB/SCASW).).

�� LoadLoad byte or word string (byte or word string (LODSB/LODSWLODSB/LODSW).).

�� StoreStore byte or word string (byte or word string (STOSB/STOSWSTOSB/STOSW).).

String InstructionsString Instructions

MOVS/CMPS/SCAS/LODS/STOSMOVS/CMPS/SCAS/LODS/STOS

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

MOVS/CMPS/SCAS/LODS/STOSMOVS/CMPS/SCAS/LODS/STOS

�� Before the string instructions are presented, the Before the string instructions are presented, the

operation of the operation of the DD flagflag--bit (bit (directiondirection), ), DIDI, and , and SISI must must

be understood as they apply to the string instructions. be understood as they apply to the string instructions.

�� The direction flag (The direction flag (DD, located in the flag register) , located in the flag register)

selects the selects the autoauto--increment increment oror thethe autoauto--decrementdecrement

operation for the operation for the DIDI and and SISI registers during string registers during string

operations.operations.

�� used only with the string instructionsused only with the string instructions

�� The The CLDCLD instruction clears the instruction clears the DD flag and the flag and the STDSTD

instruction instruction setssets it.it.

�� CLDCLD instruction selects the instruction selects the autoauto--increment increment mode mode

and and STDSTD selects the selects the autoauto--decrementdecrement mode.mode.

Page 34: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez34

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

MOVS/CMPS/SCAS/LODS/STOSMOVS/CMPS/SCAS/LODS/STOS

�� During execution of string instruction, memory During execution of string instruction, memory

accesses occur through accesses occur through DIDI and and SSI registers. I registers.

�� DIDI offset address accesses data in the extra segment offset address accesses data in the extra segment

for all string instructions that use itfor all string instructions that use it

�� SISI offset address accesses data by default offset address accesses data by default

in the data segmentin the data segment

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

MOVS/CMPS/SCAS/LODS/STOSMOVS/CMPS/SCAS/LODS/STOS

Page 35: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez35

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

�� MOVSMOVS

�� MOVSMOVS Des String Name, Des String Name, SrcSrc String NameString Name

�� MOVSB MOVSB

�� MOVSBMOVSB Des String Name, Des String Name, SrcSrc String NameString Name

�� MOVSW MOVSW

�� MOVSWMOVSW Des String Name, Des String Name, SrcSrc String NameString Name

�� Transfers a Transfers a bytebyte, , wordword, or , or doubleworddoubleword from from data data

segmentsegment addressed by addressed by SISI to to extra segmentextra segment location location

addressed by addressed by DIDI. .

�� pointers are incremented or decremented, as dictated pointers are incremented or decremented, as dictated

by the direction flagby the direction flag

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

�� The The onlyonly memorymemory‐‐‐‐‐‐‐‐toto‐‐‐‐‐‐‐‐memorymemory transfertransfer allowed.allowed.

�� MOVSBMOVSB transfers byte from transfers byte from data segmentdata segment to to extra extra

segmentsegment..

�� MOVSWMOVSW transfers word from transfers word from data segmentdata segment to to extra extra

segmentsegment. .

�� Only the source operand (Only the source operand (SISI), located in the ), located in the data data

segmentsegment may be overridden so another segment may be may be overridden so another segment may be

used. used.

�� The destination operand (The destination operand (DIDI) must always be located in ) must always be located in

the the extra segmentextra segment..

Page 36: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez36

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

�� This instruction copies a byte or a word from location This instruction copies a byte or a word from location

in the data segment to a location in the extra segment. in the data segment to a location in the extra segment.

�� The The offsetoffset of the of the sourcesource in the in the data segmentdata segment must be in must be in

the the SISI register. register.

�� The The offsetoffset of the of the destinationdestination in the in the extra segmentextra segment must must

be in the be in the DIDI register. register.

�� For multipleFor multiple--byte or multiplebyte or multiple--word moves, theword moves, the number number

of elements to be moved is put in the of elements to be moved is put in the CXCX register so register so

that it can function as a counter. that it can function as a counter.

�� After the byte or a word is moved, After the byte or a word is moved, SISI and and DIDI are are

automatically adjusted to point to the next source automatically adjusted to point to the next source

element and the next destination element. element and the next destination element.

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

�� If If DFDF is is 00, then , then SISI and and DIDI will incremented by will incremented by 11 after a after a

byte move and by byte move and by 22 after a word move. after a word move.

�� If If DFDF is is 11, then , then SISI and and DIDI will be decremented by will be decremented by 11

after a byte move and by after a byte move and by 22 after a word move. after a word move.

�� MOVSMOVS does not affect any flag.does not affect any flag.

�� When using the When using the MOVSMOVS instruction, you must in some instruction, you must in some

way tell the assembler whether you want to move a way tell the assembler whether you want to move a

string as bytes or as word. string as bytes or as word.

�� There are two ways to do this. There are two ways to do this.

�� The first way is to indicate the name of the source and The first way is to indicate the name of the source and

destination strings in the instruction, as, for example. destination strings in the instruction, as, for example.

MOVS DEST, SRCMOVS DEST, SRC. .

Page 37: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez37

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

�� The assembler will code the instruction for a byte / The assembler will code the instruction for a byte /

word word movemove if they were declared with a if they were declared with a DBDB / / DWDW. .

�� The second way is to add a The second way is to add a ““BB”” or a or a ““WW”” to the to the MOVSMOVS

mnemonic. mnemonic.

�� MOVSBMOVSB says move a string as bytes. says move a string as bytes.

�� MOVSWMOVSW says move a string as words. says move a string as words.

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

�� Example:Example:

MOV SI, OFFSET SRC ; MOV SI, OFFSET SRC ; Load offset of start of sourceLoad offset of start of source

sstring in DS into SI tring in DS into SI

MOV DI, OFFSET DES ; MOV DI, OFFSET DES ; Load offset of start of Load offset of start of destdest. String. String

in in ES into DI ES into DI

CLD ; CLD ; Clear DF to auto increment SIClear DF to auto increment SI

andand DI after moveDI after move

MOV CX, 04H ; MOV CX, 04H ; Load length of string into CX asLoad length of string into CX as

coucounternter

NEXT: MOVSBNEXT: MOVSB

LOOP NEXT ; LOOP NEXT ; Move string byte until CX = 0 Move string byte until CX = 0

Page 38: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez38

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Move StringMove String––MOVSB/MOVSWMOVSB/MOVSW

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Loads StringLoads String––LODSB/LODSWLODSB/LODSW

�� LODS / LODSB / LODSW (LODS / LODSB / LODSW (LOADLOAD STRING STRING BYTEBYTE

INTO INTO ALAL OR STRING OR STRING WORDWORD INTO INTO AXAX))

�� This instruction copies a byte from a string location This instruction copies a byte from a string location

pointed to by pointed to by SISI to to ALAL, or a word from a string , or a word from a string

location pointed to by location pointed to by SISI to to AXAX. .

�� If If DFDF is is 00, , SISI will be automatically will be automatically incrementedincremented (by (by 11

for a for a byte stringbyte string, and , and 22 for a for a word stringword string) to point to ) to point to

the next element of the string. the next element of the string.

�� If If DFDF is is 11, , SISI will be automatically will be automatically decrementeddecremented (by (by 11

for a for a byte stringbyte string, and , and 22 for a for a word stringword string) to point to ) to point to

the previous element of the string. the previous element of the string.

�� LODSLODS does not affect any flag. does not affect any flag.

Page 39: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez39

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Loads StringLoads String––LODSB/LODSWLODSB/LODSW

��Loads Loads ALAL or or AXAX with data at segment offset with data at segment offset

address indexed by the address indexed by the SISI register. register.

��a a 11 is added to or subtracted from is added to or subtracted from SISI for a for a bytebyte--

sized sized LODSLODS

��a a 2 2 is added or subtracted for a is added or subtracted for a wordword--sizedsized LODSLODS..

��The following Figure shows the The following Figure shows the LODSWLODSW

instruction.instruction.

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Loads StringLoads String––LODSB/LODSWLODSB/LODSW

The operation of the The operation of the LODSWLODSW instruction if DS=1000H, D=0, instruction if DS=1000H, D=0,

11000H=32, 11001H=A0. This instruction is shown after AX is 11000H=32, 11001H=A0. This instruction is shown after AX is

loaded from memory, but before SI increments by 2.loaded from memory, but before SI increments by 2.

Page 40: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez40

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Loads StringLoads String––LODSB/LODSWLODSB/LODSW

Form of the LODS instructionForm of the LODS instruction

CLD CLD

; ; Clear direction flag so that SI is autoClear direction flag so that SI is auto--incrementedincremented

MOV SI, OFFSET SOURCE MOV SI, OFFSET SOURCE

; ; Point SI to start of stringPoint SI to start of string

LODS SOURCE LODS SOURCE

; ; Copy a byte or a word from string to AL or AX Copy a byte or a word from string to AL or AX

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Stores StringStores String––STOSB/STOSWSTOSB/STOSW

�� STOS / STOSB / STOSW (STOS / STOSB / STOSW (STORESTORE STRING STRING BYTEBYTE OR OR

STRING STRING WORDWORD))

�� This instruction copies a byte from This instruction copies a byte from ALAL or a word from or a word from AXAX

to a memory location in the extra segment pointed to by to a memory location in the extra segment pointed to by DIDI. .

�� In effect, it replaces a string element with a byte from In effect, it replaces a string element with a byte from ALAL

or a word from or a word from AXAX. After the copy, . After the copy, DIDI is automatically is automatically

incrementedincremented or or decrementeddecremented to point to to point to nextnext or or previousprevious

element of the string. element of the string.

�� If If DFDF is cleared, then is cleared, then DIDI will automatically incremented by will automatically incremented by

11 for a byte string and by for a byte string and by 22 for a word string. for a word string.

�� If If DIDI is set, is set, DIDI will be automatically decremented by will be automatically decremented by 11 for a for a

byte string and by byte string and by 22 for a word string. for a word string.

�� STOSSTOS does not affect any flag. does not affect any flag.

Page 41: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez41

Prof. Fayez F. M. El-Sousy Form of the STOS instructionForm of the STOS instruction

String InstructionsString Instructions

Stores StringStores String––STOSB/STOSWSTOSB/STOSW

MOV DI, OFFSET TARGET MOV DI, OFFSET TARGET

STOSB STOSB

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Compare StringCompare String––CMPSB/CMPSWCMPSB/CMPSW

�� CMPS / CMPSB / CMPSW (CMPS / CMPSB / CMPSW (COMPARECOMPARE STRING STRING

BYTEBYTE OR STRING OR STRING WORDWORD))

�� This instruction can be used to compare a This instruction can be used to compare a byte / wordbyte / word

in one string with a in one string with a byte / wordbyte / word in another string. in another string. SISI is is

used to hold the offset of the byte or word in the source used to hold the offset of the byte or word in the source

string, and string, and DIDI is used to hold the offset of the byte or is used to hold the offset of the byte or

word in the destination string. word in the destination string.

�� The The AFAF, , CFCF, , OFOF, , PFPF, , SFSF, and , and ZFZF flags are affected by flags are affected by

the comparison, but the two operands are not affected. the comparison, but the two operands are not affected.

After the comparison, After the comparison, SISI and and DIDI will automatically be will automatically be

incrementedincremented or or decrementeddecremented to point to the next or to point to the next or

previous element in the two strings. previous element in the two strings.

Page 42: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez42

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Compare StringCompare String––CMPSB/CMPSWCMPSB/CMPSW

�� CMPS / CMPSB / CMPSW (CMPS / CMPSB / CMPSW (COMPARECOMPARE STRING STRING

BYTEBYTE OR STRING OR STRING WORDWORD))

�� If If DFDF is is setset, then , then SISI and and DIDI will automatically be will automatically be

decrementeddecremented by by 11 for a for a bytebyte string and by string and by 22 for a for a wordword

string. string.

�� If If DFDF is is resetreset, then , then SISI and and DIDI will automatically be will automatically be

incrementedincremented by by 11 for for bytebyte strings and by strings and by 2 2 for for wordword

strings. strings.

�� The string pointed to by The string pointed to by SISI must be in the must be in the data data

segmentsegment. .

�� The string pointed to by The string pointed to by DIDI must be in the must be in the extra extra

segmentsegment..

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Compare StringCompare String––CMPSB/CMPSWCMPSB/CMPSW

�� The The CMPSCMPS instruction can be used with a instruction can be used with a REPEREPE or or REPNEREPNE prefix prefix

to compare all the elements of a string.to compare all the elements of a string.

MOV SI, OFFSET FIRST MOV SI, OFFSET FIRST

; ; Point SI to source stringPoint SI to source string

MOV DI, OFFSET SECOND MOV DI, OFFSET SECOND

; ; Point DI to destination stringPoint DI to destination string

CLD DF CLD DF

; ; cleared, SI and DI will autocleared, SI and DI will auto--increment after compareincrement after compare

MOV CX, 100 MOV CX, 100

; ; Put number of string elements in CXPut number of string elements in CX

REPE CMPSB REPE CMPSB

; ; Repeat the comparison of string bytes until end of Repeat the comparison of string bytes until end of

string or until compared bytes are not equalstring or until compared bytes are not equal

Page 43: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez43

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Compare StringCompare String––CMPSB/CMPSWCMPSB/CMPSW

�� CXCX functions as a counter, which the functions as a counter, which the REPEREPE prefix prefix

will cause will cause CXCX to be decremented after each compare. to be decremented after each compare.

�� The The BB attached to attached to CMPSCMPS tells the assembler that the tells the assembler that the

strings are of type byte. If you want to tell the strings are of type byte. If you want to tell the

assembler that strings are of type word, write the assembler that strings are of type word, write the

instruction as instruction as CMPSWCMPSW. .

�� The The REPEREPE CMPSWCMPSW instruction will cause the instruction will cause the

pointers in pointers in SISI and and DIDI to be to be incrementedincremented by 2 after by 2 after

each compare, if the direction flag is set. each compare, if the direction flag is set.

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Scan StringScan String––SCASB/SCASWSCASB/SCASW

�� SCAS / SCASB / SCASW (SCAS / SCASB / SCASW (SCANSCAN A STRING A STRING BYTEBYTE OR OR

A STRING A STRING WORDWORD))

�� SCASSCAS compares a compares a bytebyte in in ALAL or a or a wordword in in AXAX with a with a

byte or a word in byte or a word in ESES pointed to by pointed to by DIDI. Therefore, the . Therefore, the

string to be scanned must be in the string to be scanned must be in the extra segmentextra segment, and , and

DIDI must contain the offset of the must contain the offset of the bytebyte or the or the wordword to be to be

compared. compared.

�� If If DFDF is cleared, then is cleared, then DIDI will be will be incrementedincremented by by 11 for for

bytebyte strings and by strings and by 22 for for wordword strings. strings.

�� If If DFDF is set, then is set, then DIDI will be will be decrementeddecremented by by 11 for byte for byte

strings and by strings and by 22 for word strings. for word strings. SCASSCAS affects affects AFAF, , CFCF, ,

OFOF, , PFPF, , SFSF, and , and ZFZF, but it does not change either the , but it does not change either the

operand in operand in ALAL ((AXAX) or the operand in the string.) or the operand in the string.

Page 44: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez44

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Scan StringScan String––SCASB/SCASWSCASB/SCASW

�� SCAS / SCASB / SCASW (SCAS / SCASB / SCASW (SCANSCAN A STRING A STRING BYTEBYTE OR OR

A STRING A STRING WORDWORD))

�� The following program segment scans a text string of The following program segment scans a text string of 8080

characters for a carriage return, characters for a carriage return, 0DH0DH, and puts the , and puts the

offset of string into offset of string into DIDI::

MOV DI, OFFSET STRING MOV DI, OFFSET STRING

MOV AL, 0DH ; MOV AL, 0DH ; Byte to be scanned for into ALByte to be scanned for into AL

MOV CX, 80 ; MOV CX, 80 ; CX used as element counterCX used as element counter

CLD ; CLD ; Clear DF, so that DI auto incrementsClear DF, so that DI auto increments

REPNE SCASB ; REPNE SCASB ; Compare byte in string with byte in AL Compare byte in string with byte in AL

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Scan StringScan String––SCASB/SCASWSCASB/SCASW

�� SCAS / SCASB / SCASW (SCAS / SCASB / SCASW (SCANSCAN A STRING A STRING BYTEBYTE OR OR

A STRING A STRING WORDWORD))

�� The following program using SCASB instruction that The following program using SCASB instruction that

implement a string scan operation:implement a string scan operation:

MOV DI, MOV DI, OFFSET STRINGOFFSET STRING

MOV AL, 0DH ; MOV AL, 0DH ; Byte to be scanned for into ALByte to be scanned for into AL

MOV CX, 80 ; MOV CX, 80 ; CX used as element counterCX used as element counter

CLD ; CLD ; Clear DF, so that DI auto incrementsClear DF, so that DI auto increments

AGAIN:AGAIN: SCASB ; SCASB ; Compare byte in string with byte in ALCompare byte in string with byte in AL

LOOPNE LOOPNE AGAINAGAIN

NEXT:NEXT:

Page 45: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez45

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Repeat StringRepeat String––REPE/REPZ/REPNE/REPNZREPE/REPZ/REPNE/REPNZ

�� REP / REPE / REPZ / REPNE / REPNZ (PREFIX) REP / REPE / REPZ / REPNE / REPNZ (PREFIX)

((REPEATREPEAT STRING INSTRUCTION UNTIL STRING INSTRUCTION UNTIL

SPECIFIED CONDITIONS EXIST)SPECIFIED CONDITIONS EXIST)

�� REPREP is a prefix, which is written before one of the string is a prefix, which is written before one of the string

instructions. It will cause the instructions. It will cause the CXCX register to be register to be

decremented and the string instruction to be repeated decremented and the string instruction to be repeated

until until CX = 0. CX = 0.

�� The instruction The instruction REP MOVSBREP MOVSB, for example, will continue , for example, will continue

to copy string bytes until the number of bytes loaded into to copy string bytes until the number of bytes loaded into

CXCX has been copied. has been copied.

�� REPEREPE and and REPZREPZ are two mnemonics for the same are two mnemonics for the same

prefix. prefix.

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Repeat StringRepeat String––REPE/REPZ/REPNE/REPNZREPE/REPZ/REPNE/REPNZ

�� REP / REPE / REPZ / REPNE / REPNZ (PREFIX) REP / REPE / REPZ / REPNE / REPNZ (PREFIX)

((REPEATREPEAT STRING INSTRUCTION UNTIL STRING INSTRUCTION UNTIL

SPECIFIED CONDITIONS EXIST)SPECIFIED CONDITIONS EXIST)

�� They stand for They stand for repeat if equalrepeat if equal and and repeat if zerorepeat if zero, ,

respectively. respectively.

�� They are often used with the They are often used with the Compare String instructionCompare String instruction

or with the or with the Scan String instructionScan String instruction. .

�� They will cause the string instruction to be repeated as They will cause the string instruction to be repeated as

long as the compared long as the compared bytesbytes or or wordswords are equal (are equal (ZF = 1ZF = 1) )

and and CXCX is not yet counted down to zero. In other words, is not yet counted down to zero. In other words,

there are two conditions that will stop the repetition: there are two conditions that will stop the repetition: CXCX

= 0= 0 or string or string bytesbytes or or wordswords not equal.not equal.

Page 46: Program Control Instructions in Microprocessor … change can be through a condition or sometimes unconditional. The conditions are represented by flags. Program Control Instructions

Fayez46

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Repeat StringRepeat String––REPE/REPZ/REPNE/REPNZREPE/REPZ/REPNE/REPNZ

�� REP / REPE / REPZ / REPNE / REPNZ (PREFIX) REP / REPE / REPZ / REPNE / REPNZ (PREFIX)

((REPEATREPEAT STRING INSTRUCTION UNTIL STRING INSTRUCTION UNTIL

SPECIFIED CONDITIONS EXIST)SPECIFIED CONDITIONS EXIST)

�� REPNEREPNE and and REPNZREPNZ are also two mnemonics for the are also two mnemonics for the

same prefix. same prefix.

�� They stand for They stand for repeat if not equalrepeat if not equal and and repeat if not zerorepeat if not zero, ,

respectively. respectively.

�� They are often used with the They are often used with the Compare String instructionCompare String instruction

or with the or with the Scan String instructionScan String instruction. .

�� They will cause the string instruction to be repeated as They will cause the string instruction to be repeated as

long as the compared long as the compared bytesbytes or or wordswords are not equal (are not equal (ZF = ZF =

00) and ) and CXCX is not yet counted down to zero.is not yet counted down to zero.

Prof. Fayez F. M. El-Sousy

String InstructionsString Instructions

Repeat StringRepeat String––REPE/REPZ/REPNE/REPNZREPE/REPZ/REPNE/REPNZ

�� REP / REPE / REPZ / REPNE / REPNZ (PREFIX) REP / REPE / REPZ / REPNE / REPNZ (PREFIX)

((REPEATREPEAT STRING INSTRUCTION UNTIL STRING INSTRUCTION UNTIL

SPECIFIED CONDITIONS EXIST)SPECIFIED CONDITIONS EXIST)

REPE CMPSB ;REPE CMPSB ; Compare string bytes until end ofCompare string bytes until end of

string or until string bytstring or until string bytes not equal.es not equal.

REPNE SCASW ;REPNE SCASW ; Scan a string of word until a word inScan a string of word until a word in

the string matches the worthe string matches the word in AX ord in AX or

until all of the string hauntil all of the string has been s been

scanned.scanned.