GOKARAJURANGARAJU INSTITUTE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING
QUESTION & ANSWERS FOR MICROCONTROLLERS AND ITS APPLICATIONS BTECH IV-I SEMESTER (ECE A & B)
N SWETHA Asst. Professor ECE Dept.
UNIT II INSTRUCTION SET OF 8051
is an 8-bit constant included in instruction (0-255). addr16 .is an 11-bit address.net
Here is a list of the operands and their meanings:
A . rel .Means: Long Jump LAB5 (long jump to the address marked as LAB5).www. #data . Sol: Depending on operation they perform.
The other part of instruction. jump to the address marked as LOOP).com
www. For example:
www. bit . two or three. while some of them have one.is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535).).is any bit-addressable I/O pin. control or status bit. May be within the same 2KB page of program memory as the first byte of the following instruction. Classify the instruction set of 8051 and list out the instructions in each type. control register etc.is carry flag of the status register (register PSW). all instructions are divided in several groups:
Arithmetic Instructions Branch Instructions Data Transfer Instructions Logic Instructions Bit-oriented Instructions
The first part of each instruction. May be anywhere within 64KB of program memory. called MNEMONIC refers to the operation an instruction performs (copy.accumulator. If there is more than one operand in an instruction. addr11 . It can be any general-purpose register or a SFR (I/O port. LJMP LAB5 .jntuworld. #data16 . @Ri . and C .Means: Increment register R1 (increment register R1).
Q.Means: Jump if Not Zero LOOP (if the number in the accumulator is not 0. Some of the instructions have no operand.is any 8-bit address register of RAM. On the basis of it. they are separated by a comma. JNZ LOOP .is a 16-bit address.com
. Direct . Rn .jwjobs.is the address of a close memory location (from -128 to +127 relative to the first byte of the following instruction).jntuworld.is indirect internal or external RAM location addressed by register R0 or R1.).is one of working registers (R0-R7) in the currently active RAM memory bank. addition. assembler computes the value to add or subtract from the number currently stored in the program counter. Mnemonics are abbreviations of the name of operation being executed. called OPERAND is separated from mnemonic by at least one whitespace and defines data being processed by instructions. For example:
INC R1 . logic operation etc.
the result is stored in the first operand. multiplication etc. subtraction. After execution.LOOP .@Ri SUBB A.www.#data SUBB A. CJNE A.compare accumulator with 20. jump to the address marked as TEMP.@Ri Adds the indirect RAM to the accumulator ADD A. If they are not equal. division.jntuworld.com
www.return from a subroutine.net
.jwjobs. Arithmetic Instructions Mnemonic ADD A. For example: ADD A.R3 .if the number in the accumulator is not 0.#data Adds the immediate data to the accumulator
ADDC A. jump to the address marked as LOOP.direct Adds the direct byte to the accumulator ADD A.Rn Adds the register to the accumulator with a carry flag ADDC A.add R3 and accumulator.#data INC A INC Rn Adds the direct byte to the accumulator with a carry flag Adds the indirect RAM to the accumulator with a carry flag Adds the immediate data to the accumulator with a carry flag Subtracts the register from the accumulator with a borrow Subtracts the direct byte from the accumulator with a borrow Subtracts the indirect RAM from the accumulator with a borrow Subtracts the immediate data from the accumulator with a borrow Increments the accumulator by 1 Increments the register by 1
www.Rn Description Adds the register to the accumulator
ADD A.Rn SUBB A. JZ TEMP .direct ADDC A. ADD A.#20.direct SUBB A.The result of addition (A+R1) will be stored in the accumulator.R1 .@Ri ADDC A.jntuworld.
Arithmetic instructions Arithmetic instructions perform several basic operations such as addition.
www.jntuworld. Conditional jump instructions: a jump to a new program location is executed only if a specified condition is met. the program normally proceeds with the next instruction. Otherwise.jwjobs.net
INC Rx INC @Ri DEC A DEC Rn DEC Rx DEC @Ri INC DPTR MUL AB DIV AB DA A
Increments the direct byte by 1 Increments the indirect RAM by 1 Decrements the accumulator by 1 Decrements the register by 1 Decrements the direct byte by 1 Decrements the indirect RAM by 1 Increments the Data Pointer by 1 Multiplies A and B Divides A by B Decimal adjustment of the accumulator according to BCD code
Branch Instructions There are two kinds of branch instructions: Unconditional jump instructions: upon their execution a jump to a new location from where the program continues execution is executed.jntuworld.com
.www. Mnemonic ACALL addr11 LCALL addr16 RET RETI AJMP addr11 Description Absolute subroutine call Long subroutine call
Returns from subroutine Returns from interrupt subroutine Absolute jump
LJMP addr16 Long jump SJMP rel Short jump (from –128 to +127 locations relative to the following instruction)
Jump if the accumulator is zero.#data. Compares direct byte to the accumulator and jumps if not equal. Shortjump. Jump if the accumulator is not zero. Jump if direct bit is set and clears bit. If they have the suffix “X” (MOVX). Decrements direct byte and jump if not 0. DJNZ Rn.direct.rel Compares immediate data to indirect register and jumps if not equal. Short jump. CJNE @Ri. Short jump. Short jump.rel DJNZ Rx.com
. Jump indirect relative to the DPTR
JMP @A+DPTR JZ rel JNZ rel CJNE A.direct Description Moves the register to the accumulator Moves the direct byte to the accumulator Byte Cycle 1 2 1 2
JC rel JNC rel JB bit.jwjobs. Mnemonic MOV A. Short
Compares immediate data to the accumulator and jumps if not equal.jntuworld.rel jump. the data is exchanged with external memory. Short jump. Short jump.rel NOP Decrements register and jumps if not 0. Jump if carry flag is not set.com
www.Rn MOV A. CJNE A.rel
Jump if carry flag is set.jntuworld. The register the content of which is moved remains unchanged.#data. Short jump. Short jump.www.
CJNE Rn. Short jump. Short jump.rel JBC bit. Jump if direct bit is set.rel Compares immediate data to the register and jumps if not equal. No operation
Data Transfer Instructions Data transfer instructions move the content of one register to another.#data. Short jump.
A MOV @Ri.www.A MOV direct.A Moves the accumulator to the external RAM (8-bit address)
MOVX @DPTR.#data MOV @Ri.direct MOV Rn.@DPTR Moves the external RAM (16-bit address) to the accumulator MOVX @Ri.net
MOV A.A Moves the accumulator to the external RAM (16-bit address) PUSH direct Pushes the direct byte onto the stack
www.jntuworld.#data MOV direct.@Ri
Moves the code byte relative to the DPTR to the accumulator 1 (address=A+DPTR) Moves the code byte relative to the PC to the accumulator 1 (address=A+PC) Moves the external RAM (8-bit address) to the accumulator 1 1 1 1 2
3 3-10 3-10 4-11 4-11 4
.@Ri MOV A.com
www.#data Moves a 16-bit data to the data pointer MOVC A.#data Moves the indirect RAM to the direct byte Moves the immediate data to the direct byte Moves the accumulator to the indirect RAM Moves the direct byte to the indirect RAM Moves the immediate data to the indirect RAM
Moves the indirect RAM to the accumulator Moves the immediate data to the accumulator Moves the accumulator to the register Moves the direct byte to the register Moves the immediate data to the register Moves the accumulator to the direct byte Moves the register to the direct byte
1 2 1 2 2 2 2 3 2 3 1 2 2 3
2 2 2 4 2 3 3 4 4 3 3 5 3 3 3
MOV direct.A MOV Rn.@A+PC MOVX A.direct MOV @Ri.#data MOV Rn.@Ri MOV direct.jntuworld.@A+DPTR MOVC A.jwjobs.direct Moves the direct byte to the direct byte MOV direct.
Mnemonic ANL A.A Description AND register to accumulator AND direct byte to accumulator AND indirect RAM to accumulator AND immediate data to accumulator AND accumulator to direct byte Byte Cycle 1 2 1 2 2 3 1 2 1 2 3 1 1 2 2 2 3 4 1 2 2 3 4 1
POP direct XCH A.@Ri XCHD A.@Ri ORL direct.Rn Exclusive OR register to accumulator
Pops the direct byte from the stack/td> Exchanges the register with the accumulator Exchanges the direct byte with the accumulator Exchanges the indirect RAM with the accumulator
2 1 2 1
3 2 3 3 3
Exchanges the low-order nibble indirect RAM with the 1 accumulator
Logic Instructions Logic instructions perform logic operations upon corresponding bits of two registers.direct ANL A.Rn ORL A.#data ANL direct.Rn XCH A.#data AND immediae data to direct register ORL A.jntuworld.direct ORL A.jwjobs. the result is stored in the first operand.jntuworld.A OR register to accumulator OR direct byte to accumulator OR indirect RAM to accumulator OR accumulator to direct byte
ORL direct.www.direct XCH A.Rn ANL A.@Ri ANL A. After execution.#data OR immediate data to direct byte XRL A.com
#data XRL direct.A
Exclusive OR direct byte to accumulator Exclusive OR indirect RAM to accumulator Exclusive OR immediate data to accumulator Exclusive OR accumulator to direct byte
2 1 2 2 3 1 1 1 1
2 2 2 3 4 1 1 1 1 1 1 1
XORL direct. bit-oriented instructions perform logic operations.com
www.direct XRL A.com
.jntuworld. The difference is that these are performed upon single bits.jntuworld.#data Exclusive OR immediate data to direct byte CLR A CPL A SWAP A RL A RLC A RR A RRC A Clears the accumulator Complements the accumulator (1=0.@Ri XRL A. Mnemonic Description CLR C CLR bit SETB C SETB bit CPL C CPL bit Clears the carry flag Clears the direct bit Sets the carry flag Sets the direct bit Complements the carry flag Complements the direct bit Byte Cycle 1 2 1 2 1 2 1 3 1 3 1 3
www.www.jwjobs. 0=1) Swaps nibbles within the accumulator Rotates bits in the accumulator left
Rotates bits in the accumulator left through carry 1 Rotates bits in the accumulator right 1
Rotates bits in the accumulator right through carry 1
Bit-oriented Instructions Similar to logic instructions.net
ANL C. and the second byte of the instruction (for A7-A0). No flags are affected.bit OR direct bit to the carry flag ORL C. The 16-bit PC is then stored on the stack (low-order byte first) and the stack pointer is incremented twice.jntuworld. Give any four examples for program control flow instructions and explain. ACALL addr11 Absolute subroutine call
The ACALL instruction calls a subroutine located at the specified address.www. the program normally proceeds with the next instruction.
www. Otherwise. Sol: There are two kinds of program control flow instructions: Unconditional jump instructions: upon their execution a jump to a new location from where the program continues execution is executed./bit AND complements of direct bit to the carry flag 2 ORL C.bit Moves the direct bit to the carry flag MOV bit.com
.bit AND direct bit to the carry flag
2 2 2 2 2 3
ANL C./bit OR complements of direct bit to the carry flag MOV C. The subroutine that is called must be located in the same 2KByte block of program memory as the opcode following the ACALL instruction.C Moves the carry flag to the direct bit 2 2 2 2
Q. The address of the subroutine is calculated by combining the 5 high-order bits of the incremented PC (for A15-A11). The PC is incremented twice to obtain the address of the following instruction. the 3 high-order bits of the ACALL instruction opcode (for A10-A8). 1. Conditional jump instructions: a jump to a new program location is executed only if a specified condition is met.
www. LCALL addr16
Long subroutine call
The LCALL instruction calls a subroutine located at the specified address.jwjobs. RET
Returns from subroutine
The RET instruction pops the high-order and low-order bytes of the PC from the stack (and decrements the stack pointer by 2). This result is pushed onto the stack low-byte first and the stack pointer is incremented by 2. This instruction first adds 3 to the PC to generate the address of the next instruction.jntuworld.com
. The high-order and low-order bytes of the PC are loaded from the second and third bytes of the instruction respectively. Program execution is transferred to the subroutine at this address. Program execution resumes from the resulting address which is typically the instruction following an ACALL or LCALL instruction. No flags are affected by this instruction.com
www. No flags are affected by this instruction.
Execution returns to the instruction immediately after the point at which the interrupt was detected. Write in detail about the instruction SJMP. one instruction at the return address is executed before the pending interrupt is processed.www.com
www. The RETI instruction does not restore the PSW to its value before the interrupt. No other registers are affected by this instruction. This instruction pops the high-order and low-order bytes of the PC (and decrements the stack pointer by 2) and restores the interrput logic to accept additional interrupts. The interrupt service routine must save and restore the PSW.1 PC7-0 = (SP) SP = SP .jwjobs.1 RETI
Q. If another interrupt was pending when the RETI instruction is executed. RETI
Returns from interrupt subroutine
The RETI instruction is used to end an interrupt service routine.jntuworld. RETI Bytes Cycles Encoding 1 2 00110010 RETI PC15-8 = (SP) SP = SP .jntuworld.
www. SJMP rel Short jump (from –128 to +127 locations relative to the following instruction)
www.jntuworld. The instruction is 2 bytes long.www. consisting of the opcode and relative offset byte.com
Sol: The SJMP instruction encodes the destination address as a relative offset.jwjobs.