{"id":66,"date":"2018-07-26T09:40:28","date_gmt":"2018-07-26T09:40:28","guid":{"rendered":"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=66"},"modified":"2018-08-10T10:40:20","modified_gmt":"2018-08-10T10:40:20","slug":"microcontrollers-and-embedded-processors-2","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/chapter\/microcontrollers-and-embedded-processors-2\/","title":{"rendered":"Microcontrollers and Embedded Processors"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this lecture, Instruction set of 8051 microcontroller will be discussed in detail. Arithmetic and logical instructions, and control transfer instructions will be discussed with examples.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.Instruction set<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The process of writing program for the microcontroller mainly consists of giving instructions (commands) in the specific order in which they should be executed in order to carry out a specific task. As digital circuit cannot \u201cunderstand\u201d what for example an instruction \u201cif the push button is pressed- turn the light on\u201d means, then a certain number of simpler and precisely defined orders that decoder can recognize must be used. All commands are known as INSTRUCTION SET. All microcontrollers compatible with the 8051 have a total of 255 instructions.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In these 255 instructions, many instructions are considered to be \u201cdifferent\u201d, even though they perform the same operation, so there are only 111 truly different commands. For example: ADD A,R0, ADD A,R1, ... ADD A,R7 are instructions that perform the same operation (addition of the accumulator and register). Since there are 8 such registers, each instruction is counted separately. Taking into account that all instructions perform only 53 operations (addition, subtraction, copy etc.) and most of them are rarely used in practice, there are actually 20-30 abbreviations to be learned, which is acceptable.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1 Types of instructions<\/strong>\r\n\r\n&nbsp;\r\n\r\nDepending on the operation that they perform, the instructions are divided into several groups:\r\n\r\n&nbsp;\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Arithmetic Instructions\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Branch Instructions\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Data Transfer Instructions\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Logic Instructions and\r\n\r\n\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Bit-oriented Instructions.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first part of each instruction, called MNEMONIC refers to the operation an instruction performs (copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed. For example:<\/p>\r\n&nbsp;\r\n\r\nADD A, R0 - ADD mnemonic (A, R0- operands registers)\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong><em>1.1.1 Arithmetic Instructions<\/em><\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Arithmetic instructions perform addition, subtraction, multiplication, division, increment and decrement operations. After execution, the result is stored in the first operand. For example: <em>ADD<\/em> A,R1 - The result of addition (A+R1) will be stored in the accumulator<em>.<\/em><\/p>\r\n&nbsp;\r\n\r\nTable .1 Execution Table for Arithmetic instructions\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-67 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25.png\" alt=\"\" width=\"606\" height=\"358\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">From Table.1 it is evident that always one of the operand is stored in accumulator. Addition is done through ADD and ADDC instruction. Addition with carry is needed when doing multiple addition. Subtraction is always done with borrow using SUBB instruction. Increment and decrement by one bit can be done with INC and DEC instructions. The DA A instruction is the decimal adjust used for BCD addition.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The MUL AB instruction multiplies the Accumulator by the data in the B register and puts the 16-bit product into the concatenated B and Accumulator registers. The DIV AB instruction divides the Accumulator by the data in the B register and leaves the 8-bit quotient in the Accumulator, and the 8-bit remainder in the B register.<\/p>\r\n&nbsp;\r\n\r\nExamples for addition and subtraction:\r\n\r\n&nbsp;\r\n\r\nADD A,&lt;src-byte&gt;\r\n\r\nAssume A=09h and R0=07h\r\n\r\n<strong>ADD A,R0 ; (A) \u2190 (A) + (R0), A=10h<\/strong>\r\n\r\n<strong>ADDC A,Rn ; (A) \u2190 (A) + (C) + (Rn)<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nIn the ADDC instruction of previous example if the carry bit is 1 then 10h+1 is 11h and that is stored in Accumulator.\r\n\r\nSUBB A,&lt;src-byte&gt;\r\n\r\nAssume A=09h, R0=07h, carry flag=1\r\n\r\n<strong>SUBB A,R0 ; (A) \u2190 (A) - (C) - (R0) , A=1<\/strong>\r\n\r\n&nbsp;\r\n\r\nExamples for multiplication and division :\r\n\r\n&nbsp;\r\n\r\nAssume A=09h,B=02h\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0<strong>MUL AB ; (A)9 \u2190 (A) X (B)<\/strong>\r\n\r\n(B)2,\u00a0 A=12h,B=00h\r\n\r\nMultiplying A and B gives 18 in decimal and in hexadecimal it is 12h. Here, LSB is stored in A register and MSB is stored in B register. There is no overflow in the above instruction, if there is overflow then those values are stored in B register.\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0<strong>DIV AB ; (A)9 \u2190 (A)\/(B)<\/strong>\r\n\r\n&nbsp;\r\n\r\n(B)2 , A=04h,B=01h\r\n\r\nDivision of A and B gives quotient as 4 and remainder as 1. A holds the quotient and B holds the remainder.\r\n\r\n&nbsp;\r\n\r\n<strong><em>1.1.2. Logical Instruction<\/em><\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the result is stored in the first operand. These instructions perform basic logic operations such as OR,AND,XOR ,complement and rotate operations.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-68 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26.png\" alt=\"\" width=\"574\" height=\"341\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">All logical instructions are listed in Table.2. Accumulator holds one of the operand always. ANL performs the logical AND operation. OR operation is performed through ORL instruction. XOR operation is done with XRL instruction. Reset to zero is done with CLR instruction. Complement is done with CLP. In assembly code rotation of data is also possible. Rotate left, rotate right can be done with accumulator content through RL and RR instructions. RLC and RRC will take care about rotating accumulator data with carry. SWAP will swap lower and upper nibbles of accumulator data. The execution time for all these instructions are listed in Table.2.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nExample for Logical AND operation :\r\n\r\n&nbsp;\r\n\r\nAND opcode will logically AND the content of the two operands. Here the operands are at registers A and R0.\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">Assume A= 10011101(9DH),<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>ANL A,R1\u00a0 which gives (A) \u2190 (A) <\/strong>\u2227 (R0)\r\n\r\nR0=11101110 (EEH)\r\n\r\nA=10001100 (8CH)\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">AND operation is done bitwise i.e if the first bit of A is 1 and first bit of R0 is 1 then the AND operation of A and R0 is 1. Hence the first bit is 1 which is stored as the result in Accumulator. Thus content of A register is 8CH.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Clearing or resetting the content is done by CLR opcode.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 CLR A makes (A) = 0\r\n\r\nComplement is obtained through CPL.\r\n\r\n&nbsp;\r\n\r\nA=10001100\r\n\r\n&nbsp;\r\n\r\n<strong>CPL A ; (A) \u2190 (A\u2019) Which makes A=01100010<\/strong>\r\n\r\n&nbsp;\r\n\r\nFor swapping lower and upper nibble SWAP opcode is used, if A=10001100\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0SWAP A ;\u00a0\u00a0\u00a0\u00a0\u00a0 makes A=11011001=D9H, the LSB and MSB bits are swapped.\r\n\r\n&nbsp;\r\n\r\nInstruction set provides rotation of register content also (Figure 3 &amp; 4).\r\n\r\n&nbsp;\r\n\r\nExamples for rotation:\r\n\r\n&nbsp;\r\n\r\nRotate Accumulator Left - RL A\r\n\r\n&nbsp;\r\n\r\nThe 8 bits of the accumulator are rotated left by one bit, and Bit D7 exits from the MSB and enters into LSB, D0\r\n\r\n&nbsp;\r\n\r\nif A = 1001110\r\n\r\n&nbsp;\r\n\r\nRL A\u00a0\u00a0\u00a0\u00a0\u00a0 makes A= 00111011(3EH),\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-69 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27.png\" alt=\"\" width=\"525\" height=\"187\" \/>\r\n\r\n&nbsp;\r\n\r\nBoolean Operations - Bit-oriented Instructions :\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Similar to logic instructions, bit-oriented instructions perform logic operations (Table.3). The difference is that these are performed upon single bits. All bit accesses are by direct addressing. Bit addresses 00H through 7FH are in the Lower 128, and bit addresses 80H through FFH are in SFR (special function register )space.<\/p>\r\n&nbsp;\r\n\r\nExamples\r\n\r\nANL C,bit ; Logical-AND for bit variables\r\n\r\n<strong>(C)\u00a0 <\/strong><strong>\u2190 (C) <\/strong>\u2227 (bit)\r\n\r\nANL C,ACC.7 ;and carry with accum.bit 7\r\n\r\nMOV C,P1.0 ;load carry with input pin state\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-70 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28.png\" alt=\"\" width=\"513\" height=\"290\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">SETB instruction is used for setting the value of 1. The JC rel instruction is the jump carry instruction, rel refers to the relative address. JNC is jump on No carry to a particular address. JB instruction refers to jump on bit. JNB instruction is jump if bit is equal to zero. JBC instruction is jump if bit is equal to one and clears that bit.<\/p>\r\n&nbsp;\r\n\r\n<strong><em>1.1.3 Branch Instructions<\/em><\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are two kinds of branch instructions:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Unconditional jump instructions: upon their execution a jump to a new location from where the program continues execution is performed.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Conditional jump instructions: a jump to a new program location is executed only if a specified condition is met. Otherwise, the program normally proceeds with the next instruction. Table .4 gives the list of control jump instructions<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nIn unconditional branch instruction there are three types of jump instructions, namely :\r\n\r\n&nbsp;\r\n\r\nI.\u00a0 Short jump\r\n\r\n&nbsp;\r\n\r\nII. Absolute jump\r\n\r\n&nbsp;\r\n\r\nIII. Long jump.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nShort jump:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The SJMP instruction encodes the destination address as relative offset. The instruction is 2 bytes long, consisting of the opcode and the relative offset byte. The jump distance is limited to a range of -128 to + 127 bytes relative to the instruction following the SJMP.<\/p>\r\n&nbsp;\r\n\r\nAbsolute jump :\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The AJMP instruction encodes the destination address as an 11-bit constant. The instruction is 2 bytes long, consisting of the opcode, which itself contains 3 of the 11 address bits, followed by\u00a0<span style=\"font-size: 1em;text-align: initial\">another byte containing the low 8 bits of the destination address. When the instruction is executed, these 11 bits are simply substituted for the low 11 bits in the PC. The high 5 bits stay the same.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nHence the destination has to be within the same 2K block as the instruction following the AJMP.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-71 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29.png\" alt=\"\" width=\"502\" height=\"352\" \/>\r\n\r\n&nbsp;\r\n\r\nLong jump\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The LJMP instruction encodes the destination address as a 16-bit constant. The instruction is 3 bytes long, consisting of the opcode and two address bytes. The destination address can be anywhere in the 64K Program Memory space.<\/p>\r\n&nbsp;\r\n\r\nCALL instructions are used for calling subroutines in 8051 controller.\r\n\r\n&nbsp;\r\n\r\nACALL addr11 (Absolute Call)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">ACALL unconditionally calls a subroutine located at the indicated address. The instruction increments the PC twice to obtain the address of the following instruction, then pushes the 16-bit result onto the stack (low-order byte first) and increments the Stack Pointer twice. The destination address is obtained by successively concatenating the five high-order bits of the incremented PC, opcode bits 7 through 5, and the second byte of the instruction.<\/p>\r\n&nbsp;\r\n\r\nLCALL addr16 (Long call)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">LCALL calls a subroutine located at the indicated address. The instruction adds three to the program counter to generate the address of the next instruction and then pushes the 16-bit result onto the stack (low byte first), incrementing the Stack Pointer by two.The high -order and low-order bytes of the PC are then loaded, respectively, with the second and third bytes of the<\/p>\r\n&nbsp;\r\n\r\nLCALL instruction.The subroutine may therefore begin anywhere in the full 64K byte program memory address space.\r\n\r\n&nbsp;\r\n\r\nConditional Jump :\r\n\r\n<\/div>\r\nDJNZ&lt;byte&gt;,&lt;reladdr&gt;(Decrement and Jump if Not Zero)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">DJNZ 40H,LABEL_1; decrement content of address 40H if it is not zero, then go to LABEL_1 DJNZ R1,TOGGLE ; Decrement content of R1, if it is not zero then go to TOGGLE<\/p>\r\nCJNE &lt;destbyte&gt;,&lt;src-byte&gt;, rel (Compare and Jump if Not Equal)\r\n\r\n&nbsp;\r\n\r\nCJNE compares the magnitudes of the first two operands and branches if their values are not equal\r\n<p style=\"text-align: justify\">The branch destination is computed by adding the signed relative -displacement in the last instruction byte to the PC, after incrementing the PC to the start of the next instruction. The carry flag is set if the unsigned integer value of &lt;dest-byte&gt; is less than the unsigned integer value of &lt;src-byte&gt;; otherwise, the carry is cleared. Neither operand is affected.<\/p>\r\n&nbsp;\r\n\r\nCJNE R7, # 60H, NOT_EQ\r\n\r\n&nbsp;\r\n<ol start=\"3\">\r\n \t<li><strong> Summary<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">In this lecture Instruction set of 8051 is discussed. The Arithmetic and logical operations and the control transfer instructions are discussed with examples.<\/p>\r\n&nbsp;\r\n\r\n<strong>Supporting &amp; Reference Materials<\/strong>\r\n<ol>\r\n \t<li>The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edit Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D.McKinlay.<\/li>\r\n \t<li>www.mikroe.com\/chapters\/view\/66\/chapter-3-the-8051-instruction-set\/<\/li>\r\n<\/ol>\r\n<ol start=\"4\">\r\n \t<li><strong>References<\/strong><\/li>\r\n<\/ol>\r\n<ol>\r\n \t<li>The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D.McKinlay.<\/li>\r\n \t<li>www.mikroe.com\/chapters\/view\/66\/chapter-3-the-8051-instruction-set\/<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this lecture, Instruction set of 8051 microcontroller will be discussed in detail. Arithmetic and logical instructions, and control transfer instructions will be discussed with examples.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.Instruction set<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The process of writing program for the microcontroller mainly consists of giving instructions (commands) in the specific order in which they should be executed in order to carry out a specific task. As digital circuit cannot \u201cunderstand\u201d what for example an instruction \u201cif the push button is pressed- turn the light on\u201d means, then a certain number of simpler and precisely defined orders that decoder can recognize must be used. All commands are known as INSTRUCTION SET. All microcontrollers compatible with the 8051 have a total of 255 instructions.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In these 255 instructions, many instructions are considered to be \u201cdifferent\u201d, even though they perform the same operation, so there are only 111 truly different commands. For example: ADD A,R0, ADD A,R1, &#8230; ADD A,R7 are instructions that perform the same operation (addition of the accumulator and register). Since there are 8 such registers, each instruction is counted separately. Taking into account that all instructions perform only 53 operations (addition, subtraction, copy etc.) and most of them are rarely used in practice, there are actually 20-30 abbreviations to be learned, which is acceptable.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1 Types of instructions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Depending on the operation that they perform, the instructions are divided into several groups:<\/p>\n<p>&nbsp;<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Arithmetic Instructions<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Branch Instructions<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Data Transfer Instructions<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Logic Instructions and<\/p>\n<p>\u25cf\u00a0\u00a0\u00a0\u00a0\u00a0 Bit-oriented Instructions.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first part of each instruction, called MNEMONIC refers to the operation an instruction performs (copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed. For example:<\/p>\n<p>&nbsp;<\/p>\n<p>ADD A, R0 &#8211; ADD mnemonic (A, R0- operands registers)<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong><em>1.1.1 Arithmetic Instructions<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Arithmetic instructions perform addition, subtraction, multiplication, division, increment and decrement operations. After execution, the result is stored in the first operand. For example: <em>ADD<\/em> A,R1 &#8211; The result of addition (A+R1) will be stored in the accumulator<em>.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Table .1 Execution Table for Arithmetic instructions<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-67 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25.png\" alt=\"\" width=\"606\" height=\"358\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25.png 606w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25-300x177.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25-225x133.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-25-350x207.png 350w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From Table.1 it is evident that always one of the operand is stored in accumulator. Addition is done through ADD and ADDC instruction. Addition with carry is needed when doing multiple addition. Subtraction is always done with borrow using SUBB instruction. Increment and decrement by one bit can be done with INC and DEC instructions. The DA A instruction is the decimal adjust used for BCD addition.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The MUL AB instruction multiplies the Accumulator by the data in the B register and puts the 16-bit product into the concatenated B and Accumulator registers. The DIV AB instruction divides the Accumulator by the data in the B register and leaves the 8-bit quotient in the Accumulator, and the 8-bit remainder in the B register.<\/p>\n<p>&nbsp;<\/p>\n<p>Examples for addition and subtraction:<\/p>\n<p>&nbsp;<\/p>\n<p>ADD A,&lt;src-byte&gt;<\/p>\n<p>Assume A=09h and R0=07h<\/p>\n<p><strong>ADD A,R0 ; (A) \u2190 (A) + (R0), A=10h<\/strong><\/p>\n<p><strong>ADDC A,Rn ; (A) \u2190 (A) + (C) + (Rn)<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>In the ADDC instruction of previous example if the carry bit is 1 then 10h+1 is 11h and that is stored in Accumulator.<\/p>\n<p>SUBB A,&lt;src-byte&gt;<\/p>\n<p>Assume A=09h, R0=07h, carry flag=1<\/p>\n<p><strong>SUBB A,R0 ; (A) \u2190 (A) &#8211; (C) &#8211; (R0) , A=1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Examples for multiplication and division :<\/p>\n<p>&nbsp;<\/p>\n<p>Assume A=09h,B=02h<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0<strong>MUL AB ; (A)9 \u2190 (A) X (B)<\/strong><\/p>\n<p>(B)2,\u00a0 A=12h,B=00h<\/p>\n<p>Multiplying A and B gives 18 in decimal and in hexadecimal it is 12h. Here, LSB is stored in A register and MSB is stored in B register. There is no overflow in the above instruction, if there is overflow then those values are stored in B register.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0<strong>DIV AB ; (A)9 \u2190 (A)\/(B)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>(B)2 , A=04h,B=01h<\/p>\n<p>Division of A and B gives quotient as 4 and remainder as 1. A holds the quotient and B holds the remainder.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>1.1.2. Logical Instruction<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the result is stored in the first operand. These instructions perform basic logic operations such as OR,AND,XOR ,complement and rotate operations.<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-68 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26.png\" alt=\"\" width=\"574\" height=\"341\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26.png 574w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-26-350x208.png 350w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">All logical instructions are listed in Table.2. Accumulator holds one of the operand always. ANL performs the logical AND operation. OR operation is performed through ORL instruction. XOR operation is done with XRL instruction. Reset to zero is done with CLR instruction. Complement is done with CLP. In assembly code rotation of data is also possible. Rotate left, rotate right can be done with accumulator content through RL and RR instructions. RLC and RRC will take care about rotating accumulator data with carry. SWAP will swap lower and upper nibbles of accumulator data. The execution time for all these instructions are listed in Table.2.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Example for Logical AND operation :<\/p>\n<p>&nbsp;<\/p>\n<p>AND opcode will logically AND the content of the two operands. Here the operands are at registers A and R0.<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">Assume A= 10011101(9DH),<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>ANL A,R1\u00a0 which gives (A) \u2190 (A) <\/strong>\u2227 (R0)<\/p>\n<p>R0=11101110 (EEH)<\/p>\n<p>A=10001100 (8CH)<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">AND operation is done bitwise i.e if the first bit of A is 1 and first bit of R0 is 1 then the AND operation of A and R0 is 1. Hence the first bit is 1 which is stored as the result in Accumulator. Thus content of A register is 8CH.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Clearing or resetting the content is done by CLR opcode.<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 CLR A makes (A) = 0<\/p>\n<p>Complement is obtained through CPL.<\/p>\n<p>&nbsp;<\/p>\n<p>A=10001100<\/p>\n<p>&nbsp;<\/p>\n<p><strong>CPL A ; (A) \u2190 (A\u2019) Which makes A=01100010<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>For swapping lower and upper nibble SWAP opcode is used, if A=10001100<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0SWAP A ;\u00a0\u00a0\u00a0\u00a0\u00a0 makes A=11011001=D9H, the LSB and MSB bits are swapped.<\/p>\n<p>&nbsp;<\/p>\n<p>Instruction set provides rotation of register content also (Figure 3 &amp; 4).<\/p>\n<p>&nbsp;<\/p>\n<p>Examples for rotation:<\/p>\n<p>&nbsp;<\/p>\n<p>Rotate Accumulator Left &#8211; RL A<\/p>\n<p>&nbsp;<\/p>\n<p>The 8 bits of the accumulator are rotated left by one bit, and Bit D7 exits from the MSB and enters into LSB, D0<\/p>\n<p>&nbsp;<\/p>\n<p>if A = 1001110<\/p>\n<p>&nbsp;<\/p>\n<p>RL A\u00a0\u00a0\u00a0\u00a0\u00a0 makes A= 00111011(3EH),<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-69 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27.png\" alt=\"\" width=\"525\" height=\"187\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27.png 525w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27-300x107.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27-225x80.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-27-350x125.png 350w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Boolean Operations &#8211; Bit-oriented Instructions :<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Similar to logic instructions, bit-oriented instructions perform logic operations (Table.3). The difference is that these are performed upon single bits. All bit accesses are by direct addressing. Bit addresses 00H through 7FH are in the Lower 128, and bit addresses 80H through FFH are in SFR (special function register )space.<\/p>\n<p>&nbsp;<\/p>\n<p>Examples<\/p>\n<p>ANL C,bit ; Logical-AND for bit variables<\/p>\n<p><strong>(C)\u00a0 <\/strong><strong>\u2190 (C) <\/strong>\u2227 (bit)<\/p>\n<p>ANL C,ACC.7 ;and carry with accum.bit 7<\/p>\n<p>MOV C,P1.0 ;load carry with input pin state<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-70 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28.png\" alt=\"\" width=\"513\" height=\"290\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28.png 513w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28-225x127.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-28-350x198.png 350w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">SETB instruction is used for setting the value of 1. The JC rel instruction is the jump carry instruction, rel refers to the relative address. JNC is jump on No carry to a particular address. JB instruction refers to jump on bit. JNB instruction is jump if bit is equal to zero. JBC instruction is jump if bit is equal to one and clears that bit.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>1.1.3 Branch Instructions<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are two kinds of branch instructions:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Unconditional jump instructions: upon their execution a jump to a new location from where the program continues execution is performed.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Conditional jump instructions: a jump to a new program location is executed only if a specified condition is met. Otherwise, the program normally proceeds with the next instruction. Table .4 gives the list of control jump instructions<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In unconditional branch instruction there are three types of jump instructions, namely :<\/p>\n<p>&nbsp;<\/p>\n<p>I.\u00a0 Short jump<\/p>\n<p>&nbsp;<\/p>\n<p>II. Absolute jump<\/p>\n<p>&nbsp;<\/p>\n<p>III. Long jump.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Short jump:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The SJMP instruction encodes the destination address as relative offset. The instruction is 2 bytes long, consisting of the opcode and the relative offset byte. The jump distance is limited to a range of -128 to + 127 bytes relative to the instruction following the SJMP.<\/p>\n<p>&nbsp;<\/p>\n<p>Absolute jump :<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The AJMP instruction encodes the destination address as an 11-bit constant. The instruction is 2 bytes long, consisting of the opcode, which itself contains 3 of the 11 address bits, followed by\u00a0<span style=\"font-size: 1em;text-align: initial\">another byte containing the low 8 bits of the destination address. When the instruction is executed, these 11 bits are simply substituted for the low 11 bits in the PC. The high 5 bits stay the same.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Hence the destination has to be within the same 2K block as the instruction following the AJMP.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-71 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29.png\" alt=\"\" width=\"502\" height=\"352\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29.png 502w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29-300x210.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29-65x46.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29-225x158.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-29-350x245.png 350w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Long jump<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The LJMP instruction encodes the destination address as a 16-bit constant. The instruction is 3 bytes long, consisting of the opcode and two address bytes. The destination address can be anywhere in the 64K Program Memory space.<\/p>\n<p>&nbsp;<\/p>\n<p>CALL instructions are used for calling subroutines in 8051 controller.<\/p>\n<p>&nbsp;<\/p>\n<p>ACALL addr11 (Absolute Call)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">ACALL unconditionally calls a subroutine located at the indicated address. The instruction increments the PC twice to obtain the address of the following instruction, then pushes the 16-bit result onto the stack (low-order byte first) and increments the Stack Pointer twice. The destination address is obtained by successively concatenating the five high-order bits of the incremented PC, opcode bits 7 through 5, and the second byte of the instruction.<\/p>\n<p>&nbsp;<\/p>\n<p>LCALL addr16 (Long call)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">LCALL calls a subroutine located at the indicated address. The instruction adds three to the program counter to generate the address of the next instruction and then pushes the 16-bit result onto the stack (low byte first), incrementing the Stack Pointer by two.The high -order and low-order bytes of the PC are then loaded, respectively, with the second and third bytes of the<\/p>\n<p>&nbsp;<\/p>\n<p>LCALL instruction.The subroutine may therefore begin anywhere in the full 64K byte program memory address space.<\/p>\n<p>&nbsp;<\/p>\n<p>Conditional Jump :<\/p>\n<\/div>\n<p>DJNZ&lt;byte&gt;,&lt;reladdr&gt;(Decrement and Jump if Not Zero)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">DJNZ 40H,LABEL_1; decrement content of address 40H if it is not zero, then go to LABEL_1 DJNZ R1,TOGGLE ; Decrement content of R1, if it is not zero then go to TOGGLE<\/p>\n<p>CJNE &lt;destbyte&gt;,&lt;src-byte&gt;, rel (Compare and Jump if Not Equal)<\/p>\n<p>&nbsp;<\/p>\n<p>CJNE compares the magnitudes of the first two operands and branches if their values are not equal<\/p>\n<p style=\"text-align: justify\">The branch destination is computed by adding the signed relative -displacement in the last instruction byte to the PC, after incrementing the PC to the start of the next instruction. The carry flag is set if the unsigned integer value of &lt;dest-byte&gt; is less than the unsigned integer value of &lt;src-byte&gt;; otherwise, the carry is cleared. Neither operand is affected.<\/p>\n<p>&nbsp;<\/p>\n<p>CJNE R7, # 60H, NOT_EQ<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"3\">\n<li><strong> Summary<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">In this lecture Instruction set of 8051 is discussed. The Arithmetic and logical operations and the control transfer instructions are discussed with examples.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Supporting &amp; Reference Materials<\/strong><\/p>\n<ol>\n<li>The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edit Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D.McKinlay.<\/li>\n<li>www.mikroe.com\/chapters\/view\/66\/chapter-3-the-8051-instruction-set\/<\/li>\n<\/ol>\n<ol start=\"4\">\n<li><strong>References<\/strong><\/li>\n<\/ol>\n<ol>\n<li>The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D.McKinlay.<\/li>\n<li>www.mikroe.com\/chapters\/view\/66\/chapter-3-the-8051-instruction-set\/<\/li>\n<\/ol>\n","protected":false},"author":2,"menu_order":6,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-66","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":1,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/66\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/66\/revisions\/72"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/66\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapter-type?post=66"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/contributor?post=66"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/license?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}