Skip to main content

8051 microcontroller - JUMPs and LOOPs

In this 8051 microcontroller tutorial we discuss JUMP and CALL instructions. In previous tutorials we learn about MOV and ADD instruction and how to use them with registers as well as memory locations. but some time you need to take decision between two state or you need to jump some of code, or may be you need to call a subroutine(function). In that case you need some special instruction which make our task very simple and
also save some ROM of 8051. In this assembly tutorial we cover some very useful instruction of 8051 instruction set and learn how to write code using these instruction. we create loop and delay using some Jump instruction. so lets start jump and loop: As you know in Assembly programming language we don't have if.. else and for, while, do..while loops. Instead of this In assembly we only have JUMP instructions and by using them we can create any loop or can make decision. in Assembly we have two type of JUMP instruction.
  • Conditional Jump Instruction
  • Unconditional Jump Instruction

Conditional Jump:

the decision is taken when certain condition meet. the condition depends upon the used instruction, Let you want to jump when accumulator reg. (A) is zero or when a carry generated or any thing else.
       these instruction are used where you need a loop or  you need to take decision between between two state. some mostly useful instructions are.

1.DJNZ

- (Decrease and jump if not equal to zero):  This is  a widely used conditional jump instruction. 

working:

 It first decrease the value of register by 1 and then jump if the content of register is not equal to zero else it execute just next instruction. it is a decrementing counter which jump untill reached to zero.
+++++++++++++++++++++DJNZ syntax++++++++++++++++++
DJNZ byte, target
byte: it may be a register or memory location
target: label(address) where you want to jump
++++++++++++++++++++++++++++++++++++++++++++++++++
 Example 1:
+++++++++++++++++++++DJNZ++++++++++++++++++
;multiply 30 with 5 using DJNZ
         ORG 00H
         MOV R0,#5
         MOV A, #00H
AGAIN:   ADD A, #30
         DJNZ R0, AGAIN ; decrease by 1 and jump if r0 is not zeor
         END
 ;result is 96H in reg A which is equal to 150 dec
++++++++++++++++++++++++++++++++++++++++++++++++++
in above program R0 hold 5 and DJNZ instruction decrease it by 1 compare it with 0. when it reach to zero it execute just next instruction which is END here.
Example 2:
+++++++++++++++++++++DJNZ ++++++++++++++++++
;load R0 from memory loc. 50h and decrease R2, 5 times 
        ORG 00H
        MOV R2,50h      ;load from 50h memory loc.
        MOV R1,#5H      ; load counter
sub:    SUBB R2,#1H     ; decrease by 1
        DJNZ R0, sub    ;jump again (5 times)
        END
++++++++++++++++++++++++++++++++++++++++++++++++++

2.JB and JNB

These are also very useful instruction for 8051 because they check the condition for a bit.JB instruction is use to get a jump when bit is set and JNB is to jump when  bit is not set.  We use these instruction widely in later tutorials.
Syntax-
JB: jump if bit is set
--------------------------
JB bit,target

JNB: jump if bit not set
--------------------------
JNB bit,target 

Example 3:
+++++++++++++++++++++JB and JNB ++++++++++++++++++
;check whether value at 23h memory is odd or even
;Hint-{if 7th bit of a 8bit number is set then it is a odd value}

        MOV A,23h  ;load acc form 23h loc.
        JB  ACC.7, odd_work ;jump when number is odd
        ----
        ----      ;do some thing when even value
        -----
odd_work: 
-----------
-----------       ;do some thing when odd value
++++++++++++++++++++++++++++++++++++++++++++++++++



all conditional jump instruction for 8051 microcontroller

conditional jump instruction for 8051 for byte

conditional jump instruction for 8051 for bit
JZ and JC work very similar to JB, the only difference is that JZ check for ACC reg. and JC is check for carry.
I decide to cover CALL and Delay in next tutorial.
 Let you have two switch connected at P1.1 and P1.2, write a code to do
p1.1(on) & p1.2(on)=> add 10 to accumulator
p1.1(on) & p1.2(off)=> add 20 to accumulator
p1.1(off) & p1.2(on)=> add 30 to accumulator
p1.1(off) & p1.2(off)=> add 40 to accumulator

Comments

Popular posts from this blog

Multiplex 4 Seven Segment Display With 8051 In Assembly

Multiplexing is very essential part while working with 8051 because of its limited number of ports. which offer 32 pins for connecting external devices. 32 seems large but when you working with seven segment, keypad, LCD, ADC etc. or if you project have many parts to operate simultaneously then you definitely need to multiplex some of ports.

LCD Interfacing with 8051 in 4 bit mode : assembly tutorial

Hello friends, in this tutorial we are going to interface LCD with 8051 microcontroller but in 4 bit mode. we already interfaced LCD with 8051 in 8 bit and also done scrolling of text in previous tutorials.

how to get 7805 in multisim - Replacement of 7805 in multisim

  finding some parts in multisim is really tough work because multisim  not provide all general purpose component like 7085 and other voltage regulator. one more  bad information is that manufacturer of these component also not provide any PSPICE because output of these component are predetermined and not changed according to the circuit.