Chap - 5 Mp
Chap - 5 Mp
Language
Chapter - Five
Program Control Instructions
01/27/22
Outline
The Jump Instruction
– Unconditional Jump (JMP)
– Conditional Jumps
Controlling the Flow of the Program
– Loop
– Procedures
– CALL
– RET
Introduction to Interrupts
01/27/22 1
The Jump Instruction
• The program control instructions direct the flow of a program and
allow the flow to change.
• A change in flow often occurs after a decision made with the CMP
or TEST instruction.
• The main program control instruction, the jump (JMP), allows the
programmer to skip sections of a program and branch to any part of
the memory for the next instruction.
• It can be classified as;
i. conditional jump and
ii. unconditional jump.
• A conditional jump instruction allows the programmer to make
decisions based upon numerical tests.
01/27/22
The Jump Instruction
• The results of numerical tests are held in the flag bits, which are
then tested by conditional jump instructions.
• In this section of the text, all jump instructions are illustrated with
their uses in sample programs.
01/27/22
Unconditional Jump (JMP)
• It does not depend any condition or numerical tests.
• Three types of unconditional jump instructions are available:
a. short jump,
b. near jump, and
c. far jump.
• The short and near jumps are often called intrasegment jumps,
and the far jumps are often called intersegment jumps.
• Short jump and near jump follows a distance or displacement to
jump where as far jump follows an address (segment + offset) to
jump.
01/27/22
Unconditional Jump (JMP)
• Here below is the three main forms of unconditional jump
instruction: short, near and far jumps:
01/27/22
Short Jump
• Short jump is a two - byte instruction.
• They are also called relative jumps.
• Instead of a jump address, a distance, or displacement, follows the
opcode.
• The short jump displacement is a distance represented by a 1 - byte
number(8 - bit) whose value ranges between +128 and -128.
• It allows jumps or branches to memory location within ±128 from
the address following the jump.
28 = 256 = +128 byte to -128 bytes
• When the microprocessor executes a short jump, the displacement is
sign extended and added to the instruction pointer (IP) to generate
the jump address.
– That is, the short jump instruction branches to this new address
for the next instruction in the program.
01/27/22
Example(Short Jump)
CS = 1000H
current IP = 0001H
new IP = 0006H
01/27/22
Cont…
• Example: shows how short jump instructions pass control from
one part of the program to another.
XOR BX, BX
START: MOV AX, 1
ADD AX, BX
JMP SHORT
NEXT
<skipped memory
locations>
<skipped memory
locations>
NEXT: MOV BX,AX
• Whenever a jump instruction
JMP STARTreferences an address, a label
normally identifies the address.
• The JMP SHORT NEXT instruction in above example jumps to
label NEXT for the next instruction.
01/27/22
Cont…
• Note that,
– The label NEXT must be followed by a colon (NEXT:) to
allow an instruction to reference it for a jump.
– If a colon does not follow a label, you cannot jump to it.
– The only time a colon is used after a label is when the label
is used with a jump or call instruction.
01/27/22
Near Jump
• The near jump is similar to the short jump, except that the
distance is farther.
• The near jump is a 3 - byte instruction that contains an opcode
followed by a 2 - byte displacement(16-bit ).
• A near jump passes control to an instruction in the current code
segment located within ±32K bytes from the near jump
instruction.
216 = 65536 = 64 Kbyte = +32 Kbyte to -32 Kbyte
• The distance is ±2G in the 80386 and above when operated in
protected mode.
– i.e the 80386 through Pentium 4 processors, the displacement
is 32 bits and the near jump is 5 bytes long.
232= 4G = +2G to -2G
01/27/22
Cont...
• The signed displacement adds to the instruction pointer(IP) to
generate the jump address.
• Because the displacement is in the range of ±32K, near jump can
jump to any memory location within the current real mode code
segment.
01/27/22
Cont...
• The near jump is also a relative jump.
• If the code segment moves to a new location in the memory, the
distance between the jump instruction and the operand address
remains the same.
– That is; what ever the value of segment register is, the JMP
instruction and its corresponding operand is always stored
consecutively.
• This allows a code segment to be relocated by simply moving it.
• This feature, along with the relocatable data segments, makes the
Intel family of microprocessors ideal for use in a general-purpose
computer system.
01/27/22
Example(Near Jump)
• Example: below shows the same basic program that appeared in
previous example, except that the jump distance is greater.
– Note: JMP without the type of JMP specified in the instruction
is typical near JMP.
XOR BX,BX
START: MOV AX,1
ADD AX,BX
JMP NEXT ;or you can add
NEAR
<skipped memory locations>
<skipped memory locations>
NEXT: MOV BX,AX
JMP START
01/27/22
Far Jump
• A far jump instruction obtains a new segment and offset address to
accomplish the jump. It is a 5 byte instruction.
• Bytes 2 and 3 of this 5 - byte instruction contain the new offset
address; bytes 4 and 5 contain the new segment address.
Offset Address
Segment Address
• The offset address, contains the offset address within the new code
segment.
• It is always given with OffsetAddress : SegmentAddress order
along with JMP instruction.
01/27/22
Cont...
• It allows jumps to any memory location of any memory segment.
That’s why far jump is called intersegment jump.
01/27/22
Cont...
• There are two ways by which we can use far jump in assembly
program:
1. the far jump instruction appearing with the FAR directive.
2. by defining a label as a far label along with the EXTRN
keyword (but not supported in 8086emu).
– A label is far only if it is external to the current code segment.
01/27/22
Example(Far Jump)
• Example: lists a short program that uses a far
jump instruction.
01/27/22
Cont...
• f
Example: JMP AX
• The above instruction copies the contents of the AX register into
the IP when the jump occurs.
• This allows a jump to any location within the current code
segment.
• In the 80386 and above, a JMP EAX instruction also jumps to any
location within the current code segment;
01/27/22
Conditional Jump
• A conditional jump instruction allows the programmer to make
decision based upon numerical tests.
• The conditional jump instructions are always short jump in 8086.
• Conditional jump instructions test the following flag bits: sign
(S), zero (O), 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.
• For example, a JC will jump if the carry bit is set.
01/27/22
Conditional Jump
• Examples of some common Conditional Jump instructions:
Example:
MOV CX, 03H
This program will execute the
MOV AX, 1H instruction inside XXX label(ADC)
MOV BX, 4H until the value of CX becomes 0.
XXX : ADC AX, BX
DEC CX
01/27/22
JNZ XXX ; jump if result (value of CX) not zero
Conditional Jump(Example2)
Example2:
MOV cx, 09h ;num of addition MOV cx, 09h ;num of addition
needed needed
MOV ax, 1h MOV ax, 1h
MOV bx, 2h MOV bx, 2h
adder: adder:
ADC ax, bx ADC ax, bx
INC bx ; add bx, 1h INC bx ; add bx, 1h
DEC cx LOOP adder
JNZ adder
Advantage:
– It is a reusable section of the software that is stored in memory
once, but used as often as necessary.
– It saves memory space and program development time,
– Makes it easier to develop software.
01/27/22
Procedures Cont…..
How procedure links with main program?
– The CALL instruction links to the procedure, and the RET
(return) instruction returns from the procedure.
– The stack stores the return address whenever a procedure is called
during the execution of a program.
– The CALL instruction pushes the address of the instruction
following the CALL (return address) on the stack.
What are the specific rules for using procedures?
– A procedure begins with the PROC directive and ends with the
ENDP directive.
– Each directive appears with the name of the procedure.
– The PROC directive is then followed by the type of procedure:
NEAR (intrasegment) or FAR (intersegment).
01/27/22
Procedures Cont…..
• Format of Procedure
Proc_Name PROC NEAR/FAR
…………...………
…………………...
………….………..
RET
Proc_Name ENDP
Note:
– Both procedure name should be same.
– To call a procedure in main program write:
CALL Proc_name
01/27/22
Procedures Cont…..
Example:
01/27/22
Procedures Cont…..
CALL instruction
• The CALL instruction transfers the flow of program to the procedure.
• The CALL instruction differs from the jump instruction because a
CALL saves a return address on the stack.
• The return address returns control to the instruction that immediately
follows the CALL in a program when a RET instruction executes.
MOV ax, 1h
MOV bx, 2h
CALL SUMS1
MOV cx, 3h
MOV dx, 4h
01/27/22
Interrupts
• An interrupt is a condition that halts the microprocessor temporarily
to work on a different task.
• An interrupt is either a:
– hardware - generated CALL (externally derived from a hardware
signal) or
– software - generated CALL (internally derived from the execution
of an instruction or some other internal event) that allow normal
program execution to be interrupted (stopped).
• In response to an interrupt, the microprocessor stops execution of its
current program and calls a procedure called interrupt service
procedure (ISP).
01/27/22
Interrupts…..
• How an interrupt is used?
INT nn; where nn indicates interrupt vector number
nn’s value is (0 to 255 i.e. 00H to FFH).
• Each INT instruction is 2-byte long.
– 1st byte contain opcode and
– 2nd byte contains vector type number.
• The vector type number is 1byte in size, and hence there are:
28 = 256 different interrupts.
INTO: Interrupt overflow also represented as INT 4
01/27/22
Interrupts…..
01/27/22
Interrupt Vector
• When an interrupt occurs, the CPU runs the interrupt handler. But
where is this handler found? In the interrupt vector table.
• An interrupt vector is the memory address of an interrupt handler,
or an index into an array called an interrupt vector table.
• It is the 4 byte long (CS:IP) stored in the first 1024 bytes of the
memory (00000H–003FFH) when the microprocessor operates in
the real mode. That is,
– Since we have 256 interrupts and each of them is stored in a
4byte memory addresses, and hence needs 256 x 4 = 1024
memory addresses.
– memory address: 0 - 1023 = 00000H – 003FFH.
01/27/22
Interrupt vector…
• Below shows the interrupt vectors and the memory location of each
vectors.
• Each vector contains a value for IP and CS that forms the address of
the interrupt service procedure.
• The first 2 bytes contain the IP, and the last 2 bytes contain the CS.
01/27/22
Interrupt Vectors…
01/27/22
Interrupt Vectors…
01/27/22
Quiz(5%)
1. Find the interrupt address for INT 23H instructions.
2. Find the Jump address for the instruction given below:
JMP 1003:3A33
3. Why do we use procedures in assembly program? Justify.
4. Differentiate between Jump and Procedure.
01/27/22