Branch instructions in 8086

Normally the processor executes the program in a sequential manner, due to continuous increment of IP ( instruction pointer ). Branch instructions in this 8086 microprocessor are that jumps to another part of the program without executing in a sequential manner.

Eg: for loop in other programming languages.

goto instruction in c language.

Mainly there are two types of branch instructions in 8086 which are further classified into two types each.

  • Jump instruction.
  • Call instruction.

Jump instruction:

This lets the processor jump to a specified location to execute from. From the new location again code will be executed sequentially. The address changes suddenly when jump operation is performed.

There are two types of jump operation.

  • Intrasegment branching
  • Intersegment branching

Intrasegment branching:

This is when jump operation is performed within the same segment( refer segmentation ). This is also known as the near branch. In intrasegment branching as the jump is in the same segment, so only IP ( offset address ) would change.

Intersegment branching:

This is when jump operation is performed between segments. This is also known as far jump. As this jump operation is across the segments , both CS and IP ( segment address and offset address ) will change.

Branch instructions in 8086
jump instruction in 8086

As shown in the picture, whenever the JMP statement comes, it jumps to the label given ( here label is next ), In general the programmer doesn’t know the address of each location , so the label is mentioned in the assembly code.

This was the example of unconditional jump, where it jumps unconditionally. 

Conditional jumps:

JC:

This stands for Jump if Carry. 

Eg: JC next;

If the carry flag = 1, then it will jump to the “next” label.

JNC:

This stands for Jump if No Carry. 

Eg: JNC next;

If the carry flag = 0, then it will jump to the “next” label.

JZ:

This stands for Jump if Zero. 

Eg: JZ next;

If the zero flag = 1, then it will jump to the “next” label.

JNZ:

This stands for Jump if No Zeroy. 

Eg: JNZ next;

If the zero flag = 0, then it will jump to the “next” label.

JPE:

This stands for Jump if Parity is Even. 

Eg: JPE next;

If the parity flag = 1, then it will jump to the “next” label.

JPO:

This stands for Jump if Parity is Odd. 

Eg: JPO next;

If the parity flag = 0, then it will jump to the “next” label.

JO:

This stands for Jump if Overflow. 

Eg: JO next;

If the overflow flag = 1, then it will jump to the “next” label.

JNO:

This stands for Jump if No Overflow. 

Eg: JNO next;

If the overflow flag = 0, then it will jump to the “next” label.

JS:

This stands for Jump if Sign. 

Eg: JS next;

If the sign flag = 1, then it will jump to the “next” label.

JNS:

This stands for Jump if No Sign. 

Eg: JNS next;

If the sign flag = 0, then it will jump to the “next” label.

Call branching:

call branching visual diagram

Consider PC ( program counter in 8051 )  as IP( instruction pointer ) in the above figure. 

This instruction is similar to calling functions in any other programming languages. Here sub is the first location of the call function. In call operation it is subroutine , which is known as a function in other programming languages. 

Steps:

  1. In the above diagram, the program is sequentially fetched upto location 99H.
  2. At 100th location, just when the program is fetched IP gets 101, and then 100th location program is executed, which means IP should get a subroutine address. As the IP should get subroutine location, 101 is pushed in the stack.
  3. Then IP ←subroutine top address .
  4. Now the call function is executed completely in a sequential manner.
  5. Once completed, the processor gets the RET command, but not the address of the next location, where it left the main program.
  6. So now the stack is popped, and that value is given to the IP.
  7. So IP←101, and continues.

Inside the call function( subroutine ), internally the number of pushes in the stack should be the number of pops, else the return address would not be given correctly to the IP.

Why is stack used to store the return address?

When there are nested call functions, all the return addresses of successive programs would be pushed into the stack, and while returning respective pops are done.

Branch instructions in 8086

Iterations:

Infinite iteration:

Finite loop:

Here we will make 6 iterations. We have to use conditional jumps. Mostly we use JNZ, JZ, JC, JNC. ( refer flag register ).

  • Store the number ( number of iterations ) in any general purpose register.
  • Write the code
  • Decrement the register
  • Use a jump statement.

Make sure decrementing is done at the end of code, Otherwise if any other arithmetic operation below DEC statement would affect the zero flag. Zero flag is affected by the most recent arithmetic operation. So the most recent arithmetic operation should be decrementing the number. 

In this type of finite iterations we use two instructions ( decrementing and jumping ).

Another way of finite loop:

Instead of writing two instructions , we write only one instruction

LOOP previous

This instruction actually contains two instructions embedded in it

  • DEC CX
  • If CX !=0 , goto previous

But in this method we can only use the CX register, in the previous method we could choose any register. But this method is faster as compared to the previous one. 

Read few other important topics:
Architecture of 8086
Architecture of 8051

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *