Conditional Instructions 8051
Conditional Instructions 8051
The flow of program proceeds in a sequential manner, from one instruction to the next
instruction, unless a control transfer instruction is executed. The various types of control transfer
instruction in assembly language include conditional or unconditional jumps and call instructions.
The register is loaded with the counter for the number of repetitions prior to the start of the loop.
In this instruction, both the registers decrement and the decision to jump are combined into a
single instruction. The registers can be any of R0–R7. The counter can also be a RAM location.
Example
Multiply 25 by 10 using the technique of repeated addition.
Solution − Multiplication can be achieved by adding the multiplicand repeatedly, as many times
as the multiplier. For example,
25 * 10 = 250(FAH)
25 + 25 + 25 + 25 + 25 + 25 + 25 + 25 + 25 + 25 = 250
AGAIN:DJNZ R2,
Drawback in 8051 − Looping action with the instruction DJNZ Reg label is limited to 256
iterations only. If a conditional jump is not taken, then the instruction following the jump is
executed.
When we use a loop inside another loop, it is called a nested loop. Two registers are used to
hold the count when the maximum count is limited to 256. So we use this method to repeat the
action more times than 256.
Example
Write a program to −
Solution − Since 700 is greater than 255 (the maximum capacity of any register), two registers
are used to hold the count. The following code shows how to use two registers, R2 and R3, for
the count.
CPL A ;complement
Instruction Action
JZ Jump if A = 0
JNZ Jump if A ≠ 0
JC Jump if CY = 1
JNC Jump if CY ≠ 1
JB Jump if bit = 1
https://www.tutorialspoint.com/embedded_systems/es_instructions.htm 2/5
5/23/22, 2:19 AM Embedded Systems - Instructions
JNZ (jump if A is not equal to 0) − In this instruction, the content of the accumulator is
checked to be non-zero. If it is not zero, then the 8051 jumps to the target address.
JNC (Jump if no carry, jumps if CY = 0) − The Carry flag bit in the flag (or PSW) register
is used to make the decision whether to jump or not "JNC label". The CPU looks at the carry
flag to see if it is raised (CY = 1). If it is not raised, then the CPU starts to fetch and execute
instructions from the address of the label. If CY = 1, it will not jump but will execute the next
instruction below JNC.
Note − It must be noted that all conditional jumps are short jumps, i.e., the address of the target
must be within –128 to +127 bytes of the contents of the program counter.
LJMP (long jump) − LJMP is 3-byte instruction in which the first byte represents opcode,
and the second and third bytes represent the 16-bit address of the target location. The 2-
byte target address is to allow a jump to any memory location from 0000 to FFFFH.
SJMP (short jump) − It is a 2-byte instruction where the first byte is the opcode and the
second byte is the relative address of the target location. The relative address ranges from
00H to FFH which is divided into forward and backward jumps; that is, within –128 to +127
bytes of memory relative to the address of the current PC (program counter). In case of
forward jump, the target address can be within a space of 127 bytes from the current PC. In
case of backward jump, the target address can be within –128 bytes from the current PC.
https://www.tutorialspoint.com/embedded_systems/es_instructions.htm 3/5
5/23/22, 2:19 AM Embedded Systems - Instructions
5 0006 08 INC R0
7 0008 04 INC A
10 000D E4 CLR A
18 0017 END
CALL Instructions
CALL is used to call a subroutine or method. Subroutines are used to perform operations or
tasks that need to be performed frequently. This makes a program more structured and saves
memory space. There are two instructions − LCALL and ACALL.
To make a successful return to the point after execution of the called subroutine, the CPU saves
the address of the instruction immediately below the LCALL on the stack. Thus, when a
subroutine is called, the control is transferred to that subroutine, and the processor saves the PC
(program counter) on the stack and begins to fetch instructions from the new location. The
instruction RET (return) transfers the control back to the caller after finishing execution of the
subroutine. Every subroutine uses RET as the last instruction.
https://www.tutorialspoint.com/embedded_systems/es_instructions.htm 4/5
5/23/22, 2:19 AM Embedded Systems - Instructions
within the 64K-bytes address space of the 8051, while the target address of CALL is within a 2K-
byte range.
https://www.tutorialspoint.com/embedded_systems/es_instructions.htm 5/5