Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ass. Prof. Dr Masri Ayob Lecture 5: Arithmetic and Logic Instructions TK 2633: Microprocessor & Interfacing.

Similar presentations


Presentation on theme: "Ass. Prof. Dr Masri Ayob Lecture 5: Arithmetic and Logic Instructions TK 2633: Microprocessor & Interfacing."— Presentation transcript:

1 Ass. Prof. Dr Masri Ayob Lecture 5: Arithmetic and Logic Instructions TK 2633: Microprocessor & Interfacing

2 OBJECTIVES Prepared by: Dr Masri Ayob - TK2123 2 Explain the operation of the 8085 arithmetic and logic instructions and describe their effect on the flag bits. Explain the operation of the 8085 arithmetic and logic instructions and describe their effect on the flag bits. Describe how DAA adjusts the result of a BCD addition. Describe how DAA adjusts the result of a BCD addition. Write short programs using the arithmetic and logic operations. Write short programs using the arithmetic and logic operations. Use the logic instructions to clear, set, and complement bus. Use the logic instructions to clear, set, and complement bus. Test the accumulator, using the ANA A or ORA A instruction. Test the accumulator, using the ANA A or ORA A instruction. Explain the operation of the shift and rotate instructions and their effect on the flag bits. Explain the operation of the shift and rotate instructions and their effect on the flag bits.

3 ARITHMETIC OPERATIONS Prepared by: Dr Masri Ayob - TK2123 3 Addition, subtraction, increment, and decrement.

4 ARITHMETIC OPERATIONS Prepared by: Dr Masri Ayob - TK2123 4

5 ARITHMETIC OPERATIONS Prepared by: Dr Masri Ayob - TK2123 5

6 ARITHMETIC OPERATIONS Prepared by: Dr Masri Ayob - TK2123 6

7 ARITHMETIC OPERATIONS Prepared by: Dr Masri Ayob - TK2123 7

8 ADDITION Prepared by: Dr Masri Ayob - TK2123 8 Addition takes several forms in the 8085 microprocessor: Addition takes several forms in the 8085 microprocessor: 8-bit binary, 8-bit binary, 16-bit binary, 16-bit binary, and two-digit binary-coded-decimal (BCE)) addition. and two-digit binary-coded-decimal (BCE)) addition. Binary addition functions with either signed or unsigned numbers; Binary addition functions with either signed or unsigned numbers; BCD addition uses only unsigned numbers. BCD addition uses only unsigned numbers. The instruction set supports additions using register addressing, register indirect addressing, and immediate addressing, but not direct addressing. The instruction set supports additions using register addressing, register indirect addressing, and immediate addressing, but not direct addressing.

9 ADDITION Prepared by: Dr Masri Ayob - TK2123 9

10 ADDITION Prepared by: Dr Masri Ayob - TK2123 10

11 ADDITION Prepared by: Dr Masri Ayob - TK2123 11

12 Addition with Carry Prepared by: Dr Masri Ayob - TK2123 12 Whenever large numbers (numbers wider than 8 bits, or multiple-byte numbers) are added, the carry must be propagated from one 8-bit segment to the next. Whenever large numbers (numbers wider than 8 bits, or multiple-byte numbers) are added, the carry must be propagated from one 8-bit segment to the next.

13 Addition with Carry Prepared by: Dr Masri Ayob - TK2123 13

14 Example Prepared by: Dr Masri Ayob - TK2123 14 Suppose that the DE register pair contains a 16-bit number that we must add to the number in the BC register pair. To accomplish this multiple-byte addition, add F and C together and then add D and B together with the carry. The add-with-carry instruction uses the carry from the addition of E and C to generate the correct answer when D and B are added. Suppose that the DE register pair contains a 16-bit number that we must add to the number in the BC register pair. To accomplish this multiple-byte addition, add F and C together and then add D and B together with the carry. The add-with-carry instruction uses the carry from the addition of E and C to generate the correct answer when D and B are added.

15 Example Prepared by: Dr Masri Ayob - TK2123 15

16 Sixteen-Bit Addition Prepared by: Dr Masri Ayob - TK2123 16 The 8085 instruction set does contain special instructions (DAD) that do 16-bit addition.

17 BCD Addition Prepared by: Dr Masri Ayob - TK2123 17 BCD addition is like binary addition except that the numbers range in value only from 0 through 9. BCD addition is like binary addition except that the numbers range in value only from 0 through 9. A special instruction allows BCD addition by using the standard binary addition (ADD) instructions. A special instruction allows BCD addition by using the standard binary addition (ADD) instructions. The DAA instruction appears after a BCD addition (with a binary add instruction) to correct the BCD result. The DAA instruction appears after a BCD addition (with a binary add instruction) to correct the BCD result. The DAA instruction does not convert a binary number to a BCD number. The DAA instruction does not convert a binary number to a BCD number.

