Jump, Loop and Call Instructions

When a certain task is needed to be performed several times or for infinite time period, a loop is used. in other words, repeating a sequence of instructions a certain number of times or infinitely is called a loop.

There are various types of jumps, viz. :

Unconditional Jumps
Conditional Jumps

Conditional Jumps

When using conditional jump instructions, the status of a bit or a register may be continuously checked and depending on this a certain instruction may be used and the task is carried out.

JZ label; Jump to label if A = 0
MOV R0, #00H; Move the immediate data 00H to Register R0
MOV A,R0; Move the contents of R0 to Accumulator
JZ Next; Check the contents of A, if A=0 then jump to Next label
//As the contents of A is zero, the instruction will make the jump to Next label.//

MOV R1, #55H; Move the immediate data 55H to Register R1
MOV A,R1; Move the contents of R1 to Accumulator
JZ Next; Check the contents of A, if A=0 then jump to Next label
//As the contents of A is not zero, the instruction will not make the jump to Next label.//
Next: ....
JNZ label; Jump to label if A ≠ 0
MOV R0, #00H; Move the immediate data 00H to Accumulator
MOV A,R0; Move the contents of R0 to Accumulator
JNZ Next; Check the contents of A, if A≠0 then jump to Next label
//As the contents of A is zero, the instruction will not make the jump to Next label.//

MOV R1, #55H; Move the immediate data 55H to Register R1
MOV A,R1; Move the contents of R1 to Accumulator
JNZ Next; Check the contents of A, if A≠0 then jump to Next label
//As the contents of A is not zero, the instruction will make the jump to Next label.//
Next: ....
JC label; Jump to label if CY = 1
MOV A,#33H; Move the immediate data 33H to Accumulator
ADD A, #55H; Add the immediate number 55H to the contents of Accumulator
// The result will be stored in Accumulator, A = 33H + 55H = 88H. Carry is not generated.//
JC Next; Jump to Next label if CY = 1
// As carry bit is not generated; so ,the above instruction will not make the jump.//

MOV A, #0CCH; Move the immediate data CCH to Accumulator
ADD A, #55H; Add the immediate number 55H to the contents of Accumulator
// The result will be stored in Accumulator, A = CCH + 55H = 121H. Carry is generated.//
JC Next; Jump to Next label if CY = 1
// As carry bit is generated; so, the above instruction will make the jump to Next label.//
Next:....
JNC label; Jump to label if CY = 0
MOV A,#33H; Move the immediate data 33H to Accumulator
ADD A, #55H; Add the immediate number 55H to the contents of Accumulator
// The result will be stored in Accumulator, A = 33H + 55H = 88H. Carry is not generated.//
JNC Next; Jump to Next label if CY = 0
// As carry bit is not generated; so ,the above instruction will make the jump to Next label.//

MOV A, #0CCH; Move the immediate data CCH to Accumulator
ADD A, #55H; Add the immediate number 55H to the contents of Accumulator
// The result will be stored in Accumulator, A = CCH + 55H = 121H. Carry is generated.//
JNC Next; Jump to Next label if CY = 0
// As carry bit is generated; so, the above instruction will not make the jump to Next label.//
Next:....
JB label; Jump to label if bit = 0
SETB P2.0; Set P2.0 Port 2 bit 0 as input bit
JB P2.0, Do_This; Jump to Do_This label if P2.0 = 0 otherwise follow the next instruction
....
Do_This:....
JNB label; Jump to label if bit ≠ 0
SETB P2.0; Set P2.0 Port 2 bit 0 as input bit
JNB P2.0, Do_This; Jump to Do_This label if P2.0 = 1 otherwise follow the next instruction
.....
Do_This:....
JBC label; Jump to label if bit = 1 an then clear the bit, bit =0
SETB P2.0; Set P2.0 Port 2 bit 0 as input bit
JBC P2.0, Do_This; Jump to Do_This label if P2.0 = 1 and then reset P2.0 = 0; otherwise follow the next instruction
.....
Do_This:....
CJNE A, Register, label; Compare the contents of Accumulator, if A≠byte then jump to label. also, if A<Register then set the carry flag, CY=1 otherwise CY=0
MOV A,#3CH; Move immediate data 3CH to Accumulator
MOV R0,#4CH; Move immediate data 4CH to Accumulator
CJNE A, R0, Do_This; Compare the contents of Accumulator and Register R0;
;if A≠R0 then jump to Do_This label otherwise follow the next instruction
.....
Do_This:....

This instruction can also be used as following:

CJNE A, #data, label
CJNE Register, #data, label
CJNE @Register, #data, label
DJNZ Register, label; Decrement Register contents by 1, if Register ≠0 then jump to label
MOV R0, #05H; Move immediate data 05H to Register R0
AGAIN:DJNZ R0, AGAIN; Decrement Register R0 content by 1; if R0≠0 then jump to AGAIN label, otherwise follow the next instruction
....

When a task is needed to be performed certain number of times, then this instruction is used. But, there is a limit to this that for any of the Register the maximum count can be taken is up to FFH or 255d, because of the range of any of the Registers is 00H to FFH or 00d to 255d.

When the count is greater than FFH or 255d, nested loop is to be used. Let’s say a task is to be performed 10 times, then following method can be used.

MOV R0, #05H; Move immediate data 05H to Register R0
MOV R1, #02H; Move immediate data 02H to Register R1
AGAIN:DJNZ R0, AGAIN; Decrement Register R0 content by 1; if R0≠0 then jump to AGAIN label, otherwise follow the next instruction
DJNZ R1, AGAIN; Decrement Register R0 content by 1; if R0≠0 then jump to AGAIN label, otherwise follow the next instruction
....

Unconditional Jumps

The unconditional jumps are used to transfer the control without checking any conditions to the target location. There are two types of unconditional jumps, viz.

LJMP label; long jump to label

  • 3-byte instruction
  • First byte is the opcode
  • Second and third bytes represent the 16-bit target address
    • Any memory location from 0000 to FFFFH

SJMP label; short jump to label

  • 2-byte instruction
  • First byte is the opcode
  • Second byte is the relative target address
    • 00 to FFH (forward +127 and backward -128 bytes from the current PC)

To calculate the target address of a short jump (SJMP, JNC, JZ, DJNZ, etc.)

  • The second byte is added to the PC of the instruction immediately below the jump
  • If the target address is more than -128 to +127 bytes from the address below the short jump instruction
  • The assembler will generate an error stating the jump is out of range

CALL subroutines

Call instruction is used to call subroutine. Subroutines are often used to perform some specific tasks that need to be performed frequently. This makes a program more structured in addition to saving memory space.

LCALL label; Long call to label

  • 3-byte instruction
  • First byte is the opcode, Second and third bytes are used for address of target subroutine
  • Subroutine is located anywhere within 64K byte address space

ACALL label; Absolute call to label

  • 2-byte instruction
  • 11 bits are used for address within 2K-byte range

When a subroutine is called, control is transferred to that subroutine, and the processor performs the following tasks.

  • saves on the stack the the address of the instruction immediately below the LCALL/ACALL
  • Begins to fetch instructions form the new location
  • After finishing execution of the subroutine
  • The instruction RET transfers control back to the main program
  • Every subroutine needs RET as the last instruction to go back to the main program,otherwise the program remains in the subroutine forever giving false output.

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.

Use the following pattern for calling out subroutines. The format below shows how subroutine as well as short jump is used in the program.

MAIN: LCALL SUBR_1; Long call to subroutine 1
LCALL SUBR_2; Long call to subroutine 2
LCALL SUBR_3; Long call to subroutine 3
SJMP MAIN; Short jump to MAIN label and repeat the given tasks forever
; end of MAIN loop

SUBR_1: ...
...
RET; end of subroutine 1 and return the program flow to main program

SUBR_2: ...
...
RET; end of subroutine 2 and return the program flow to main program

SUBR_3: ...
...
RET; end of subroutine 3 and return the program flow to main program

END; end of the asm file

For example,

MOV A,#55H ;Move immediate data 55H to Accumulator
BACK: MOV P1,A ;Copy the contents of Accumulator into Port P1
ACALL DELAY ;Absolute call to DELAY subroutine
CPL A ; Complement the contents of Accumulator
SJMP BACK ; Short jump to BACK label to keep doing this indefinitely
;End of main program

DELAY:....
....
RET;end of DELAY subroutine and return the program flow to main program

END ; End of asm file

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: