Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Branch group of instructions

Branch instructions provide lot of convenience to the programmer to perform operations


selectively, repetitively etc.

Branch group of instructions

Conditional Uncondi-tional Iteration CALL Return


jumps jump instructions instructions instructions

Conditional Jump instructions

Conditional Jump instructions in 8086 are just 2 bytes long. 1-byte opcode followed by
1-byte signed displacement (range of –128 to +127).

Conditional Jump Instructions

Jumps based on a single flag Jumps based on more than one flag

Jumps Based on a single flag

JZ r8 ;Jump if zero flag set (if result is 0). JE also means same.
JNZ r8 ;Jump if Not Zero. JNE also means same.
JS r8 ;Jump if Sign flag set to 1 (if result is negative)
JNS r8 ;Jump if Not Sign (if result is positive)
JC r8 ;Jump if Carry flag set to 1. JB and JNAE also mean same.
JNC r8 ;Jump if No Carry. JAE and JNB also mean same.
JP r8 ;Jump if Parity flag set to 1. JPE (Jump if Parity Even) also means same.
JNP r8 ;Jump if No Parity. JPO (Jump if Parity Odd) also means same.
JO r8 ;Jump if Overflow flag set to 1 (if result is wrong)
JNO r8 ;Jump if No Overflow (if result is correct)

JE is abbreviation for Jump if Equal. JNE is abbreviation for Jump if Not Equal.
JB is abbreviation for Jump if Below. JNAE is for Jump if Not Above or Equal.
JAE for Jump if Above or Equal. JNB for Jump if Not Above.

JZ, JNZ, JC and JNC used after arithmetic operation

JE, JNE, JB, JNAE, JAE and JNB are used after a compare operation.
Examples for JE or JZ instruction

Ex. for forward jump


Only examples using JE instruction given for forward and backward jumps.

CMP SI, DI
JE SAME
ADD CX, DX ;Executed if Z = 0
Should be<=127 bytes : (if SI not equal to DI)
:
SAME: SUB BX, AX ;Executed if Z = 1
(if SI = DI)

Ex. for backward jump

BACK: SUB BX,AX ;Executed if Z = 1 (if SI=DI)


:
Should be :
<=127 bytes CMP SI, DI
JE BACK
ADD CX,DX ;Executed if Z = 0 (if SI <> DI)

Jumping beyond -128 to +127?

Requirement Then do this!

CMP SI, DI CMP SI, DI


JE SAME JNE NEXT
What if ADD CX, DX JMP SAME
>127 bytes : NEXT: ADD CX, DX
: :
SAME: SUB BX, AX :
SAME: SUB BX, AX

15
Range for JMP (unconditional jump) can be +2 = + 32K. JMP instruction discussed in
detail later
Terms used in comparison

Above and Below used for comparing Unsigned numbers. Greater than and less than used
when comparing signed numbers. All Intel microprocessors use this convention.
Accordingly, all the following statements are true.
95H is above 65H Unsigned comparison - True
95H is less than 65H Signed comparison – True (as 95H is negative, 65H is positive)
65H is below 95H Unsigned comparison - True
65H is greater than 95H Signed comparison - True

Jump based on multiple flags


Conditional Jumps based on multiple flags are used after a CMP (compare) instruction.

JBE / JNA instruction

‘Jump if Below or Equal’ or ‘Jump if Not Above’

Jump if No Jump if Ex.


Cy = 1 OR Z= 1 Cy = 0 AND Z = 0 CMP BX, CX
Below OR Equal Surely Above JBE BX_BE

BX_BE (BX is Below or Equal) is a symbolic location

JNBE / JA instruction

‘Jump if Not (Below or Equal)’ or ‘Jump if Above’

Jump if No Jump if Ex.


Cy = 0 AND Z= 0 Cy = 1 OR Z = 1 CMP BX, CX
Surely Above Below OR Equal JBE BX_BE

JLE / JNG instruction

‘Jump if Less than OR Equal’ or ‘Jump if Not Greater than’


Jump if No Jump if

[(S=1 AND V=0) OR (S=0 AND V=0)] [(S=0 AND V=0) OR (S=1 AND V=1)]
OR Z=1 AND Z=0
[(surely negative) or (wrong answer [(surely positive) or (wrong answer
positive!)] or Equal negative!)] and not equal
i.e. [S XOR V=1] OR Z=1 i.e.[S XOR V=0] AND Z=0
JNLE / JG instruction

‘Jump if Not (Less than OR Equal)’ or ‘Jump if Greater than’


Jump if No Jump if

[(S=0 AND V=0) OR (S=1 AND V=1)] [(S=1 AND V=0) OR (S=0 AND V=1)]
AND Z=0 OR Z=1
[(surely positive) or (wrong answer [(surely negative) or (wrong answer
negative!)] and not equal positive!)] or equal
i.e. S XOR V=0 AND Z=0 i.e.S XOR V=1 OR Z=1

JL / JNGE instruction

‘Jump if Less than’ or ‘Jump if NOT (Greater than or Equal)’