18 Example Prepared by: Dr Masri Ayob - TK2123 18 Example 5-6 illustrates the summation of the packed BCD numbers 11 and 19. Example 5-6 illustrates the summation of the packed BCD numbers 11 and 19. After this addition, the accumulator contains a 2AH, which is not a BCD number: After this addition, the accumulator contains a 2AH, which is not a BCD number: the answer should be a 30 BCD the answer should be a 30 BCD The DAA instruction corrects the answer after the addition and provides a 30 BCD after the DAA instruction executes. The DAA instruction corrects the answer after the addition and provides a 30 BCD after the DAA instruction executes.

19 BCD Addition Prepared by: Dr Masri Ayob - TK2123 19 The DAA instruction changes the result through the two tests listed by adding a OOH, 06H, 60H, or 66H to the accumulator. The DAA instruction changes the result through the two tests listed by adding a OOH, 06H, 60H, or 66H to the accumulator. If the least significant half-byte is greater than 9 or if the AC flag = 1, the DAA instruction adds a 06H to the accumulator. If the least significant half-byte is greater than 9 or if the AC flag = 1, the DAA instruction adds a 06H to the accumulator. If the most significant half-byte is greater than 9 or if the C flag bit 1, the DAA instruction adds a 60H to the accumulator. If the most significant half-byte is greater than 9 or if the C flag bit 1, the DAA instruction adds a 60H to the accumulator.

20 Increment Prepared by: Dr Masri Ayob - TK2123 20 The last form of addition is to increment or add 1. The last form of addition is to increment or add 1. The increment command is either an 8-bit (INR) increment or a 16-bit (INX) Increment instruction. The increment command is either an 8-bit (INR) increment or a 16-bit (INX) Increment instruction. The INR instructions affect all the flags except carry, and the INX instructions affect no flags. The INR instructions affect all the flags except carry, and the INX instructions affect no flags.

21 Prepared by: Dr Masri Ayob - TK2123 21

22 Subtraction Prepared by: Dr Masri Ayob - TK2123 22 The 8085 supports 8-bit binary subtraction and decrement. The 8085 supports 8-bit binary subtraction and decrement. It also supports a subtraction instruction that allows a borrow to be propagated through additional bytes of a number. It also supports a subtraction instruction that allows a borrow to be propagated through additional bytes of a number. The subtract-with-borrow instruction aids in the subtraction of multiple-byte numbers.. The subtract-with-borrow instruction aids in the subtraction of multiple-byte numbers..

23 Subtraction Prepared by: Dr Masri Ayob - TK2123 23 The 8085 performs subtraction by using the method of 2’s complement. The 8085 performs subtraction by using the method of 2’s complement. Subtraction can be performed by using either: Subtraction can be performed by using either: the instruction SUB to subtract contents of a source register or the instruction SUB to subtract contents of a source register or the instruction SUI to subtract an 8-bit number from contents of the accumulator. the instruction SUI to subtract an 8-bit number from contents of the accumulator. In either case, the accumulator contents are regarded as minuend (the number from which to subtract). In either case, the accumulator contents are regarded as minuend (the number from which to subtract).

24 Subtraction Prepared by: Dr Masri Ayob - TK2123 24 Various subtraction instructions: register, register indirect, and immediate addressing. Various subtraction instructions: register, register indirect, and immediate addressing. Direct addressing is not allowed for a subtraction. Direct addressing is not allowed for a subtraction. Each of these instructions affects the flag bits, so they reflect various conditions about the difference after a subtraction. Each of these instructions affects the flag bits, so they reflect various conditions about the difference after a subtraction.

25 Subtraction Prepared by: Dr Masri Ayob - TK2123 25 The 8085 performs the following steps internally to execute the instruction SUB (or SUI): The 8085 performs the following steps internally to execute the instruction SUB (or SUI): Converts subtrahend (the number to be subtracted) into its 1’s complement. Converts subtrahend (the number to be subtracted) into its 1’s complement. Adds 1I to 1’s complement to obtain 2’s complement of the subtrahend. Adds 1I to 1’s complement to obtain 2’s complement of the subtrahend. Add 2’s complement to the minuend (the contents of the accumulator). Add 2’s complement to the minuend (the contents of the accumulator). Complements the Carry flag. Complements the Carry flag.

26 Illustrative Program: Subtraction of Two Numbers Prepared by: Dr Masri Ayob - TK2123 26 PROBLEM STATEMENT PROBLEM STATEMENT Write a program to do the following: Write a program to do the following: Load the number 3011 in register B and 3911 in register C. Load the number 3011 in register B and 3911 in register C. Subtract 39H from 3011. Subtract 39H from 3011. Display the answer at PORT I. Display the answer at PORT I.

27 Illustrative Program: Subtraction of Two Numbers Prepared by: Dr Masri Ayob - TK2123 27

28 Illustrative Program: Subtraction of Two Numbers Prepared by: Dr Masri Ayob - TK2123 28 PROGRAM DESCRIPTION PROGRAM DESCRIPTION Registers B and C are loaded with 30H and 39H, respectively. Registers B and C are loaded with 30H and 39H, respectively. The instruction MOV A,B copies 30H into the accumulator (shown as register contents). The instruction MOV A,B copies 30H into the accumulator (shown as register contents).

29 Illustrative Program: Subtraction of Two Numbers Prepared by: Dr Masri Ayob - TK2123 29 To execute the instruction SUB C the microprocessor performs the following steps internally: To execute the instruction SUB C the microprocessor performs the following steps internally:

30 Illustrative Program: Subtraction of Two Numbers Prepared by: Dr Masri Ayob - TK2123 30 PROGRAM DESCRIPTION PROGRAM DESCRIPTION The number F7H is a 2’s complement of the magnitude (39H- 30H)=09H. The number F7H is a 2’s complement of the magnitude (39H- 30H)=09H. This ‘Cy’ flag is set, indicating the answer is in 2’s complement. This ‘Cy’ flag is set, indicating the answer is in 2’s complement. The instruction OUT displays F7 at PORT1. The instruction OUT displays F7 at PORT1.

31 Subtraction Prepared by: Dr Masri Ayob - TK2123 31 P= 0 Odd parity

32 Subtract with Borrow Prepared by: Dr Masri Ayob - TK2123 32

33 Subtract with Borrow Prepared by: Dr Masri Ayob - TK2123 33

34 Example Prepared by: Dr Masri Ayob - TK2123 34 Suppose that the number in the DE pair must be subtracted from the BC pair. The least significant is operated on first. Once the difference of C and E is found, the D register is subtracted from the B register with a borrow. The subtraction with borrow effectively propagates the borrow through the most significant byte of the result.

35 LOGIC INSTRUCTIONS Prepared by: Dr Masri Ayob - TK2123 35 Four basic logic functions: Four basic logic functions: invert, AND, OR, and exclusive-OR. invert, AND, OR, and exclusive-OR. Why does a microprocessor instruction set contain logic instructions? Why does a microprocessor instruction set contain logic instructions? Logic instructions sometimes replace discrete logic gates. Logic instructions sometimes replace discrete logic gates. Today, program storage costs about of a cent per byte. Today, program storage costs about of a cent per byte. System control software usually requires bit manipulation — a logic operation. System control software usually requires bit manipulation — a logic operation.

36 Inversion (NOT) Prepared by: Dr Masri Ayob - TK2123 36 The CMA instruction (2FH in machine language), one’s-complements or inverts the contents of the accumulator. The CMA instruction (2FH in machine language), one’s-complements or inverts the contents of the accumulator. This operation, which affects none of the flag bits, causes each bit of the accumulator to be inverted (changed from 1 to 0 or 0 to 1). This operation, which affects none of the flag bits, causes each bit of the accumulator to be inverted (changed from 1 to 0 or 0 to 1). The CMA instruction causes the accumulator to appear as eight inverters (or NOT). The CMA instruction causes the accumulator to appear as eight inverters (or NOT). This means that this one-byte instruction replaces eight discrete inverters if the speed required is not too great. The amount of circuitry replaced by the CMA instruction is 14 of a 7404 TTL hex inverter. This means that this one-byte instruction replaces eight discrete inverters if the speed required is not too great. The amount of circuitry replaced by the CMA instruction is 14 of a 7404 TTL hex inverter.

37 The AND Operation Prepared by: Dr Masri Ayob - TK2123 37 The AND instruction, has two separate functions in a microprocessor- based system. The AND instruction, has two separate functions in a microprocessor- based system. The AND instruction selectively clears bits of the accumulator or replaces discrete AND gates. The AND instruction selectively clears bits of the accumulator or replaces discrete AND gates. The AND instruction, functions as eight independent two-input AND gates. This instruction replaces two 7408 quad two-input AND gates. The AND instruction, functions as eight independent two-input AND gates. This instruction replaces two 7408 quad two-input AND gates.

