Program Control Instruction
Program Control Instruction
2
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
This presentation slide only contains the overview of the related
topics. Students are advised to take decent class-notes and read
thoroughly from the prescribed text books.
3
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Introduction
4
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
THE JUMP GROUP
• Allows programmer to skip program sections and
branch to any part of memory for the
next instruction.
• A conditional jump instruction allows decisions based
upon numerical tests.
– results are held in the flag bits, then tested by conditional
jump instructions
• LOOP and conditional LOOP are also forms
of the jump instruction.
5
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Unconditional Jump (JMP)
Far Jump
6
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Unconditional Jump (JMP)…
• Short jump is a 2-byte instruction that allows jumps to
memory locations within +127 and –128 bytes from the
address following the jump
• 3-byte near jump allows a jump within ±32K bytes from the
instruction in the current code segment. (i.e. anywhere in the
current code segment)
8
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Short Jump
• Called relative jumps because they can be moved, with
related software, to any location in the current code segment
without a change. Jump address is not stored with the opcode
– a distance, or displacement, follows the opcode. The short jump
displacement is a distance represented by a 1-byte signed number
whose value ranges between +127 and –128.
0009
– The instruction
branches to this
new address for
the next instruction
in the program
10
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Short Jump…
11
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Near Jump
• A near jump passes control to an instruction in the current
code segment located within ±32K bytes from the near jump
instruction.
– Near jump is a 3-byte instruction with opcode followed by a signed 16-
bit displacement. because signed displacement is ±32K, a near jump
can jump to any memory location within the current real mode code
segment. The near jump is also relocatable because it is also a relative
jump.
The address of the next instruction (000AH) is added to the displacement (01F6H) of
the first jump, the address of NEXT is at location
(000AH + 01F6H) or 0200H. 12
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Problem!
• Find the value of X. Is it short or near or far jump?
13
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Near Jump
Figure 6–3 A near jump that adds the displacement (0002H) to the contents of IP.
14
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Far Jump
• Obtains a new segment and offset address
to accomplish the jump:
– bytes 2 and 3 of this 5-byte instruction contain
the new offset address
– bytes 4 and 5 contain the new segment address.
15
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Far Jump…
Figure 6–4 A far jump instruction replaces the contents of both CS and IP with 4
bytes following the opcode.
EA 2701 00A3
16
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Jumps with Register Operands
• Jump can also use a 16 bit register as an operand.
– address of the jump is in the register specified
by the jump instruction
• Unlike displacement associated with the near jump, register
contents are transferred directly into the instruction pointer.
• Example:
MOV AX, 1009H
JMP AX
– copies the contents of the AX register into the IP.
– allows a jump to any location within the current code
segment
17
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Conditional Jumps and Conditional Sets
• Always short jumps in 8086.
– limits range to within +127 and –128 bytes from the
location following the conditional jump
• Allows a conditional jump to any location within the current
code segment.
• Conditional jump instructions test flag bits:
sign (S), zero (Z), carry (C), parity (P), overflow (0)
• If the condition under test is true, a branch to the label
associated with the jump instruction occurs.
• if false, next sequential step in program executes
• Example: inatruction JC will jump if the carry bit is set (CF=1)
18
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Conditional Jumps and Conditional Sets…
19
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
LOOP
• A combination of a decrement CX and the JNZ conditional
jump.
• In 8086 LOOP decrements CX.
– if CX != 0, it jumps to the address indicated
by the label
– If CX becomes 0, the next sequential instruction executes
20
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Problem!
• Using jump instruction, write an assembly language program
for the following series.
i. 1+2+3+4+5+ ………… + ∞
ii. 1+2+3+4+5+ ………… + 100
iii. 5+6+8+11+15+ ………… + 110
iv. 1+3+5+7+9+ ………… + ∞
v. 2+4+6+8+10+ ………… + 200
vi. 10+20+30+40+ ………… + 500
vii. 1+5+9+13+17+ ………… + 107
viii. 1+5+6+11+17+28+ …… + ∞
100
ix. 𝑋=0
(X + 1)
x. 12+22+32+42+52+ ………… + ∞
21
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
CONTROLLING THE FLOW OF THE PROGRAM
22
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
CONTROLLING THE FLOW OF THE PROGRAM…
23
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
.WHILE - .ENDW
• .WHILE
logic
operation
.ENDW
MOV AX, 5500H
MOV BX, 0001H
.WHILE
AL != 05 H
ADD AX, BX
.ENDW
24
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
.REPEAT- .UNTIL
• .REPEAT
operation
.UNTIL
logic
MOV AX, 5500H
MOV BX, 0001H
.REPEAT
ADD AX, BX
.UNTIL
AL=06H
25
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
.IF - .ENDIF
• .IF
logic
operation
.ENDIF
MOV AX, 30FF H
.IF
AL >= FF && AH< 33
MOV BX, 0DEF H
ADD AX, BX
.END IF
26
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
IMPORTANT
27
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
PROCEDURES
• A procedure (also known as subroutine or function) is a
group of instructions that usually performs one task. A
procedure is a reusable section of the software stored in
memory once, used as often as necessary.
– It has many advantages. Like it saves memory space and
makes it easier to develop software.
– Only disadvantage of procedure is time it takes the computer
to link to, and return from it.
• A procedure begins with the PROC directive and ends with the
ENDP directive.
– each directive appears with the procedure name
28
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
PROCEDURES…
• CALL links to the procedure; the RET (return) instruction
returns from the procedure
• CALL pushes the address of the instruction following the CALL
(return address) on the stack.
– the stack stores the return address when a procedure is
called during a program
• RET instruction removes an address from the stack so the
program returns to the instruction following the CALL.
29
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
PROCEDURES…
30
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
CALL
• CALL instruction transfers the flow of the program to the
procedure.
• It 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.
31
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Near CALL
• 3 bytes long.
– the first byte contains the opcode; the second and third
bytes contain the displacement (within ±32K bytes)
• When the near CALL executes, it first pushes the offset
address of the next instruction onto the stack.
– offset address of the next instruction appears in the
instruction pointer (IP)
• It then adds displacement from bytes 2 & 3 to the IP to transfer
control to the procedure.
32
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Near CALL…
The effect of a near CALL on the stack and the instruction pointer
After CALL,
IP= (0003 H+0FFF H)
= 1002 H
33
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Far CALL
• 5-byte instruction contains an opcode followed by the next
value for the IP and CS registers.
– bytes 2 and 3 contain new contents of the IP
– bytes 4 and 5 contain the new contents for CS
• Far CALL places the contents of both IP and CS on the stack
before jumping to the address indicated by bytes 2 through 5.
• This allows far CALL to call a procedure located anywhere in
the memory and return from that procedure.
34
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Far CALL…
The effect of a far CALL instruction
35
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
CALLs with Register Operands
• Like jump instructions, call instructions also may contain a
register operand.
• An example CALL BX, which pushes the contents of IP onto the
stack.
– then jumps to the offset address, located in register BX, in
the current code segment
• Always uses a 16-bit offset address, stored in any 16-bit
register except segment registers.
36
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
RET
• Removes a 16-bit number (near return) from the stack placing
it in IP, or removes a 32-bit number (far return) and places it in
IP & CS.
The effect of a near return instruction on the stack and instruction pointer
37
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
INTERRUPTS
• An interrupt is a hardware-generated CALL or a software-
generated CALL
– Hardware interrupts occur when a peripheral device asserts
an interrupt input pin of the microprocessor.
– Internal interrupts are initiated by the state of the CPU (e.g.
divide by zero error) or by an instruction. At times an internal
interrupt is called an exception.
• Either type interrupts the program by calling
an interrupt service procedure (ISP) or interrupt handler.
38
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
INTERRUPTS…
• This section explains software interrupts, which are special
types of CALL instructions.
• This section describes the three types of software interrupt
instructions.
INT
INTO
INT 3
• In the real mode, each of these instructions fetches a vector
from the vector table, and then calls the procedure stored at the
location addressed by the vector.
39
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Interrupt Vectors
• An interrupt vector is a 4-byte number stored in the first 1024
bytes of memory in real mode.
• 256 different interrupt vectors.
– each vector contains the address of an interrupt service
procedure.
• Each vector contains a value for IP and CS that forms the
address of the interrupt service procedure.
– the first 2 bytes contain IP; the last 2 bytes CS
• Intel reserves the first 32 interrupt vectors (0-31) for the
present and future products.
• Remaining interrupt vectors (32–255) are available to users.
40
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
INTs
• 256 different software interrupt instructions (INTs) available to
the programmer.
– each INT instruction has a numeric operand whose range is
0 to 255 (00H–FFH)
• Address of the interrupt vector is determined by multiplying the
interrupt type number by 4.
– For example, INT 10H instruction calls the interrupt service
procedure whose address is stored beginning at memory
location 40H (10H 4) in the mode.
41
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
INTs…
Consequences of software interrupts
44
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
INT 3
• A special software interrupt designed to function as a
breakpoint.
– a 1-byte instruction, while others are 2-byte
• Common to insert an INT 3 in software to interrupt or break the
flow of the software.
– function is called a breakpoint
– breakpoints help to debug faulty software
• A breakpoint occurs for any software interrupt, but because INT
3 is 1 byte long, it is easier to use for this function.
45
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
INTO
• Interrupt on overflow (INTO) is a conditional software interrupt
that tests overflow flag (O).
– If O = 0, INTO performs no operation
– if O = 1 and an INTO executes, an interrupt occurs via vector
type number 4
• The INTO instruction appears in software that adds or subtracts
signed binary numbers.
– With these operations, it is possible to have an overflow
• JO or INTO instructions detect the overflow.
46
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
An Interrupt Service Procedure
• Interrupts are usually reserved for system events. This is simply
an example showing how an interrupt service procedure
appears.
• Suppose a procedure is required to add the contents of DI, SI,
BP, and BX and save the sum in AX.
– as a common task, it may be worthwhile to develop the task
as a software interrupt.
47
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
MACHINE CONTROL AND MISCELLANEOUS INSTRUCTIONS
48
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
WAIT
49
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
HLT
• The halt instruction (HLT) stops the execution of software.
• There are three ways to exit a halt:
– by interrupt
– a hardware reset
– or DMA (Direct memory access) operation
• Often synchronizes external hardware interrupts with the
software system.
50
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
NOP
• When the microprocessor encounters a no operation instruction
(NOP), it takes a short time to execute.
• In early years, before software development tools were available,
a NOP, which performs absolutely no operation, was often used to
pad software with space for future machine language instructions.
• When the microprocessor encounters a NOP, it takes a short time
to execute.
• If you are developing machine language programs, which are
extremely rare, it is recommended that you place 10 or so NOPS
in your program at 50-byte intervals., in case you need to add
instructions at some future point.
• A NOP may also find application in time delays to waste time.
• A NOP used for timing is not very accurate because of the cache
and pipelines in modern microprocessors.
51
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
52
Prepared by Mehdi Hasan Chowdhury, EEE, CUET