flow chart if elseflow chart

If you are familiar with some higher level language like C/C++, C#, Java or any other language you may already be well aware of if else statements. But if you are new to programming then lets just first understand what these statements are all about? and later know how to make it, and if else statements in assembly language using 8051 architecture.

What is If statement?

In programming, the “if” statement is referred to conditional statements. Mean such statements where some conditions are required to be met before executing them.

To create any programming logic, no matter which language you programming in, the Conditional statements are very important. They are used as basic building blocks to any kind of programs.  If statement normally denotes to be executed when some condition is true. for example,

If (today ==  Hot) take(bath);

In above statement the if is a keyword determining that we are intended to do some task if the condition mentioned in parentheses is true, and in this example, the task is to take a bath which will only be executing if today is equal to hot. taday==Hot is an expression in this case which will result in only bool value mean it will either it’s true or its false. If this expression will return “true” then the bath will be taken but if this will return “false” the bath will not be taken.

Types of If statements:

There are normally following types of if statements




nested if statements

If Else Statement:

if else statement is the proper full form of this conditional statement. The “if” form execute the instruction next if the expression is given to if statement returns true but the that is not true “then” if we want to do something that is called “ELSE” condition. so the full form normally became like this

flow chart if else
flow chart

if(today==Hot)take(bath); else goto sleep;

Above statement will execute the “else” statement only if the “if” expression returns false.

Implementation in Assembly

So let us just implement this in Assembly language and try to figure out how we can write these statements using Assembly language with 8051 instruction set.  First of all, let us figure out which instruction we are going to use?

Which Instruction is used for conditional statement?

The very well known instruction in 8051 architecture’s instruction set for conditional jumps is CJNE which’s function is to Compare two values and jump to a relative address provided to this instructions.

About CJNE instruction

CJNE or Compare and Jump if Not Equal instruction takes 3 bytes of code memory and needs 2 clock cycles of the microcontroller to be executed. We can use this instruction to compare two bytes but we cannot directly compare two values we need to put one value into either one of R0-R7  or into Accumulator or “ACC” Register normally known as “A” register.

The format of this instruction is something like this

     CJNE    A,#23,MAKE_EQUAL
     SJMP    BACK
     MOV     A,#23
     SJMP    BACK

Above instruction will compare “A” Register with number of Decimal 23 and if these both numbers are not equal then the program counter will jump to label named MAKE_EQUAL and by jumping to this label we load register “A” with value #23D by doing this next time when we jump to BACK then the compare instruction will be executed again and this time “A” is equal to #23 so no jump will occur to MAKE_EQUAL again and the line SJMP back will be executed and microcontroller will keep looping this again and again.




By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.