INSTRUCTION SET Part2

on Saturday, 14 September 2013
In our previous post we have seen General instructions and Arithmetic instructions along with examples. In this post we will look at the Loop, Jump and Call instructions.

JUMP, LOOP AND CALL INSTRUCTIONS:


Loop Instructions:
Loop is nothing but repeating sequence of instructions for certain number of times.
We use DJNZ instruction for this purpose.
DJNZ reg, label

For every time the loop reaches this instruction it decrements the 'reg' i.e register value and jump backs to the label.

Example1: Perform this operation using loop 10+10+10+10+10

            Mov R0,#5                  ;number of time we need to repeat is saved in R0
            Mov A,#0                    ; clear A
Back:   Add A,#10
            DJNZ R0,Back           ; here R0 will be decremented for every time the control
                                                 reach this line and jumps to label "Back"

Fore every time the control reaches this loop instruction it will decrements the register value and loop continues until the register value becomes zero.

Example2: Write a program to add first ten natural numbers
We need to add 1,2,3....10

           Mov A,#0                         ; Clearing A
           Mov R3,#10                     ; Number of times loop should repeat
           Mov R0,#0                       ; Clearing R0 , even we can write it as Mov R0,A
 Bck:    INC R0                             ; Increment R0
           ADD A,R0                         ; Add R0 value with A
           DJNZ R3, Bck                   ; Jump back to 'Bck' until r3=0
           Mov 50H,A                      ; save result in 50h location

First iteration R0=1(incrimented), A=0, after ADD A=1
2nd  iteration R0=2(incrimented),A=1(previous last value) , after add   A= 3
3rd iteration  R0=3(incrimented) A=3(previous last value), after add A=6
.
.
.
Until last i.e 10 iteration it continues.

Important Note: Generally all the 8051 are 8 bit registers i.e they can store atmost 255 value.i.e we can only make a loop upto 255 times, but if we want to make more than that then we need to use loop inside the loop.

Example3: Increment the accumulator register(A) for 500 times using loop

As 500 is greater than 255 we use two registers to make a loop. One register holds the value 50 and other register holds value 10, for every 50 loops the 2nd register should decremented once i.e 50x10=500 times.

                          Mov R1,#10
                          Mov A,#00
            again1:  Mov R0,#50
            again2:  INC A
                          DJNZ R0, again2      
                          DJNZ R1,again1      
                                                           
Until R0 is zero the loop goes back to 'again2'. when R0 becomes 0 then R1 will be decremented by one and jump back to again1 and makes R0=50 continues untill R1 becomes 0.
After R1 becomes 0 the loop will be ended it happens after 50x10=500 times

JUMP INSTRUCTIONS:

We have two types of jump instructions, Conditional jump inst and Unconditional jump inst.

Conditional Jump Instructions: All conditional jumps can jump upto 2 bytes long
The control will be shifted depending upon the condition.

JZ: JZ means  'Jump if A=0'
          Mov A,R1
          JZ label       ; if A=0 then control will be shifted to Label if not it continues to next step
          .
          .
          .
Label: Few other instructions
          .
          .

JNZ: If A is not equal to Zero then control will be shifted

          Mov A,R1
          JZ labelx     ; if A=0 then control will be shifted to 'Labelx' if not it continues to next step
          .
          .
          .
Labelx: some instructions
          .
          .

JC: Jump if Carry bit is equal to 1 i.e psw.7=1 or C=1
     
         CLR C
         Mov A,#25H
         Add A,#72H
         JC labelx       ; after addition if it generates carry then it will be jump
         .
         .
labelx: some instruction depending upon our requirements

In the same way JNC

JB: If any bit is 1 then jump if not dont jump
JB bit,label
 JB 01,label    ; if the bit of 01 address is 1 then jump or else dont jump

JNB is similar to that of JB i.e If the bit is zero then it will jump


CJNE: Compare jump if not equal.
CJNE reg, data/address data, label

If reg and data are not equal then jumps to some predefined label or else operation will continues to next step.

Example: If A=25h then save 1 in R0 or else save 2 in R0

                    CJNE A,#25h, somelabel
                    Mov R0,#1                      ;if A is equal to 25 then R0=1
                    SJMP xyz                        ; jump to some xyz location
somelabel:   Mov R0,#2                     ; if A is not equal then R0=2
xyz:              .                                     ; any other instructions continue
                    .
                    .

Unconditional Jump Instructions: They are said to be unconditional because the jump will not be depend on any condition i.e if something is right then go to so and so line, its not happens here.

LJMP (Long Jump) : This jump can jump upto 3 bytes long.

xyz: Mov R1,#10
       .
       .
      LJMP xyz  ; as soon as the control reaches here it go backs to its label(xyz)

SJMP(Short Jump) : It is similar to LJMP but it is only 2 bytes long.

xyz: Mov R1,#10
       .
       .
      SJMP xyz  ; as soon as the control reaches here it go backs to its label(xyz)





CALL INSTRUCTIONS: 

We have seen jump instructions above, now let us see the Call instructions. We have 2 call instructions in 8051 i.e LCALL(i.e long call) ,ACALL(absolute call). Both are the same except Lcall is of 3bytes and Acall is of 2bytes.
The difference between Jump and Call instructions are, when we use jump instruction we shift the control to predefined label and we never come back again. But when we use Call instruction after jumping to predefined label control will come back again.

Example:
                      ORG 00                          ;org is the origin of the program in the memory i.e at 00
                      Mov A,#55h
                      Mov B,#22h
                      Acall xyz                         ; jumps to xyz
                      .                                      ;perform certain task as required
                      .
               xyz: Mov A,#00h
                      Mov B,#25h
                      RET                                ;after jumps to xyz, from here it again returns back
                      END                               ;END of program

Note here ORG, RET, END are the directives which are used to show the direction for the assembler.They never concern with controllers.
For every program there will be one origin ORG and one end END.


In our next post we will look at the Logical instructions.

AUTHOR: YOUSTRON SIC













2 comments:

Kapil Sonawane said...

In 6th instructuion (in 6th row)of Example 2. threre must DJNZ R3 instead of DJNZ R2

YOUSTRON SIC said...

Kapil thanq for correcting me right.. :-)

Post a Comment