uc 2(vii)

30
INSTRUCTION SET OF 8051 RAVI DADSENA

Upload: ankita-jaiswal

Post on 29-Jun-2015

48 views

Category:

Engineering


1 download

DESCRIPTION

Instruction Set of 8051

TRANSCRIPT

Page 1: Uc 2(vii)

INSTRUCTION SET OF 8051

RAVI DADSENA

Page 2: Uc 2(vii)

Instruction syntax of 8051 ucAssembly text is usually divided into fields, separated by spaced and tabs. A format for a typical line from assembly language program are

• The first field which is optional ,is the label field,used to specify symbolic lebels.A label is an identifier that is assigned to the address of the first byte of the instruction in which it appears.The presence of a label is optional, but if present, the label provides a symbolic name that can be used in branch instructions to branch to the instruction.

• The second field is mnemonic ,which is compulsary.All instructions muct contain a mnemonic.

• The 3rd fields are operands.The presence of operands depends on the instruction.Some inst have no operands and some have 1 or 2 operands.If there are 2 operands ,they are separated by comma.

• The last field is comment field.The comments used are for our benefits and begins with semicolon and continues to the end of line,they tell us what the program is trying to accomplish.

Label : Mnemonic Operand1,operand 2 ; comment

Page 3: Uc 2(vii)

Programming model of 8051 uc8051 is a 8 bit registers.It basically includes the registers A,B, and R0-R7.It also contains 2 16 bit register DPTR & PC.The PC can be divided into 2 parts DPH(high) and DPL(low) and can be accessed seperately.

Page 4: Uc 2(vii)

Program status word registerIt is an 8 bit register and also refered as flag register.Although PSW register is 8 bit wide , only 6 bits of it are used by 8051.The t unused bits ar user definable flags.4 of the flags are called conditional flags, meaning that they indicate some conditions that result after an instruction executed.These 4 are carry, auxillary carry, parity ,overflow.In the below fig bits PSW3 & PSW4 are designated as RS0 & RS1 respectively and used to change the register banks.CY(crry flag) : The flag is set whenever there is a carry out from the D7 bit.This flag bit is affected after an 8 bit addition or subtraction.It can also be set to 1 or 0 directly by an inst such as ‘SET C’ and ‘CLR C’ where ‘SETB C’ stands for ‘set bit carry’ and ‘CLRC’ for ‘clear carry’.AC(auxillary carry flag) : If there is a carry from D3 to D4 during an add or sub operation this bit is set,otherwise it is getitng cleared.This flag is used by instructions that perform BCD.3

Page 5: Uc 2(vii)

• CY(carry flag): This flag is set whenever there is a carry out from D7 bit.This flag is affected after an 8 bit addition or subtraction.It can also be set to 1 or 0 directly by an instruction ‘SETB C’ & ‘CLR C’ where setb stands for ste bit carry and clrc stands for clear carry.

• AC(Auxiliary carry flag): If there is a carry from D3 to D4 bit during an add or sub operation,this bit is set : otherwise,it is cleared.This flag is used by instructions that perform BCD operations.

• P(Parity flag): The parity flag reflects the no of 1s in the A(accumulator) register only.If the A register contains an odd no of 1s,then P=1.Therefore,P=0 if A has an even no of 1s.

• OV(overflow flag): This flag is set whenever the result of a signed no operation is too large,causing the igh-order bit to overflow in to the sign bit.In general,the carry flag is used to detect errors in unsigned arithmatic operations.The overflow flag is only used to detect errors in signed arithmatic operations.

Page 6: Uc 2(vii)

Create assemble and run 8051 assembly language program

• The 1st step in the process is to write an assembly language program.The assembly language program can be written or typed with a text editor such as MSDOS or notepad in windows.This program must be saved with a file extension asm.

• In the 2nd step .asm source file containing the program code containing in the 1st step is fed to an 8051 assembler.The assembler translates assembly language instructions into machine code and it will produce an object file and a list file.The extension of object file is .obj and extension of the list file is .1st.This 1st file is an optional file.It is useful to the programmer because it lists all the opcodes and adresses as well as errors that the assembler detected.