38 The AND Operation Prepared by: Dr Masri Ayob - TK2123 38

39 The AND Operation Prepared by: Dr Masri Ayob - TK2123 39

40 The OR Operation Prepared by: Dr Masri Ayob - TK2123 40 Has two separate functions in a microprocessor-based system: Has two separate functions in a microprocessor-based system: It selectively sets bits of the accumulator or It selectively sets bits of the accumulator or Replaces discrete OR gates. Replaces discrete OR gates. The inclusive-OR instruction functions as eight independent two-input OR gates. The inclusive-OR instruction functions as eight independent two-input OR gates. This instruction replaces two 7432 quad two-input OR gates. This instruction replaces two 7432 quad two-input OR gates.

41 The OR Operation Prepared by: Dr Masri Ayob - TK2123 41

42 The OR Operation Prepared by: Dr Masri Ayob - TK2123 42

43 Prepared by: Dr Masri Ayob - TK2123 43

44 Illustrative Program: ORing Data from Two Input Ports Prepared by: Dr Masri Ayob - TK2123 44 PROBLEM STATEMENT PROBLEM STATEMENT Two input port with eight switches (each port) at address 00H and OIH (Figure 6.9) is connected to the microcomputer to control the same appliances and lights from the bedroom as well as from the kitchen. Two input port with eight switches (each port) at address 00H and OIH (Figure 6.9) is connected to the microcomputer to control the same appliances and lights from the bedroom as well as from the kitchen. Write instructions to turn on the devices from any of the input ports. Write instructions to turn on the devices from any of the input ports.

45 Illustrative Program: ORing Data from Two Input Ports Prepared by: Dr Masri Ayob - TK2123 45

46 Illustrative Program: ORing Data from Two Input Ports Prepared by: Dr Masri Ayob - TK2123 46 PROBLEM ANALYSIS PROBLEM ANALYSIS To turn on the appliances from any one of the input ports, the microprocessor needs to read the switches at both ports and logically OR the switch positions. To turn on the appliances from any one of the input ports, the microprocessor needs to read the switches at both ports and logically OR the switch positions. Assume that the switch positions in one input port (located in the bedroom) correspond to the data byte 9lH and the switch positions in the second port (located in the kitchen) correspond to the data byte A8H. Assume that the switch positions in one input port (located in the bedroom) correspond to the data byte 9lH and the switch positions in the second port (located in the kitchen) correspond to the data byte A8H. The person in the bedroom wants to turn on the air conditioner, the radio, and the bedroom light; and the person in the kitchen wants to turn on the air-conditioner, the coffeepot, and the kitchen light. The person in the bedroom wants to turn on the air conditioner, the radio, and the bedroom light; and the person in the kitchen wants to turn on the air-conditioner, the coffeepot, and the kitchen light. By ORing these two data bytes, the MPU can turn ON the necessary appliances. By ORing these two data bytes, the MPU can turn ON the necessary appliances.

47 Illustrative Program: ORing Data from Two Input Ports Prepared by: Dr Masri Ayob - TK2123 47

48 Exclusive OR (XOR) Prepared by: Dr Masri Ayob - TK2123 48

49 ROTATE INSTRUCTIONS Prepared by: Dr Masri Ayob - TK2123 49

50 SHIFT INSTRUCTIONS Prepared by: Dr Masri Ayob - TK2123 50

51 COMPARE INSTRUCTIONS Prepared by: Dr Masri Ayob - TK2123 51 CMP R/M: Compare (Register or Memory) with Acc. CMP R/M: Compare (Register or Memory) with Acc. This is a 1-byte instruction. This is a 1-byte instruction. It compares the data byte in register or memory with the contents of the acc.. It compares the data byte in register or memory with the contents of the acc.. If (A) < (R/M), the CY flag is set and the Zero flag is reset. If (A) < (R/M), the CY flag is set and the Zero flag is reset. If (A) = (R/M), the Zero flag is set and the CY flag is reset. If (A) = (R/M), the Zero flag is set and the CY flag is reset. If (A) > (R/M), the CY and Zero flags are reset. If (A) > (R/M), the CY and Zero flags are reset. When memory is an operand, its address is specified by (HL). When memory is an operand, its address is specified by (HL). No contents are modified; however, all remaining flags (S, P, AC) are affected according to the result of the subtraction. No contents are modified; however, all remaining flags (S, P, AC) are affected according to the result of the subtraction. CPI 8-bit: Compare Immediate with Accumulator CPI 8-bit: Compare Immediate with Accumulator This is a 2-byte instruction, the second byte being 8-bit data. This is a 2-byte instruction, the second byte being 8-bit data. It compares the second byte with (A). It compares the second byte with (A). If (A) < 8-bit data, the CY flag is set and the Zero flag is reset. If (A) < 8-bit data, the CY flag is set and the Zero flag is reset. If (A) = 8-bit data, the Zero flag is set, and the CV flag is reset. If (A) = 8-bit data, the Zero flag is set, and the CV flag is reset. If (A)> 8-bit data, the CY and Zero flags are reset. If (A)> 8-bit data, the CY and Zero flags are reset. No contents are modified; however, all remaining flags (S, P, AC) are affected according to the result of the subtraction. No contents are modified; however, all remaining flags (S, P, AC) are affected according to the result of the subtraction.