Jump if No Jump if
[S=1 AND V=0] OR [S=0 AND V=1] [S=0 AND V=0] OR [S=1 AND V=1]
(surely negative)or (wrong answer (surely positive) or (wrong answer
positive!) negative!)

i.e. S XOR V=1 i.e.S XOR V=0


Note: When S=1, result cannot be 0

JNL / JGE instruction

‘Jump if Not Less than’ or ‘Jump if Greater than OR Equal’

Jump if No Jump if
[S=0 AND V=0] OR (S=1 AND V=1) [S=1 AND V=0] OR (S=1 AND V=1)
(surely positive) or (wrong answer (surely negative) or (wrong answer
negative!) positive!)

i.e. S XOR V=0 i.e.S XOR V=1


Note: When S=0, result can be >= 0

Unconditional Jump instruction

Unconditional Jump Instruction

Near Jump or Intra segment Jump Far Jump or Inter segment Jump
(Jump within the segment) (Jump to a different segment)
Near Unconditional Jump instruction

Near Jump

Direct Jump (common) Indirect Jump (uncommon)

2-bytes Short Jump (EB r8) 3-bytes Long Jump (E9 r16) 2 or more bytes
7 15
Range: + 2 Range: +2 Starting with FFH
Range: complete segment

Three Near Jump and two Far Jump instructions have the same mnemonic JMP, but they
have different opcodes

Short Jump Instruction

2 byte (EB r8) instruction with Range: -128 to +127 bytes

For Backward jump: Assembler knows the quantum of jump. Generates Short Jump code
if <=128 bytes is the required jump. Generates code for Long Jump if >128 bytes is the
required jump.

For Forward jump: Assembler doesn’t know jump quantum in pass 1. Assembler
reserves 3 bytes for the forward jump instruction. If jump distance turns out to be >128
bytes, the instruction is coded as E9 r16 (E9H = Long jump code). If jump distance
becomes <=128 bytes, the instruction is coded as EB r8 followed by code for NOP (E8H
= Short jump code).

SHORT Assembler Directive

Assembler generates only 2 byte Short Jump code for forward jump, if the SHORT
assembler directive is used.

JMP SHORT SAME


:
Programmer should ensure that :
the Jump distance is <=127 bytes SAME: MOV CX, DX

Long Jump instruction

3-byte (E9 r16) instruction with Range: -32768 to +32767 bytes

Long Jump can cover entire 64K bytes of Code segment


CS:0000H :
:
CS:8000H JMP FRWD
:
Long Jump can handle it as jump :
quantum is <=32767 FRWD = CS:FFFFH :

Long Jump can handle it as jump BKWD= CS:0000H :


quantum is <=32767 :
CS:8000H JMP BKWD
:
FRWD = CS:FFFFH :

Long Jump or Short Jump?

Can be treated as a CS:0000H :


small (20H) Backward : Jump distance =FFE0H.
Branch! CS:0010H JMP FRWD Too very long forward
: jump.

FRWD = CS:FFF0H :
:
CS:FFFFH :

Can be treated as a CS:0000H :


small (20H) : Jump distance =FFE0H.
Forward Branch! BKWD = CS:0010H : Too very long
backward jump
:
CS:FFF0H JMP BKWD
:
CS:FFFFH :

Intra segment indirect Jump

It is also called Near Indirect Jump. It is not commonly used.


Instruction length: 2 or more bytes Range: complete segment

Ex.1: JMP DX
If DX = 1234H, branches to CS:1234H. 1234H is not signed relative displacement.
Ex. 2: JMP wordptr 2000H[BX]

If BX contents is 1234H DS:3234H 5678H


Branches to CS:5678H DS:3236H AB22H

Far Jump instruction

Far Jump

Direct Jump (common) Indirect Jump (uncommon)

5 bytes, opcode EA, 2 byte offset, 2 or more bytes,


2 byte segment value starting with opcode FFH

Range: anywhere in memory Range: anywhere in memory


As stated earlier, three Near Jump and two Far Jump instructions have the same
mnemonic JMP but different opcodes.
Inter segment Direct Jump instruction

Also called Far Direct Jump. It is the common inter segment jump scheme
It is a 5 byte instruction. 1 byte opcode (EAH), 2 byte offset value, 2 byte segment value

Ex. JMP Far ptr LOC

Inter segment Indirect Jump instruction

Also called Far Indirect Jump. It is not commonly used. Instruction length depends on the
way jump location is specified. It can be a minimum of 2 bytes.

Ex. JMP DWORD PTR 2000H[BX]


If BX contents is 1234H branch takes place to location ABCDH:5678H. It is a 4-byte
instruction.

DS:3234H 5678H
DS:3236H ABCDH

Iteration Instructions

Iteration instructions provide a convenient way to implement loops in a program

Iteration instructions

LOOP LOOPZ or LOOPE LOOPNZ or LOOPNE JCXZ


LOOP Instruction

Let us say, we want to repeat a set of instructions 5 times.

For 8085 processor For 8086processor

MVI C, 05H MOV CX, 0005H


AGAIN: MOV B, D AGAIN: MOV BX, DX
: :
DCR C LOOP AGAIN
JNZ AGAIN

General format: LOOP r8; r8 is 8-bit signed value. It is a 2 byte instruction.


Used for backward jump only. Maximum distance for backward jump is only 128 bytes.
LOOP AGAIN is almost same as: DEC CX
JNZ AGAIN
LOOP instruction does not affect any flags.
If CX value before entering the iterative loop is:
0005, then the loop is executed 5 times till CX becomes 0
0001, then the loop is executed 1 time till CX becomes 0
0000, then the loop is executed FFFF+1 = 10000H times!

JCXZ Instruction

Jump if CX is Zero is useful for terminating the loop immediately if CX value is 0000H
It is a 2 byte instruction. It is used for forward jump only. Maximum distance for forward
jump is only 127 bytes.

Ex. MOV CX, SI


JCXZ SKIP
AGAIN: MOV BX, DX
:
:
LOOP AGAIN
SKIP: ADD SI, DI ; Executed after JCXZ if CX = 0

LOOPZ instruction

LOOP while Zero is a 2-byte instruction. It is used for backward jump only. Backward
jump takes place if after decrement of CX it is still not zero AND Z flag = 1. LOOPE is
same as LOOPZ. LOOPE is abbreviation for LOOP while Equal. LOOPE is normally
used after a compare instruction.

Ex. MOV CX, 04H


BACK: SUB BX, AX
MOV BX, DX
:
:
ADD SI, DI
LOOPZ BACK ; if SI+DI = 0 and CX not equal to 0, branch to BACK

CALL Instructions

CALL instruction is used to branch to a subroutine. There are no conditional Call


instructions in 8086.

CALL instructions

Near CALL or Intra segment CALL Far CALL or Inter segment CALL

Near Direct CALL Near Indirect CALL Far Direct CALL Far Indirect CALL

Near Direct CALL instruction

It is a 3-byte instruction. It has the format CALL r16 and has the range + 32K bytes.
Covers the entire Code segment. It is the most common CALL instruction.

It is functionally same as the combination of the instructions PUSH IP and ADD IP, r16.

Ex. CALL Compute

Near Indirect CALL instruction

Not commonly used. Instruction length depends on the way the called location is
specified.

Ex.1: CALL AX ; If (AX) = 1234H, branches to procedure at CS: 1234H.


1234H is not relative displacement.

Ex. 2: CALL word ptr 2000H[BX]


If BX contents is1234H Branches to subroutine at CS:5678H

DS:3234H 5678H
DS:3236H ABCDH

Far Direct CALL instruction

It is a 5-byte instruction. 1-byte opcode, 2-byte offset, 2-byte segment value.


Far direct CALL is functionally same as:
PUSH CS
PUSH IP
IP = 2-byte offset value provided in CALL
CS = 2-byte segment value provided in CALL

Ex. CALL far ptr Compute

Far Indirect CALL instruction

Not commonly used. Instruction length depends on the way the called location is
specified.

Ex. CALL dword ptr 2000H[BX]

If BX contents is1234H bBranches to subroutine at ABCDH:5678H

DS:3234H 5678H
DS:3236H ABCDH

Conditional CALL?

What if we want to branch to subroutine COMPUTE only if Cy flag = 0?

Solution:
JC NEXT
CALL COMPUTE ; execute only if Cy = 0
NEXT:

RETURN instructions

RET is abbreviation for Return from subroutine

RET instructions

Near RET or Intra segment RET Far RET or Inter segment RET

RET RET d16 RET RET d16

Near RET instruction

It is 1-byte instruction. Opcode is C3H. It is functionally same as : POP IP


Ex:
Compute Proc Near ; indicates it is a NEAR procedure
:
:
RET
Compute ENDP ; end of procedure Compute
In fact, default procedure type is NEAR

Near RET d16 instruction

It is a 3-byte instruction. 1-byte opcode (C2H) and 2-byte data. It is functionally same as:
POP IP
SP = SP + d16

Ex. RET 0004H

RET d16 is useful for flushing out the parameters that were passed to the subroutine
using the stack

Use of RET d16 instruction

Main Program
:
: SP after CALL Compute IP
PUSH Var1 Var2
PUSH Var2 Var1
CALL Compute SP before PUSH Var1
:
:

Subroutine
COMPUTE PROC Near IP
: SP if RET is executed Var2
: Var1
RET 0004H SP if RET 0004H is executed
COMPUTE ENDP

Far RET instruction

It is 1-byte instruction. Opcode is CBH. It is functionally same as: POP IP + POP CS

Ex. SINX Proc Far ; indicates it is a FAR procedure


:
:
RET
SINX ENDP ; end of procedure SINX
Default procedure type is NEAR

Far RET d16 instruction

It is a 3-byte instruction. 1-byte opcode (CAH) and 2-byte data.


It is functionally same as: POP IP + POP CS + ADD SP, d16

Ex. RET 0006H

RET d16 is useful for flushing out the parameters that were passed to the subroutine
using the stack.

You might also like