Sunteți pe pagina 1din 14

8051 Arithmetic and logic instructions

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

MESSAGE NEXT A, P1, #Y A

CLR MOV ADD MOV MOV

C A, A, R0, A, #45H #0ECH A #02H #0FCH A

ADDC A, MOV R1,

Finally we get the result as R0=31H and R1=FFH.

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.

Multiplication and Division of numbers


In multiplying or dividing two numbers in 8051,the use of registers A and B is required since the multiplication and division instructions work only with these two registers. Syntax: MUL and, DIV AB ; divide A by B AB ;A X B ,place 16 bit result in B and A

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).

Logic and Compare Instructions


AND
ANL destination, source ;dest= dest AND source This instruction will perform a logical AND on the two operands and place the result in the destination. Example: Show the result of the following: MOV ANL A, A, #35H #0FH ;A=A AND 0FH (now A=05)

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.

CPL A(complement accumulator)


This instruction complements the contents of register A. The complement action changes the 0s to 1s and the 1s to 0s. Notice that in complementing a byte ,the data must be in register A. Although the CPL instruction cannot be used to complement R0-R7, it does work on P0-P3 ports.

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.

Rotating the bits of A right or left


RR A ; rotate right 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.

Rotating through the carry


There are two more rotate instructions in the 8051. They involve the carry flag. RRC A ;rotate right through carry

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

Loop and jump instructions


Looping in the 8051
Repeating a sequence of instructions a certain number of times is called a loop. In the 8051 the loop action is performed by the instruction DJNZ . In this instruction ,the register is decremented ; if it is not zero ,it jumps to the target address referred to by the label. Example: Multiply 25 by 10 using the technique of repeated addition. Solution: MOV MOV AGAIN: ADD DJNZ MOV A, R2, A, R2, R5, #0 #10 #25 AGAIN A ;A=0,clear ACC ;the multiplier is placed in R2 ;add the multiplicand to the ACC ;repeat until R2=0(10 times) ;save A in R5; R5=FAH

Conditional jumps for the 8051 are summarized as below:


Instruction JZ JNZ DJNZ CJNE A, data JC JNC JB JNB JBC Action Jump if A=0 Jump if A is not equal to 0 Decrement and jump if register is not equal to 0 Jump if A is not equal to data Jump if CY=1 Jump if CY=0 Jump if bit =1 Jump if bit =0 Jump if bit=1 and clear bit

Unconditional jump instructions


In the 8051 there are two unconditional jumps: LJMP and SJMP. LJMP(long jump) LJMP is an unconditional long jump. The 2-byte target address allows a jump to any memory location from 0000 to FFFFH. SJMP (short jump) In this 2-byte instruction ,the first byte is the opcode and the second byte is the relative address of the target location. The target address allows a jump to any memory location from 00 to FFH.

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.

Some basic commands needed to initialize a LCD:


CODE 38 0E 01 06 80 C0 DESCRIPTION to initialize LCD in 2 lines and 5x7 matrix cursor blinking clear display screen increment cursor (shift cursor to right while writing) force cursor to beginning of 1st line force cursor to beginning of 2nd line

Sending commands and data to LCD with a time delay


P3 port of the controller is connected to LCD data pins D0-D7. P2.0 is connected to RS pin of LCD. P2.1 is connected to R/W pin of LCD. P2.2 is connected to E pin of LCD. Task1: Write a program to display the word RAD on the LCD. $ INCLUDE(MOD51) ORG MOV 0000H A, #38H

LCALL COMMAND

MOV

A,

#0EH

LCALL COMMAND MOV A, #01H

LCALL COMMAND MOV A, #06H

LCALL COMMAND MOV A, #80H

LCALL COMMAND MOV A, #R

LCALL DATAWRITE MOV A, #A

LCALL DATAWRITE MOV A, #D

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 MOV A, #0EH

LCALL COMMAND MOV A, #01H

LCALL COMMAND MOV A, #06H

LCALL COMMAND MOV A, #80H

LCALL COMMAND MOV A, #R

LCALL DATAWRITE MOV A, #A

LCALL DATAWRITE MOV A, #D

LCALL DATAWRITE LOOP: MOV A, #1CH

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

LCALL COMMAND MOV A, #0EH

LCALL COMMAND MOV A, #01H

LCALL COMMAND MOV A, #06H

LCALL COMMAND MOV A, #80H

LCALL COMMAND MOV A, #R

LCALL DATAWRITE MOV A, #A

LCALL DATAWRITE MOV A, #D

LCALL DATAWRITE MOV A, #C2

LCALL COMMAND MOV A, #I

LCALL DATAWRITE MOV A, #N

LCALL DATAWRITE MOV A, #N

LCALL DATAWRITE MOV A, #O

LCALL DATAWRITE MOV A, #V

LCALL DATAWRITE MOV A, #A

LCALL DATAWRITE MOV A, #T

LCALL DATAWRITE MOV A, #I

LCALL DATAWRITE MOV A, #O

LCALL DATAWRITE MOV A, #N

LCALL DATAWRITE MOV A, #S

LCALL DATAWRITE HANG: LJMP HANG

S-ar putea să vă placă și