52 EXAMPLE 1 Prepared by: Dr Masri Ayob - TK2123 52 Write a program to load the accumulator with the data byte 64H, and verify whether the data byte in memory location 2050H is equal to the accumulator contents. If both data bytes are equal, jump to memory location BUFFER. Write a program to load the accumulator with the data byte 64H, and verify whether the data byte in memory location 2050H is equal to the accumulator contents. If both data bytes are equal, jump to memory location BUFFER. LXI H,2050H MVI A,64H CMP M JZ BUFFER

53 EXAMPLE 2 Prepared by: Dr Masri Ayob - TK2123 53 Write a program to load the accumulator with the data byte 64H, and verify whether the data byte in memory location 2050H is greater than the accumulator contents. If yes, jump to memory location BUFFER1. Else jump to memory location BUFFER2. Write a program to load the accumulator with the data byte 64H, and verify whether the data byte in memory location 2050H is greater than the accumulator contents. If yes, jump to memory location BUFFER1. Else jump to memory location BUFFER2.

54 SUMMARY Prepared by: Dr Masri Ayob - TK2123 54 Most arithmetic and logic instructions affect the flag bits, whereas the data transfer instructions did not affect the flags. Most arithmetic and logic instructions affect the flag bits, whereas the data transfer instructions did not affect the flags. Many arithmetic and logic instructions gate the result into the accumulator. Many arithmetic and logic instructions gate the result into the accumulator. The arithmetic and logic instructions use register, immediate, and register indirect addressing. The arithmetic and logic instructions use register, immediate, and register indirect addressing. Addition is available as add 1 to any register or register pair, 8- and 16-bit binary, 8-bit binary with carry, and binary-coded decimal (BCD). Addition is available as add 1 to any register or register pair, 8- and 16-bit binary, 8-bit binary with carry, and binary-coded decimal (BCD).

55 SUMMARY Prepared by: Dr Masri Ayob - TK2123 55 Subtraction is available as subtract 1 from any register or register pair, 8-bit binary, 8-bit binary with borrow, and as a compare, which is a form of subtraction. Subtraction is available as subtract 1 from any register or register pair, 8-bit binary, 8-bit binary with borrow, and as a compare, which is a form of subtraction. The logic operations are AND, OR, exclusive-OR, and invert. The logic operations are AND, OR, exclusive-OR, and invert. The logic instructions are ideal for control because AND clears bits, OR sets bits, and exclusive-OR complements bits. This gives the programmer complete control over each bit of a number. The logic instructions are ideal for control because AND clears bits, OR sets bits, and exclusive-OR complements bits. This gives the programmer complete control over each bit of a number. Programmed logic replaces discrete logic circuits at a substantial cost advantage. Programmed logic replaces discrete logic circuits at a substantial cost advantage.

56 SUMMARY Prepared by: Dr Masri Ayob - TK2123 56 Some 8085 instructions have hidden functions: ADD A shifts the accumulator left, and DAD H shifts HL left. Some 8085 instructions have hidden functions: ADD A shifts the accumulator left, and DAD H shifts HL left. Both ANA A and ORA A test the accumulator, and SUB A and XRA A clear the accumulator. Both ANA A and ORA A test the accumulator, and SUB A and XRA A clear the accumulator. The rotate commands create the shift-right functions: logical and arithmetic shift right. The rotate commands create the shift-right functions: logical and arithmetic shift right.

57 Thank you Q&A Prepared by: Dr Masri Ayob - TK2123 57


Download ppt "Ass. Prof. Dr Masri Ayob Lecture 5: Arithmetic and Logic Instructions TK 2633: Microprocessor & Interfacing."

Similar presentations


Ads by Google