0% found this document useful (0 votes)
9 views41 pages

Chap - 5 Mp

Chapter Five of the document discusses program control instructions in microprocessors, focusing on jump instructions, including unconditional and conditional jumps. It explains how these instructions control program flow, with examples of short, near, and far jumps, as well as procedures and interrupts. The chapter emphasizes the importance of these instructions in programming and their role in decision-making and code reuse.

Uploaded by

gbonsa2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views41 pages

Chap - 5 Mp

Chapter Five of the document discusses program control instructions in microprocessors, focusing on jump instructions, including unconditional and conditional jumps. It explains how these instructions control program flow, with examples of short, near, and far jumps, as well as procedures and interrupts. The chapter emphasizes the importance of these instructions in programming and their role in decision-making and code reuse.

Uploaded by

gbonsa2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Microprocessor and Assembly

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.

1byt 1byt 1byt


e e e
CS = 1000H
Current IP = 0002H
New IP = 0005H

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.

• The JMP UP instruction in the next slides example references a far


label since it calls a label UP that is declared EXTRN and FAR.
– The label UP is defined as a far label by the EXTRN UP: FAR
directive.

01/27/22
Example(Far Jump)
• Example: lists a short program that uses a far
jump instruction.

EXTERN UP: FAR ; type 2


indicates UP is a
XOR BX,BX
far label
START: ADD AX,1
JMP NEXT
<skipped memory locations>
<skipped memory locations>
NEXT: MOV BX,AX
JMP FAR START ; type 1
JMP UP references a far
label
01/27/22
Jumps with Register Operands
• The jump instruction can also use a 16-bit, 32-bit or 64-bit register
as an operand.
• This automatically sets up the instruction as an indirect jump.
• The address of the jump is in the register specified by the jump
instruction.
• Unlike the displacement associated with the near jump, the
contents of the register are transferred directly into the instruction
pointer.
• An indirect jump does not add to the instruction pointer, as with
short and near jumps.

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:

TOP: ADD CX, BX


JC EXIT This program will check the value
JMP TOP of CF to Jump to EXIT label.

EXIT: MOV CX, 00H


Check what will happen setting values of
MOV BX, 00H – CX = 02H and BX = 0FFFFH
01/27/22
– CX = 02H and BX = 0FFFDH
LOOP Instruction
• The loop instruction is a combination of a decrement CX and the
JNZ conditional jump. So, it by default executes this two
instructions.
• That is, LOOP decrement CX and check for the value of CX, if CX!
=0, it jumps to the address indicated by the label.
• But if CX becomes 0, the program will proceeds to the next
sequential instruction.
• Example: Assembly language program to find sum of the following
series using LOOP instruction. 1+2+3+……….+10
Solution: In C++,
Sum = 1
• How we perform this in assembly
for(i=2, i < 10, i++) program then?
01/27/22 { Sum = Sum + i; }
Loop Instruction(Cont...)

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

• As we can see from the previous example, loop instruction by


default executes;

decrement the count register, CX and
01/27/22

Jump if not Zero, JNZ statements.
Procedures
• The procedure (subroutine, method, or function) is an important
part of any computer system’s architecture.
• It is a group of instructions that usually performs particular task.
• It allows the same piece of code to be reused multiple times.

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:

SUMS PROC NEAR Then if you want to call this


ADD AX,BX procedure somewhere in your
ADD AX,CX program you can use:
ADD AX,DX
RET CALL SUMS ;for the first
SUMS ENDP procedure.

SUMS1 PROC FAR


CALL SUMS1 ;for the second
---------------
procedure
---------------
RET
SUMS1 ENDP
01/27/22
• Some emulator may not support procedures of FAR type!
Procedures Cont…..
– Procedures that are to be used by all software (global) should be
written as far procedures.
– Procedures that are used by a given task (local) are normally
defined as near procedures.
– Most procedures are near procedures.
Don’t Forget!
• Once written a procedure in our program how we call it the main
program?
– We use the CALL instruction along with the procedure name.
– That is CALL Proc_name.

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

SUMS1 PROC near


01/27/22 INC ax
RET ; when the instruction reaches here it go back to
CALL instruction Cont….
• Whenever a CALL instruction executes it:
– Pushes the IP or, CS:IP on the stack.
– Changes the value of IP or, CS:IP.
– Jumps to the procedure by new IP or, CS:IP address.
• Difference between JMP and CALL instruction:
• If a JMP instruction is executed, we jump to the destination
location, and the execution carries on from there, without bothering
to come back later to the instruction after the JMP.
• If a CALL instruction is executed, we jump to the subroutine, and
the execution carries on from there till the RET instruction is
executed in the subroutine, and then we come back to the instruction
after the CALL in the main program.
01/27/22
CALL instruction Cont….

• The return (RET) instruction:


– removes a 16-bit number (near return) from the stack and places
it into IP or
– removes a 32-bit number (far return) and places it into IP and CS.
• The near and far return instructions are both defined in the
procedure’s PROC directive(indirectly), which automatically selects
the proper return instruction.

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…..

INTO: Interrupt overflow also represented as INT 4

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…

• As we have said 4byte is required for storing the


address of each interrupt, thus:
• The address of the interrupt vector is determined
by multiplying the interrupt type number by 4.
• For example, INT 100 uses interrupt vector
number 100, which appears at memory address
100 X 4 = 400, but this is in decimal so we have
to convert into Hexadecimal:
– i.e 190H, thus the memory address of
INT 100 is 190H – 193H.

01/27/22
Interrupt Vectors…

• Example2:Find the memory address/interrupt


vector for the following instruction:
INT 10H
Solution:
• This instruction calls the interrupt service
procedure whose address is stored beginning at
memory location:
4 X 10 = 40 = 28H
• Therefore, INT 10H will be stored at memory
addresses: 28H - 31H.

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

You might also like