• The 32d step is called linking.In this step the program takes one or more object files and produces an absolute object file with the extension abs.This file is used by 8051 trainers that have a monitor program.

• Finally the abs file is fed into a program called object to hex convert er(0H program) to produce a file with an extension hex This file is an executable file nd is ready to burn in to ROM.

Page 7: Uc 2(vii)

Steps in creating an executable program

Page 8: Uc 2(vii)

Instruction Groups• The 8051 has 255 instructions

– Every 8-bit opcode from 00 to FF is used except for A5.

• The instructions are grouped into 5 groups– Arithmetic– Logic– Data Transfer– Boolean– Branching

• ADD– 8-bit addition between the accumulator (A) and a second operand.

• The result is always in the accumulator.• The CY flag is set/reset appropriately.

• ADDC– 8-bit addition between the accumulator, a second operand and the

previous value of the CY flag.• Useful for 16-bit addition in two steps.• The CY flag is set/reset appropriately.

Page 9: Uc 2(vii)
Page 10: Uc 2(vii)

• DA– Decimal adjust the accumulator.

• Format the accumulator into a proper 2 digit packed BCD number.• Operates only on the accumulator.• Works only after the ADD instruction.

• SUBB– Subtract with Borrow.

• Subtract an operand and the previous value of the borrow (carry) flag from the accumulator.– A A - <operand> - CY.– The result is always saved in the accumulator.– The CY flag is set/reset appropriately.

• INC– Increment the operand by one.

• The operand can be a register, a direct address, an indirect address, the data pointer.

• DEC– Decrement the operand by one.

• The operand can be a register, a direct address, an indirect address.

• MUL AB / DIV AB– Multiply A by B and place result in A:B.– Divide A by B and place result in A:B.

Page 11: Uc 2(vii)
Page 12: Uc 2(vii)

Logical Operations• ANL / ORL: It performs AND operation between accumulator and 2nd operand and the

result is always stored in the accumulator.– Work on byte sized operands or the CY flag.

• ANL A, Rn• ANL A, direct• ANL A, @Ri• ANL A, #data• ANL direct , A• ANL direct, #data

• ANL C, bit• ANL C, /bit

• XRL: It performs XOR operation between accumulator and 2nd operand and the result is always stored in the accumulator.

Eg: MOV A,#32H 32H-0011 0010 MOV R4,#50H 50H-0101 0000 XRL A,R4 62H-0110 0010Works on bytes only.

Page 13: Uc 2(vii)

• RL A : Rotate left the accumulator.This rotates the bits of A left.The bits rotated out of A are rotated back into A at the opposite end.

Eg-MOV A,#69H A-01101001 RL A NOW A-11010010 RL A NOW A-10100101• RLC A : Rotate A left through carry.This rotates the bits of the accumulator left.The bits

are rotated out of register A are rotated into CY, and the CY bit is rotated into the opposite end of the accumulator.

Eg : CLR C CY-0 MOV A,#99H A-10011001 RLC A NOW A-00110010 AND CY-1 RLC A NOWA-01100101 AND CY-0• RR A : Rotate right the accumulator.This rotates the bits of A right.The bits rotated out of

A are rotated back into A at the opposite end.Eg-MOV A,#69H A-01100110 RR A NOW A-00110011 RR A NOW A-10011001• RRC A : Rotate A right through carry.This rotates the bits of the accumulator rightt.The

bits are rotated out of register A are rotated into CY, and the CY bit is rotated into the opposite end of the accumulator.

Page 14: Uc 2(vii)

• SWAP A: The swap instruction exchanges the low order and high order nibbles within the accumulator.No flags are affected by this instruction.

Eg-MOV A,#59H ;A=59H(0101 1001 in binary) SWAPA ;A=95H(1001 0101 in binary)

– Swap the upper and lower nibbles of the accumulator.

