Transcript of "Arithmetic & logical operations in 8051"
1.
Subject Code :151001Name Of Subject :Microcontroller & InterfacingName of Unit :Arithmetic& logical instructionsTopic :Arithmetic & logical operationsName of Faculty : Mr. Haresh Suthar Miss. Madhuri ThakkarName of Students : (i) Savalia Avani(100870111020) (ii) Patel Jay (100870111021)
2.
Arithmetic Instruction : There are 24 arithmetic opcodes which are grouped into the following types: ADD and ADDC SUBB MUL DIV INC DEC DASub: MC Topic: Arithmetic & Logical operations
3.
Arithmetic Flags Flag: It is a 1-bit register that indicates the status of the result from an operation Flags are either at a flag-state of value 0 or 1 Arithmetic flags indicate the status of the results from mathematical operations ( +, −, *, / )Sub: MC Topic: Arithmetic & Logical operations
4.
Arithmetic Flags (Conditional Flags) There are 4 arithmetic flags in the 8051 Carry (C) Auxiliary Carry (AC) Overflow (OV) Parity (P) All the above flags are stored in the Program Status Word (PSW) CY AC -- RS1 RS0 0V -- P PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0Sub: MC Topic: Arithmetic & Logical operations
5.
Arithmetic Flags (Conditional Flags) CY PSW.7 Carry flag AC PSW.6 Auxiliary carry flag -- PSW.5 Available to the user for general purpose RS1 PSW.4 Register Bank selector bit 1 RS0 PSW.3 Register Bank selector bit 0 0V PSW.2 Overflow flag -- PSW.1 User definable flag P PSW.0 Parity flag The C flag is keeping track in unsigned operations The OV flag is keeping track in signed operationsSub: MC Topic: Arithmetic & Logical operations
6.
Instructions that Affecting Flags (1/2) Instruction Mnemonic Flags Affected ADD C AC OV ADDC C AC OV SUBB C AC OV MUL C=0 OV DIV C=0 OV DA A C SETB C C=1 MOV C, bit CSub: MC Topic: Arithmetic & Logical operations
7.
Instructions that Affecting Flags (2/2) Instruction Mnemonic Flags Affected ORL C, bit C ANL C, bit C RLC C RRC C CLR C C=0 CPL C C = /C CJNE CSub: MC Topic: Arithmetic & Logical operations
8.
The ADD and ADDC Instructions ADD A, source ; A = A + source ADDC A, source ; A = A + source + C A register must be involved in additions The C flag is set to 1 if there is a carry out of bit 7 The AC flag is set to 1 if there is a carry out of bit 3 ADD is used for ordinary addition ADDC is used to add a carry after the LSB addition in a multi-byte processSub: MC Topic: Arithmetic & Logical operations
9.
Example -1 Show how the flag register is affected by the following instructions. MOV A, #0F5h ; A = F5h ADD A, #0Bh ; A = F5 + 0B = 00 Solution F5h 1111 0101 + 0Bh + 0000 1011 100h 0000 0000 After the addition, register A (destination) contains 00 and the flags are as follows: CY = 1 since there is a carry out from D7 P = 0 because the number of 1s is zero AC = 1 since there is a carry from D3 to D4Sub: MC Topic: Arithmetic & Logical operations
10.
Example -2 Assume that RAM locations 40h – 42h have the following values. Write a program to find the sum of the values in these locations. At the end of the program, register A should contain the low byte and R7 contain the high byte. RAM locations: 40h = (7Dh), 41h = (EBh), 42h = (C5h) Solution: MOV A, 40h ; set A = RAM location 40h MOV R7, #0 ; set R7 = 0 ADD A, 41h ; add A with RAM location 41h JNC NEXT ; if CY = 0 don’t accumulate carry INC R7 ; keep track of carry NEXT: ADD A, 42h ; add A with RAM location 42h JNC NEXT1 ; if CY = 0 don’t accumulate carry INC R7 ; keep track of carry NEXT1: ENDSub: MC Topic: Arithmetic & Logical operations
11.
Example -3 Write a program segment to add two 16-bit numbers. The numbers are 3CE7h and 3B8Dh. Place the sum in R7 and R6; R6 should store the lower byte. CLR C ; make C=0 MOV A, #0E7h ; load the low byte now A=E7h ADD A, #8Dh ; add the low byte now A=74h and C=1 MOV R6, A; save the low byte of the sum in R6 MOV A, #3Ch ; load the high byte ADDC A, #3Bh ; add with the carry ; 3B + 3C + 1 = 78 (all in hex) MOV R7, A; save the high byte of the sumSub: MC Topic: Arithmetic & Logical operations
12.
ADDC ….. ADD …..The DA Instruction DA A DA A DA A The action is to “decimal adjust” the register A Used after the addition of two BCD numbers Example 4 : MOV A, #47h ; A=47h first BCD operand MOV B, #25h ; B=25h second BCD operand ADD A, B ; hex (binary) addition (A=6Ch) DA A ; adjust for BCD addition (A=72h)Sub: MC Topic: Arithmetic & Logical operations
13.
Example 4 of DA Instruction Hex BCD 47 0100 0111 + 25 + 0010 0101 6C 0110 1100 + 6 + 0110 72 0111 0010 Offset decimal 6 !Sub: MC Topic: Arithmetic & Logical operations
14.
SUBB A, #data The SUBB Instruction SUBB A, direct SUBB A, @Ri , where i =0 or 1 SUBB A, source SUBB A, Rn, where n =0,1,,7 No borrow: A = A – source With borrow: A = A – source – carry (i.e. borrow) Note that the 8051 uses the 2’s complement method to do subtraction After execution: The C flag is set to 1 if a borrow is needed into bit 7 The AC flag is set to 1 if a borrow is needed into bit 3Sub: MC Topic: Arithmetic & Logical operations
15.
The MUL Instruction MUL AB Uses registers A and B as both source and destination registers Numbers in A and B are multiplied, then put the lower-order byte of the product in A and the high- order byte in B The OV flag is set to 1 if the product > FFh Note that the C flag is 0 at all timesSub: MC Topic: Arithmetic & Logical operations
16.
The DIV Instruction DIV AB Similarly, it uses registers A and B as both source and destination registers The number in A is divided by B. The quotient is put in A and the remainder (if any) is put in B The OV flag is set to 1 if B has the number 00h (divide-by-zero error) Note that the C flag is 0 at all timesSub: MC Topic: Arithmetic & Logical operations
17.
The INC and DEC Instructions To increment (INC) or decrement (DEC) the internal memory location specified by the operand No change with all the arithmetic flags in this operation e.g. INC 7Fh ; content in 7Fh increased by 1 DEC R1 ; content in R1 decreased by 1 INC A INC direct INC @Ri where i=0,or 1 INC Rn where n=0,,7Sub: MC Topic: Arithmetic & Logical operations
18.
Logic Operation in 8051 Logical operations Rotate and swap operations Comparison operationsSub: MC Topic: Arithmetic & Logical operations
19.
Logical Instructions The source operand can be any of the 4 addressing modes (i.e. immediate/register/ direct/indirect) ANL can be used to clear (0) certain bits ORL can be used to set (1) certain bits Examples Instruction ANL A,R0 ORL A,R0 XRL A,R0 A before: 10010111 10010111 10010111 R0 before: 11110010 11110010 11110010 A afterwards: 10010010 11110111 01100101Sub: MC Topic: Arithmetic & Logical operations
20.
The CLR and CPL Instructions CLR A All bits in register A are cleared CPLA All bits in register A are complemented (inverted) Note that CLR and CPL instructions operate on register A onlySub: MC Topic: Arithmetic & Logical operations
21.
RL A The Rotate Instructions RR A Contents in register A is rotated one bit position to the left or to the right (operated in A only) The bit shifted out is used as the new bit shifted in May include the C flag in the operation Useful in inspecting the bits in a byte one by one Also useful for multiplication and division in powers of 2Sub: MC Topic: Arithmetic & Logical operations
22.
The Rotate Instructions RL A Rotates A one bit position to the left RLC A Rotates A and the carry flag one bit position to the left RR A Rotates A one bit position to the right RRC A Rotates A and the carry flag one bit position to the right Note that for RLC and RRC, you have to know the C flag firstSub: MC Topic: Arithmetic & Logical operations
23.
The Rotate Instructions 7 6 5 4 3 2 1 0 Before: 10011100 After: 00111001 RL A C 7 6 5 4 3 2 1 0 Before: 10011100 CY = 0 After: 00111000 CY = 1 Carry Flag RLC A 7 6 5 4 3 2 1 0 Before: 10011100 After: 01001110 RR A 7 6 5 4 3 2 1 0 C Before: 10011100 CY = 1 After: 11001110 CY = 0 RRC A Carry FlagSub: MC Topic: Arithmetic & Logical operations
24.
The SWAP Instruction Swapping the lower-nibble (lower 4 bits) and the higher-nibble (upper 4 bits) of register A. 7 6 5 4 3 2 1 0 High Nibble Low Nibble SWAP A Register A = 5Eh (original value) after SWAP Register A = E5hSub: MC Topic: Arithmetic & Logical operations
25.
Comparison Operation CJNE destination, source, relative address Compare the source and destination operands first Jump to the relative address (subroutine) if they are not equal Carry flag = 1, if destination-byte is less than the source-byte, Otherwise, the carry flag is cleared.Sub: MC Topic: Arithmetic & Logical operations
26.
Example -5 Write a program segment to monitor P1 continuously for the value of 63h. It should get out of the monitoring only if P1 = 63h. Solution : MOV P1, #0FFh ; make P1 an input port HERE: MOV A, P1 ; get P1 CJNE A, #63h, HERE ; keep monitoring unless ; P1=63hSub: MC Topic: Arithmetic & Logical operations
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.