Saturday 26 January 2013

JUMP, LOOP AND CALL INSTRUCTIONS

loop:

Repeating a sequence of instructions a certain number of times is called a loop
  • Loop action is performed by
DJNZ reg, Label
  • The register is decremented
  • If it is not zero, it jumps to the target address referred to by the label
  • Prior to the start of loop the register is loaded with the counter for the number of repetitions
  • Counter can be R0 – R7 or RAM location
Example: 
;This program adds value 3 to the ACC ten times
MOV A,#0                           ;A=0, clear ACC
MOV R2,#10                       ;load counter R2=10
AGAIN:
ADD A,#03                          ;add 03 to ACC
DJNZ R2,AGAIN                 ;repeat until R2=0,10 times
MOV R5,A                           ;save A in R5


You can increase the repetition time by changing value in R2, A loop can be repeated maximum 255 times.

nested loop


If we want to repeat an action more times than 256, we use a loop inside a loop, which is called nested loop

  • We use multiple registers to hold the count
Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC 700 times

Example:
MOV A,#55H                      ;A=55H
MOV R3,#10                       ;R3=10, outer loop count
NEXT:
MOV R2,#70                       ;R2=70, inner loop count
AGAIN:
CPL A                                  ;complement A register
DJNZ R2,AGAIN                ;repeat it 70 times
DJNZ R3,NEXT


Jump if Zero

Jump only if a certain condition is met
JZ label          ;jump if A=0

Example:
MOV A,R0           ;A=R0
JZ OVER               ;jump if A = 0
MOV A,R1           ;A=R1
JZ OVER               ;jump if A = 0
...
OVER:

Example:

Determine if R5 contains the value 0. If so, put 55H in it.
MOV A,R5           ;copy R5 to A
JNZ NEXT            ;jump if A is not zero
MOV R5,#55H
NEXT: ...

Jump if no Carry

JNC label                  ;jump if no carry, CY=0
  • If CY = 0, the CPU starts to fetch and execute instruction from the address of the label
  • If CY = 1, it will not jump but will execute the next instruction below JNC

Example:
Find the sum of the values 79H, F5H, E2H. Put the sum in registers R0 (low byte) and R5 (high byte).
MOV A,#0           ;A=0
MOV R5,A           ;clear R5
ADD A,#79H        ;A=0+79H=79H
JNC N_1              ;if CY=0, add next number
INC R5                 ;if CY=1, increment R5
N_1:
ADD A,#0F5H     ;A=79+F5=6E and CY=1
JNC N_2              ;jump if CY=0
INC R5                 ;if CY=1,increment R5 (R5=1)
N_2:
ADD A,#0E2H      ;A=6E+E2=50 and CY=1
JNC OVER           ;jump if CY=0
INC R5                  ;if CY=1, increment 5
OVER:
MOV R0,A           ;now R0=50H, and R5=02

8051 conditional jump instructions

  • All conditional jumps are short jumps

Instructions
Actions
JZ
Jump if A 0
JNZ
Jump if A 0
DJNZ
Decrement and Jump if A 0
CJNE A,byte
Jump if A byte
CJNE reg,#data
Jump if byte #data
JC
Jump if CY 1
JNC
Jump if CY 0
JB
Jump if bit 1
JNB
Jump if bit 0
JBC
Jump if bit 1 and clear bit
  • The unconditional jump is a jump in which control is transferred unconditionally to the target location
    • LJMP (long jump)
    • SJMP (short jump)

Call instruction

Call instruction is used to call subroutine
  • Subroutines are often used to perform tasks that need to be performed frequently
  • This makes a program more structured in addition to saving memory space
    • LCALL (long call)
    • ACALL (absolute call)
  • When a subroutine is called, control is transferred to that subroutine, the processor
    • Saves on the stack the the address of the instruction immediately below the LCALL
    • Begins to fetch instructions form the new location
  • After finishing execution of the subroutine
    • The instruction RET transfers control back to the caller
    • Every subroutine needs RET as the last instruction
ORG 0
BACK:
 MOV A,#55H          ;load A with 55H
MOV P1,A                ;send 55H to port 1
LCALL DELAY        ;time delay*
MOV A,#0AAH        ;load A with AA (in hex)
MOV P1,A                ;send AAH to port 1
LCALL DELAY
SJMP BACK             ;keep doing this indefinitely
;---------- this is delay subroutine ------------
ORG 300H                            ;put DELAY at address 300H
DELAY:
MOV R5,#0FFH       ;R5=255 (FF in hex), counter**
AGAIN:
DJNZ R5,AGAIN     ;stay here until R5 become 0
RET                                      ;return to caller (when R5 =0)***
END
  • *Upon executing “LCALL DELAY”, the address of instruction “MOV A,#0AAH” is pushed onto stack, and the 8051 starts to execute at 300H.
  • **The counter R5 is set to FFH; so loop is repeated 255 times.
  • ***When R5 becomes 0, control falls to the RET which pops the address from the stack into the PC and resumes executing the instructions after the CALL.

Differencce Between ACALL and LCALL

The only difference between ACALL and LCALL is
  • The target address for LCALL can be anywhere within the 64K byte address
  • The target address of ACALL must be within a 2K-byte range 
  • The use of ACALL instead of LCALL can save a number of bytes of program ROM space

Machine Cycles

CPU executing an instruction takes a certain number of clock cycles
  • These are referred as to as machine cycles
  • The length of machine cycle depends on the frequency of the crystal oscillator connected to 8051

Example:


  • Find the size of the delay in following program, if the crystal frequency is 11.0592MHz.

MOV A,#55H
AGAIN:
MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;---time delay-------
DELAY:
MOV R3,#200
HERE:
DJNZ R3,HERE
RET

Solution:
Machine cycle
DELAY: MOV R3,#200                              1
HERE: DJNZ R3,HERE                               2
RET 2
Therefore, [(200x2)+1+2]x1.085μs 436.255μs.

1 comment:

  1. so what is the difference in summery btn loop, jump and call

    ReplyDelete