• No compare instruction.– Built into conditional branching instructions.

• CPL / CLRA,CLR bit– Complement / Clear accumulator.– Work on the accumulator or a bit.

• CLR P1.2

Page 15: Uc 2(vii)
Page 16: Uc 2(vii)

Data Transfer Instructions• MOV

– 8-bit data transfer for internal RAM and the SFR.• MOV A, Rn• MOV A, direct• MOV A, @Ri• MOV A, #data• MOV Rn, A• MOV Rn, direct• MOV Rn, #data• MOV direct, A• MOV direct, Rn• MOV direct, direct• MOV direct, @Ri• MOV direct, #data• MOV @Ri, A• MOV @Ri, direct• MOV @Ri, #data

Page 17: Uc 2(vii)

• MOVC– Move Code Byte

• Load the accumulator with a byte from program memory.• Must use indexed addressing

• MOVC A, @A+DPTR• MOVC A, @A+PC

• MOVX– Data transfer between the accumulator and a byte from external data memory.

• MOVX A, @Ri• MOVX A, @DPTR• MOVX @Ri, A• MOVX @DPTR, A

• PUSH / POP– Push and Pop a data byte onto the stack.– The data byte is identified by a direct address from the internal RAM locations.

• PUSH DPL• POP 40H

Page 18: Uc 2(vii)

• XCH– Exchange accumulator and a byte variable

• XCH A, Rn• XCH A, direct• XCH A, @Ri

• XCHD– Exchange lower digit of accumulator with the lower digit of the memory location specified.

• XCHD A, @Ri

• The lower 4-bits of the accumulator are exchanged with the lower 4-bits of the internal memory location identified indirectly by the index register.

• The upper 4-bits of each are not modified.

Data Transfer Operations• MOV

– 1-bit data transfer involving the CY flag• MOV C, bit• MOV bit, C

• MOV– 16-bit data transfer involving the DPTR

• MOV DPTR, #data

Page 19: Uc 2(vii)
Page 20: Uc 2(vii)

Boolean Operations• This group of instructions is associated with the single-bit operations of the 8051.• This group allows manipulating the individual bits of bit addressable registers and memory

locations as well as the CY flag.– The P, OV, and AC flags cannot be directly altered.

• This group includes:– Set, clear, and, or complement, move.– Conditional jumps.

• CLR– Clear a bit or the CY flag.

• CLR P1.1• CLR C

• SETB– Set a bit or the CY flag.

• SETB A.2• SETB C

• CPL– Complement a bit or the CY flag.

• CPL 40H ; Complement bit 40 of the bit addressable memory

Page 21: Uc 2(vii)

• ORL / ANL– OR / AND a bit with the CY flag.

• ORL C, 20H ; OR bit 20 of bit addressable memory with the CY flag

• ANL C, /34H ; AND complement of bit 34 of bit addressable memory with the CY flag.

• MOV– Data transfer between a bit and the CY flag.

• MOV C, 3FH ; Copy the CY flag to bit 3F of the bit addressable memory.

• MOV P1.2, C ; Copy the CY flag to bit 2 of P1.• JC / JNC

– Jump to a relative address if CY is set / cleared.

• JB / JNB– Jump to a relative address if a bit is set / cleared.

• JB ACC.2, <label>

• JBC– Jump to a relative address if a bit is set and clear the bit.

Page 22: Uc 2(vii)

• JC / JNC– Jump to a relative address if CY is set / cleared.

• JB / JNB– Jump to a relative address if a bit is set / cleared.

• JB ACC.2, <label>

• JBC– Jump to a relative address if a bit is set and clear the bit.

Page 23: Uc 2(vii)

Branching InstructionsCALL Instruction : It 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.• The 8051 provides 2 forms for the CALL instruction:• Long Call – LCALL – 3 byte inst.First byte is opcode.2nd and 3rd bytes are used for address

of target subroutine.Subroutine is located anywhere within 64 K byte address space.• When a subroutine is called,control is transferred to that subrouitne, the processor

