Branch Operations

Instruction sets that cause branch operations ( not in the sequential order ) are known as branch control groups of instructions. To acess complete 8051 series, click here.
Eg: loops
In the sequential manner of program execution, every next line of the program is executed due to increment of PC ( program counter ). So branch control instruction sets cause a change in the PC.

There are two types of branch operations.

  • Jump
  • Call

Jump instructions:

Refer previous tutorial for jump instructions.

Call instructions:

branch operations in 8051
Call instruction in 8051

This instruction is similar to calling functions in any other programming languages. Here sub is the first location of call function. Following are the steps how this type of branch operations work.

Steps:

  1. In the above diagram, the program is sequentially fetched upto location 99H.
  2. At 100th location, just when the program is fetched PC gets 101, and then 100th location program is executed, which means PC should get a subroutine address. As the PC should get subroutine location, 101 is pushed in the stack.
  3. Then PC←subroutine .
  4. Now the call function is executed completely in a sequential manner.
  5. Once completed the subroutine, the processor gets the return 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( 101 ) is given to the PC.
  7. So PC←101, and continues.

In the call function, 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 PC.

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 operations in 8051
Call stack working

There are two types of calls:

  • ACALL sub
  • LCALL sub

There are two types of returns

  • RET
  • RETI

Conditional jumps:

CJNE
syntax : x,y,label

In the last tutorial, unconditional jumps were explained. These are conditional jumps and very important part of branch operations in 8051. CJNE stands for compare and jump if not equal. 

If x is not equal to y, 
Then
It will jump to the label.( in practical we give label, not address )

DJNZ
syntax : count, label

DJNZ stands for decrement and jump if not zero.

Until counts gets zero

Jumping continues.

Eg : DJNZ 5, label.

5 becomes 4 and jumps to label

4 becomes 3 and jumps to label

1 becomes 0 and jumps to label.

Now count is equal to zero, no jumps further. Few other jump instrcutions which are part of branch operations are listed below.

JC label:

JC stands for jump if carry. If carry flag =1 , then it jumps to the specified label. 

JNC label:

JNC stands for jump if not carry. If carry flag =0 , then it jumps to the specified label.

JZ label:

JZ stands for jump if zero. If A=0 , then it jumps to the specified label. 

JNZ label:

JNZ stands for jump if not zero. If carry A =1 , then it jumps to the specified label.

Bitwise conditional jumps:

The below listed branch operations or jump instructions are based on some conditions, similar to if statements in programming languages.

JB bit label:

Jump if bit. 

Eg: JB P0.1 , label

If P0.1 has 1 in it, then jump to the specified label.

JB bit label:

Jump if not a bit. 

Eg: JB P0.1 , label

If P0.1 has 0 in it, then jump to the specified label.

JBC bit label:

Jump if bit and clear or compliment. 

Eg: JB P0.1 , label

If P0.1 has 1 in it, then jump to the specified label and resets P0.1( P0.1 = 0 ) .

If P0.1 has 0 in it, then no action.

This was all about branch operations in 8051.

Check out the keil website , software which helps simulating a 8051 microcontroller.

Spread knowledge

Leave a Comment

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