Documente Academic
Documente Profesional
Documente Cultură
Arithmetic instructions:
Addition of numbers:
In the 8051, in order to add numbers together,the accumulator register (A) must be involved. The form of the ADD instruction is ADD A, source ;A=A+ source
The destination operand is always in register A while the source operand can be a register,immediate data, or in memory. Memory to memory arithmetic operations are never allowed in 8051. ADDITION OF INDIVIDUAL BYTES To calculate the sum of any number of operands ,the carry flag should be checked after the addition of each operand. Example: Two numbers are stored in registers R0 and R1.Verify if their sum is greater than FFH. Solution: MOV ADD JC SJMP MESSAGE: MOV MOV NEXT: NOP END ADDC and addition of 16-bit numbers When adding two 16-bit data operands,we need to be concerned with the propagation of a carry from the lower byte to the higher byte. The instruction ADDC(add with carry) is used on such occasions. Example: Write a program to add two 16-bit numbers- FC45H and 02ECH Solution: A, A, R0 R1
Subtraction of numbers
SUBB A, source ;A=A source- CY In many microprocessors there are two different instructions for subtraction:SUB and SUBB(subtract with borrow). In the 8051 we have only SUBB. To make SUB out of SUBB ,we have to make CY=0 prior to the execution of the instruction. Therefore there are two cases for the SUBB instruction: CY=0 and CY=1 (CY is a falg used for the borrow). SUBB instruction will take care of the borrow of the lower operand. If CY=1 prior to executing the SUBB instruction ,it also subtracts 1 from the result. Example: Analyze the following program: CLR MOV SUBB MOV MOV SUBB MOV C A, A, R7, A, A, R6, #62H #96H A #27H #12H A ;CY=0 ;A=62H ;62H - 96H = CCH with CY=1 ;save the result ;A=27H ;27H - 12H 1 = 14H ;save the result
Solution: After the SUBB ,A= 62H-96H=CCH and the carry flag is set high indicating there is a borrow.Since CY=1 when SUBB is executed the second time A=27H-12H-1=14H.Therefore we have 2762H 1296H = 14CCH.
In byte by byte multiplication or division ,one of the operands must be in register A,and the second operand must be in register B. After multiplication the result is in A and B registers; the lower byte in A, and the upper byte in B. Similarly, after the division operation is performed ,the quotient is in A and the remainder is in B. Notice the following points for instruction DIV AB 1. This instruction always makes CY=0 and OV=0 if the denominator is not 0. 2. If the denominator is 0,OV =1 indicates an error. This indicates the invalid result of infinity. Example: MOV MOV DIV A, B, AB #95 #10 ; now A= 09(quotient) and B= 05(remainder).
Solution: 35H 0FH 05H 0011 0101 0000 1111 0000 0101 35H AND 0FH= 05H
OR
ORL destination, source ;dest=dest OR source
The destination and source operands are Ored, and the result is placed in the destination. The destination is normally the accumulator.
XOR
XRL destination, source ;dest=dest XOR source This instruction will perform the XOR operation on the two operands and place the result in the destination. The destination is normally the accumulator. XRL can also be used to see if the two registers have the same value. XRL A, R1 will exclusive-or register A and register R1, and put the result in A. If both the registers have the same value , 00 is placed in A. Then we can use the JZ instruction to make a decision based on the result.
Compare instruction
The 8051 has an instruction for the compare operation. It has the following syntax: CJNE destination, source, relative address
In the 8051, the actions of comparing and jumping are combined into a single instruction called CJNE(compare and jump if not equal). The CJNE instruction compares two operands, and jumps if they are not equal.The destination operand can be in the accumulator or in one of the Rn registers.
Rotate instruction
In many applications there is a need to perform a bitwise rotation of an operand. In the 8051 the rotation instructions RR, RL, RLC, and RRC are designed specifically for that purpose.To rotate a byte the operand must be in register A.
In rotate right ,the 8 bits of the accumulator are rotated right one bit . MOV RR A, A #36H ;A=0011 0110 ;A=0001 1011
Similarly , in rotate left , the 8 bits of the accumulator are rotated left one bit. Notice in the RR and RL instructions that no flags are affected.
In RRC A,as bits are rotated from left to right, they exit the LSB to the carry flag, and the carry flag enters the MSB. The carry flag acts as if it is part of register A. CLR MOV RRC RRC RRC C A, A A A #26H ;make CY=0 ;A=0010 0110 ;A=0001 0011 CY=0 ;A=0000 1001 CY=1 ;A=1000 0100 CY=1
LCD INTERFACING
In the recent years the LCD is finding widespread use. The LCD discussed here has 16 pins. The function of each pin is given below. Pin 1 2 3 4 Symbol GND Vcc Vee RS Description Ground +5V Power supply power supply to control contrast RS=0 to select command register RS=1 to select data register 5 6 7 8 9 10 11 12 13 14 15 16 R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 Vcc GND R/W=0 for write, R/W=1 for read Enable Data line Data line Data line Data line Data line Data line Data line Data line +5V power supply Ground
RS register select If RS=0, the instruction command code register is selected, allowing the user to send a command. If RS=1 the data register is selected ,allowing the user to send data to be displayed on the LCD.
R/W, READ/WRITE R/W allows the user to write information to the LCD or read information from it. R/W=1 when reading, R/W=0 when writing. E, enable The enable pin is used by the LCD to latch information presented to its data pins. When data is supplied to its data pins, a high to low pulse must be applied to this pin in order for the LCD to latch in the data present at the data pins. D0-D7 The 8-bit data pins are used to send information to the LCD or read the contents of the LCD s internal registers.
LCALL COMMAND
MOV
A,
#0EH
LCALL DATAWRITE COMMAND: MOV CLR CLR SETB P3, P2.0 P2.1 P2.2 A
LCALL DELAY CLR RET DATAWRITE: MOV SETB CLR SETB P3, P2.0 P2.1 P2.2 A P2.2
LCALL DELAY CLR RET DELAY: BACK1: BACK: MOV MOV DJNZ DJNZ RET END TASK2: Write a program to display the word RAD moving left to right on the LCD. $ INCLUDE(MOD51) ORG MOV 0000H A, #38H R0, R1, R1, R0, #40H #0FFH BACK BACK1 P2.2
LCALL COMMAND LCALL DELAY LJMP COMMAND: MOV CLR CLR SETB LOOP P3, P2.0 P2.1 P2.2 A
LCALL DELAY CLR RET DATAWRITE: MOV SETB CLR SETB P3, P2.0 P2.1 P2.2 A P2.2
LCALL DELAY CLR RET DELAY: BACK1: BACK: MOV MOV DJNZ DJNZ R0, R1, R1, R0, #40H #0FFH BACK BACK1 P2.2
RET TASK3: Write a program to display the following on the LCD: RAD INNOVATION SOL: $ INCLUDE(MOD51) ORG MOV 0000H A, #38H