saves on the stack the address of the inst immediately below the LCALL.Begins to fetch inst from the new location.After finishing execution of the subroutine.The inst RET transfers control back to the caller.Every subroutine needs RET as the last instruction.• Uses a 16-bit address similar to LJMP• The subroutine can be anywhere within 64K.

Page 24: Uc 2(vii)

Example of LCALL

Page 25: Uc 2(vii)

• Absolute Call – ACALL- 2 byte inst.The only difference between ACALL & LCALL is the target address for LCALL can be anywher within the 64k address.The target address of ACALL must be within a 2k byte range.The use of ACALL instead of LCALL can save a no of bytes of program ROM space.

• Uses an 11-bit address similar to AJMP• The subroutine must be within the same 2K page.

• Both forms push the 16-bit address of the next instruction on the stack and update the stack pointer.

Page 26: Uc 2(vii)

JUMP Instruction• Jump inst is used to redirect the program sequence to the specified location in the

instruction, Jump inst are classified into 2 categories conditional & unconditional jump.• The conditional jump checks the different conditions before program execution.All

conditional jumps are short jumps.The address of the target must within -128 to +127 bytes of the contents of the PC.

• The unconditional jump is a jump in which control is transferred unconditionally to the target location.

Page 27: Uc 2(vii)

• The 8051 provides four different types of unconditional jump instructions:a) Long Jump – LJMP : 3byte inst.1st byte is the opcode.2nd and 3rd byte represent the 16 bit

target address.Any memory location from 0000 to FFFFH.• Uses a 16-bit address.• 3 byte instruction capable of referencing any location in the entire 64K of

program memory.

b) Short Jump – SJMP : 2byte inst.1st byte is the opcode 2nd byte is the relative target address 00 toFFH.The advantage of SJMP is that address calculation becomes easy and it saves memory space.

• Uses an 8-bit signed offset relative to the 1st byte of the next instruction.

Page 28: Uc 2(vii)

– Absolute Jump – AJMP• Uses an 11-bit address.• 2 byte instruction

– The upper 3-bits of the address combine with the 5-bit opcode to form the 1 st byte and the lower 8-bits of the address form the 2nd byte.

• The 11-bit address is substituted for the lower 11-bits of the PC to calculate the 16-bit address of the target.– The location referenced must be within the 2K Byte memory page containing

the AJMP instruction.

– Indirect Jump – JMP• JMP @A + DPTR

• The 8051 provides 2 forms for the return instruction:– Return from subroutine – RET

• Pop the return address from the stack and continue execution there.– Return from ISV – RETI

• Pop the return address from the stack.• Restore the interrupt logic to accept additional interrupts at the same priority level

as the one just processed.• Continue execution at the address retrieved from the stack.• The PSW is not automatically restored.

Page 29: Uc 2(vii)

• The 8051 supports 5 different conditional jump instructions.– ALL conditional jump instructions use an 8-bit signed offset.

– Jump on Zero – JZ / JNZ• Jump if the A == 0 / A != 0

– The check is done at the time of the instruction execution.

– Jump on Carry – JC / JNC• Jump if the C flag is set / cleared.

– Jump on Bit – JB / JNB• Jump if the specified bit is set / cleared.• Any addressable bit can be specified.

– Jump if the Bit is set then Clear the bit – JBC• Jump if the specified bit is set.• Then clear the bit.

Page 30: Uc 2(vii)

• Compare and Jump if Not Equal – CJNE– Compare the magnitude of the two operands and jump if they are not equal.

• The values are considered to be unsigned.• The Carry flag is set / cleared appropriately.

• CJNE A, direct, rel• CJNE A, #data, rel• CJNE Rn, #data, rel• CJNE @Ri, #data, rel

• Decrement and Jump if Not Zero – DJNZ– Decrement the first operand by 1 and jump to the location identified by the second

operand if the resulting value is not zero.

• DJNZ Rn, rel• DJNZ direct, rel

• No Operation– NOP