Download as pdf
Download as pdf
You are on page 1of 27
APPENDIX A 8051 INSTRUCTIONS, TIMING, AND REGISTERS OVERVIEW In the first section of this appendix, we describe the instructions of the 8051 and give their formats with some examples. In many cases, more detailed programming examples will be given toclarify thae ceation sons; These instructions will operate on any 8031, 8032, 8051, ot 8052 microcontroller. The fet Section concludes with a list of machine cycles (clock counts) for each 8051 instruction, In the second section, a list of all the 8051 registers is provided for ease of reference for the 8051 programmer. 453 “SECTION A.1: THE 8051 INSTRUCTION SET ACALL target address Function: Absolute Call Flags: None ACALL stands for “absolute call program counter (PC). See LCALL for more discu ADD A,source byte Function: ADD Flags: OV, AC, CY “This adds the source byte to the accumulator (A), and places the result in A. Since register A is one byte in size, source operands must also be one byte. ‘The ADD instruction is used for both signed and unsigned numbers. Each one is discussed separately. Unsigned addition In the addition of unsigned numbers, the status of CY, AC, and OV may change. The most important of these fag isCY_ It becomes 1 when there isa carry from D7 out in 8-bit (DO - D7) operations. Example: Mov A, #45H ADD A, #4FH Example: MOV A, HOFEH Mov 83, #751 ADD A,R3 Example: MOV A, #25H ADD A, #42 TH (25H+42H=67H) JAC=0 Addressing modes The following addressing modes are supported for the ADD instruction: 1 Immediate Example: ADD A, #25H 2 Register: Example: ADD A, R3 3. Direct: rect Example, ADD A, 30H ;add i loc. 30H 4. Register-indirect where is0 or isl only Examples: ADD A, @RO :add to A data pointed to by RO Bi ;add to A data pointed to by Ri Je: the contents of RAM locations 50H to 3FH are added together, and the sum is sav est 8 4 APPENDIX cuR A = 7R<0 MOV R0,#50H ;source pointer a Mov R2,#16 jeounter R3, #0 jelear R3 ,aRO ;ADD to A from source Ba PIF C¥=0 go to next byte BB jotherwise keep carries Ro ynext location R2;Act seepeatetor all bytes. i 70H/A yeave low byte of sum sn ae a Mov 71H,R3 yeave high byte of sum Notice in all the above examples that we ignored the status of the OV flag. Although ADD instructions do affect OV, sin the context of signed numbers that the OV flag has any significance. This is discussed next. gned addition and negative numbers In the addition of signed numbers, special attention should be given tothe overflow flag (OV) since this indicates Pane an crtor in the result of the addition. There are two rules for setting OV in signed number operation. The arflow flag is set to 1: If there is a carry from Dé to D7 and no carry from D7 out. If there is a carry from D7 out and no carry from D6 to D7. Notice that if there is a carry both from D7 out and from D6 to D7, OV = 0. Example: Mov A, #48 ;A=0000 1000 MOV RL, Hed ;R1=0000 0190 ADD A,RL }A=0000 1100 OV=0,CY=0 Notice that D7 = 0 since the result is positive and OV = 0 since there is neither a carry from D6 to D7 nor any carry ryond D7. Since OV =0, the result is correct [(+8) + (+4) = (+12)] Example: Mov A, #466 ;A=0100 0010 NOV R4,#+69 ;R4=0100 0202 ADD A, R4 pAe1000 0111 = -122 (INCORRECT) CY=0, D7=1, OV=1 In the above example, the correct result is +135 [(+66) + (+69) = (+135), but the result was -121, OV = onat this eevee, Note that D7 = I since the resultis negative; OV = 1 since there is a carry from Dé to D7 and C isan indica- =0. Example: Mov A, #-12 ;Ae1111 0100 Mov R3,#+18 ;R3=0001 0010 000 0110 (+6) correct and © ADD A, R3 ), since there is a carry from D6 to D7 and a carry from D7 out Mo OV = 0 since th 7. APPENDIX. A Example MOV &,#-126 ;Az1000 0010 MOV R7,#-127 ;R7=1000 0001 e ADD A,R7 ;Ae0000 0021 (+3, WRONG) :D7=0, OV=1 C¥= I since there isa carry from D7 out but no carry from Dé to D7. From the above discussion we conclude that while CY is important in any addition, OV is extremely important If signed umber addition since itis Used to indicate whether or Hot the result is valid. As we will see in instruction " 2" the AC flag is used in the addition of BCD numbers. OV is also used in DIV and MUL instructions as well. See t description of these two instructions for further details, ADDC A,source byte Function: Add with carry Flags: OV, AC,CY This will add the source byte to A, in addition to the CY flag (A = A + byte + CY). IFCY = CY is also added to A.IFO addition prior to this instructi 0 prior to the instruction, source is added to destination plus 0. This is used in multibyt In the addition of 25F2H to 3189H, for example, we use the ADDC instruction as shown below. ’ Example: CLR C iC¥=0 MOV A, #aoH pA=89H ADDC A, #0) 7AsB9H+F2H+0=17BH, AR7B, CY. HOV R3,A :SAVE A MoV. A, #31H ADDC A, #258 jA231H425H4 1257H ‘Therefore the result is: 25528 4 31858 57H The addressing modes for ADDC are the same as for “ADD A. byte” AJMP target address Function: Absolute jump. Flag: None SIMP stands for “absolute jump.” It transfers program execution to the target address unconditionally. The ta address for this instruction must be within 2K bytes of program memory. See LIMP for more discussion on the ANL clest-byte,source-byte wpical AND for byte variables ne affected 2 logical AND on the operands, bit by bit, storing the result in the that both the source and destination values are byte-size onl 0022 0000 (0000 xample lov A, 83: yo01 1002 1001 2H lov R4, #50H NE ARS. ‘or the ANL instruction there are a total of six addressing modes. In four of them, the ‘accumulator must bé thie” nation. They are as follows: Immediate: Register: Direct ANL Adata ANLARn ANL A.direct, Register-indirect: Example: Example: Example: Example: 32 0022 0010 500201 9000 100001 0000 — ANL A, #251 ANL A,R3 ANL A,30H ;AND A with data in RAM location 30H ANL A,@RO ;AND A with data pointed to by RO In the next two addressing modes the destination is a direct address (a RAM location or one of the SFR registers) e the source is either A or immediate data. ANL direct, #tdata Example: Assume that RAM location 32H has the value 67H, Find its content after execution of the following code. ANL 32H aan 44H 0100 0100 67H 0110 0111 44H 0100 0101 Therefore, Or look at these examples: ANL PL, #11111110B ANL P1, #012111118 ANL Pi, #11110111B ANL Pi, #11111100B it has 44H - ymask PL. rmask P21 pmask PL imask Pi 0(D0 of Port 1) (D7 of Port 1) 3(D3 of Port 1) 0 and P2.2 ‘The above instructions clear (mask) certain bits of the output port of PL ANLdirectA Example: Find the contents of register B after the following: Mov 8, # voy a, # aa 67H ANL OFOH,A ;BeaaH A=67H A AND after BiB is located at RAM FOH) the operation B=44H Note: We cannot use this to mask bits of input ports! For example, “ANL A, P2” is incorrect! NL Csource-bit Function Flag. Logical AND for bit vai PPENDIN flag bit is ANDed with a source bit and the result is placed in carry: Therefore, if source © Example: Write code t6 clear the accumulator if bits P2.1 and P2.2 are both high; otherwise, make A = FFH. NOV A, HOFFH jae FFA MOV C,P2.2 feopy bit P2.1 to carry flag ANL C,P2.2 jand then INC 31 ijump if one of them is low CLR OA Bud Another variation of this instruction involves the ANDing of the CY flag bit with the complement of the source bit, Its format is “ANL C, /bit”. See the following example. Example: Clear A if P2.1 is high and P22 is low; otherwise, make A = FFH. MOV A, #0FFH MoV C,P2.1 iget a copy of P2.1 bit ANL C,/P2.2 ;AND P2.1 with complement of P2.2 gnc Bl CLR A CINE dest-byte,source-byte,target Function: Compare and jump if not equal Flag: cy The magnitudes of the source byte and destination byte are compared. If they are not equal, it jumps to the target. address. Example: Keep monitoring P1 indefinitely for the value of 99H. Get out only when P1 has the value 99H. MOV P1,OFFH jmake P1 an input port BACK Mov A,P1 pread Pi CONE A, #99, BACK ikeep monitoring Notice that CJNE jumps only for the not-equal value. To find out if it is greater or less after the comparison, we must check the CY flag, Notice also that the CJNE instruction affects the CY flag only, and after the jump to the target ad dress the carry flag indicates which value is greater, as shown here. In the following example, PI is read and compared with value 65. Then: If PL is equal to 65, the accumulator keeps the result, eee Sees If PI hasa value less than 65, R2 has the result, and finally, IF P1 hasa value greater than 65, itis kept by R3. 1 At the end of the program, A will contain the equal value, or R2 the smaller value, or R3 the greater value Example READ Pl }IS IT 65? :YES, A KEEPS :NO EXIT E THE SMALLER IN R2 exiT THE LARGER IN R3 APPEND IX. ‘This instruction supports four addressing modes. In two of them, A is the destination Immediate: CJNE A,tdata,target Example: CINE A, #96,NEXT ;JuMe IF A IS NOT 96 Direct CINE A.direct.target Example: CINE A, 40H, NEXT ;JUMP TF A NOT = VWITH THE VALUE HELD BY RAM LOC. 40H Notice the keene of the™®” sigan in the above instruction. This indicates RAM location 40H. Notice in this mode twee can test the value at an input port. This isa widely used application of this instruction. See the Following Mov PL, #OFF 1 is an input port Mov A, #100 = 100 | HERE: —-CUNE. A, PL, HERE SWAIT HERE TIL PL 200 In the third addressing mode, any register, RO - R7, can be the destination. 1 Register: CINE Rn data target 8 ‘BE Beample: GJNE R5,#70,NEXT rump if RS is not 70 In the fourth addressing mode, any RAM location can be the destination. The RAM location is held by register 5 Dor RI 5 | -Register-indirect: CINE @Ri,Adata.target ¢ | Example: CONE @R1, #80,NEXT sjump if RAM C jlocation whose address is held by Ri 7 jis not equal to 80 } Notice that the target address can be no more than 128 bytes backward or 127 bytes forward, since it is a -byte {_ mstruction. For more on this see SIMP. : , CLRA { Function: Clear accumulator NS Fag: None are affected “This instruction clears register A. AI bits of the accumulator are set t0 0. Example cLR OA ov RO,A jelear RO Mov R2,A jelear R2 Mov Pia jelear port 1 CLR bi Function: Clear bit “This instruction clears a single bit. The bit can be the carry flag, or any bit-addressable location in the 8051, Here are some exam ples of its format “CPL'A Function Complement accumulator Flags: None are affected e This complements the contents of register A, the accumulator. The result is the 1’s complement of the accumulatot ‘That is: Os become Is and 1s become 0s. anne, . MUMOV. A, WSSH7A=01010201 same AGAIN: CPL A pcomplement reg. A Mov. PL,A stoggle bits of P1 ‘SUMP AGAIN jcontinuously CPL bit Function: Complement bit This instruction complements a single bit. The bit can be any bit-addressable location in the 8051. Example: SETB P1.0 jset P1.0 high AGAIN: CPL P1.0 complement reg. bit SMP. AGAIN :continuously DAA Function: Decimal-adjust accumulator after addition Flags: CY ‘This instruction is used after addition of BCD numbers to convert the result back to BCD. The data is adjusted in the following two possible cases. 1. Itaddds 6 to the lower 4 bits of A if it is greater than 9 or if AC = 1. 2. Italso adds 6 to the upper 4 bits of A if tis greater than 9 or if CY = 1 Example: MOV A, 47H ;A=0100 0111 ADD A, #208 JAs47H+20H-7FH, invalid BCD DA OA. ;A=1000 0101=85H, valid BCD am | + 38H j TPH (invalid BCD) | 6H (after DA SSH (valid BCD) In the above examplegsince the lower nibble was greater than 9, DA added 6 to A. Ifthe lower nibble is less than 9 but AC =1, italso adds 6 tothe lower nibble. See the following example Example: MOV A, #291 ADD A,#1aH DA OA 460 APPENDIX A ce Pd aq E +188 GiH incorrect result in BCD) + 6H G7H correct result in BCD “The same thing can happen for the upper nibble. See the following example. Example: Mov A, #52H sted : semana ADD A, #918 DAA ;A=0100 0011 AND CY: 52H + saH 3H (invalid BCD) 46 (after DA A, adding to upper nibble) Yas valid BCD Similarly, ifthe upper ble is less than 9 and CY = 1, it must be corrected. See the following example. Example: ov A, #94 ;A=1002 0100 ADD A, #91 {Aa0010 0101 INCORRECT DAA ;Re1000 0101, VALID BCD ;FOR 85,C¥=2 Itis possible that 6 is added to both the high and low nibbles, See the following example. Example: Nov A, #54H ;A=0101 0200 ADD A, #87H Yme1101 1011 INVALID BCD DA OA Jaco100 0001, C¥=1 (BCD 141) IEC byte Function: Decrement Flags: None “This instruction subtracts 1 from the byte operand. Note that CY (carry/ borrow) is unchanged even if a value 00 is Jecremented. and becomes FF. This instruction supports four addressing modes. 1. Accumulator: DECA Example: DEC A 2 Register Rn Example: DEC RL or DEC R3 3. Direct: DEC direct Example: DEC 40H ;dec byte in RAM location 40H 1. Register-indirect: DEC @Ri swwhere i= 0.0r 1 only Example: DEC aRO Mgecr. byte pointed to by RO DIV AB £ Function: Divide Flags: CV and OV This insstruction divides a byte accumulator by the byte in register B. It is assumed that both registers A and Bcon- ster A and the remainder in register B. If you divide tain an unsigned byte. After the disision, the quotient will be in re | APPENDIX A se < 2Y 2eto (that is, set register B = 0 before the execution of “DIV AB"), the values in register A and B are undefined ai the OV flag is set to high to indicate an inwalid result. Notice that CY is always 0 in this instruction. Example: Mov a, #35 Mov B, #10 DIV AB ;Re3 and B=5 ieee Example Aorta or nN Mov A, #972 Mov By #12H DIV AB ;A=8 and B-7 Notice in this instruction that the carry and OV flags are both cleared, unless we divide A by 0, in which case result is invalid and OV = 1 to indicate the invalid condition, DJNZ byte, target Function: Decrement and jump if not zero Flags: None In this instruction a byte is decremented, and if the result is not zero it will jump to the target address. Example: Count from 1 to 20 and send the count to P1. CIR A ° Mov R2,#20 ;R2=20 counter BACK: INC A 5 Mov PLA DUNZ R2,BACK — ; repeat if R2 not = zero The following two formats are supported by this instruction. Register: DJNZ Rn,target (where n=0 to 7) Example: DNZ R3, HERE 2. Direct. — DJNZ directrtarget Notice that the target address can be no more than 128 bytes backward or 127 bytes forward, since itis a 2-byte instruction. For more on this see SIMP. INC byte 3 = Funetion: Increment Flags: None This instruction adds 1 to the zegister or memory location specified by the operand. Note that CY is not.affecte! even if value FF is incremented to 00. This instruction supports four addressing modes } 1. Accumulator INCA Example, INC A 2 Register: INC Rn Example: INC R1 or INC R5 3. Direct INC Example N loc. 308 4. Register-indirect: INC @Ri (i = 0 or) Example INC apo since 362 APPENDIX SY PTR netion: Increment data pointer igs: None egister DPTR (data pointer) by 1. Notice that DPTRs the only 16-bit register uis instruction increments the 16-bit r t version of this instruction. inbe incremented. Also notice that there is no decrement cample: oy SPR Gieeen—perRa6FFH oe sc DPTR ynow DPTR=1700H _ . * it,target also: JNB bit target Jump ifbit set Jump if bit not set None .dadress if a given bit is high or low. In the hese instructions are used to monitor a given bit and jump to a target a fen bit can be any of the bit- 1B tthe bits high it wil jamp, while for JNB ifthe bit slow it will jump. The gi essable bits of RAM, ports, of registers of the 8051. =xample: Monitor bit PL continously. When it becomes low, send SSH to P2 SETB P1.5 jake P1.5 an input bit HERE: JB P1.5,HERE jstay here as long as Pl. Mov P2,#55H jsince Pl.S=0 send 55H to P2 Example: See if register A has an even number. If so, make it odd JB ACC.0,NEXT ;jump if it is oad INC A jit is even, make it odd NEXT: Example: Monitor bit P1.4 continously. When it becomes high, send 35H t0 F2 jmake P1.4 an input bit jstay here as long as P1.4=0 jsince Pi.4=1 send 55H to P2 SETB P1.4 HERE: JNB P1.4,HERE MOV P2,#55H Example: See if register A has an even number. Ifnat, make it even. sa ACC.0,NEXT Jump if DO is 0 (even) INC A ;Dozi, make it even NEXT sC bit target Function: Jump if bit is set and clear bit Flags: None Ifthe desived bitis high it will jump to the target address at the same time the bt is cleared to zero. ° Example: ‘The following instruction will jump to label NEXT if D7 of register A is high; at the some time D7 is cared to zero. \PPENDIX A -_ #Struction. For more on this see SMP. JC target E a Function: Jump if CY Regs Now This instruction examines the CY flag; if it is high, it will jump to the target address. ma oramaas — “i “ JMP @A+DPTR Function: Jump indirect, Flags: None The JMP instruction is an unconditional jump to a target address. The target address is provided by the total sum register A and the DPTR register. Since this is not a widely used instruction we will bypass further discussion of it. JNB bitytarget ‘See JB and JNB. JNC target Function: Jump if no carry (CY = 0) Flags: None This instruction examines the CY flag, and if its zero it will jump to the target address, Example: Find the total sum of the bytes F6H, 98H, and 8AH. Save the carries in register R3. CLR OA Mov R3,A ADD A, #OF6H INC OVERL INC R3 OVER1: ADD A, #98H SNC OVER2 INC R3 OVER2: ADD A, #8AH INC OVER? INC R3 OVER? Notice that this is a 2-byte instruction and the target address cannot be farther than -128 to +127 bytes from the Program counter. See | condition for more on this. INZ target © Function: Jump if accumulator is not zero Flags: None This instruction jomps if register A has a value c than zero.” i i . ‘Mov RS, 16 ;set counter Nov R3,#0 {pa holds"nunber of 0s ov RL, #40 jaddress BACK: MOV A,@RI ibring data to reg A Nz OVER INC RS over: INC RL ypoint to next location DINZ RS,BACK ‘Pepeat for all locations AR ave progeant will Bing the data mtorthe accumulatorand if itis zeropitincrements counter R3. Notice {hot isa d-byte instruction; therefore, the target address cannot be more than _128 to +127 bytes away from the propa inter, See J condition for further discussion on this. target | Function: Jump if A= zero + Flags: None $ This instruction examines the contents of the accumulator and jumps fit has value 0. Example: A string of bytes of data is stored in RAM locations Ser, at address 50H. The end of the string is indi- ect by the value 0. Send the values to PI one by one with a delay between each. Mov RO, #50H jaddress BACK: MOV. A, @RO jbring the value into reg A gz EXIT yend of string, exit 2 NOV. PLLA jsend it to Pl 7 ACALL DELAY A INC RO ypoint to next i SUMP BACK : Bi) exrr: ¢ {Notice that this is a2-byte instruction; therefore tre target address cannot be more than ~128 to +127 bytes away {__omthe program counter See J condition for further discussion on this, y ae j condition target Function: Conditional jump Inthis type of ump, control is transferred toa target adress if cory conditions are met. The target address cannot e198 to #127 bytes away from the current PC (program counter) ic Jump carry jumpifCY=1 JNC jump no carry jump if CY =0 iz Jump zero jump if register A =0 JNZ jump no zero jump if register A is not 0 INBbit Jump no bit jump ifbit = 0 JBbit Jump bit jump if bit =1 IBC bit jump bit clear bit jump if bit = 2 and clear bit DINZ Rn, De€rement and jump if not zero CINE A. #val Compare A with value and jump if not equal Notice that all “J condition” instructions are short jumps, meaning that te UE address 150 J! m oA vow Ar30032008 [eee © RRCA z Faracton: Rotate A right vou eary ol. APPENIDIX A. 473,

You might also like