ass. prof. dr masri ayob lecture 5: arithmetic and logic instructions tk 2633: microprocessor &...

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

Upload: cecily-hensley

Post on 13-Dec-2015

224 views

Category:

Documents


4 download

TRANSCRIPT

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

Ass. Prof. Dr Masri AyobAss. Prof. Dr Masri Ayob

Lecture 5: Arithmetic and Logic Instructions

TK 2633:Microprocessor & Interfacing

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

OBJECTIVES

Prepared by: Dr Masri Ayob - TK21232

Explain the operation of the 8085 arithmetic Explain the operation of the 8085 arithmetic and logic instructions and describe their and logic instructions and describe their effect on the flag bits.effect on the flag bits.

Describe how DAA adjusts the result of a Describe how DAA adjusts the result of a BCD addition.BCD addition.

Write short programs using the arithmetic Write short programs using the arithmetic and logic operations.and logic operations.

Use the logic instructions to clear, set, and Use the logic instructions to clear, set, and complement bus.complement bus.

Test the accumulator, using the ANA A or Test the accumulator, using the ANA A or ORA A instruction.ORA A instruction.

Explain the operation of the shift and rotate Explain the operation of the shift and rotate instructions and their effect on the flag bits.instructions and their effect on the flag bits.

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

ARITHMETIC OPERATIONS

Prepared by: Dr Masri Ayob - TK21233

Addition,subtraction,increment, and decrement.

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

ARITHMETIC OPERATIONS

Prepared by: Dr Masri Ayob - TK21234

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

ARITHMETIC OPERATIONS

Prepared by: Dr Masri Ayob - TK21235

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

ARITHMETIC OPERATIONS

Prepared by: Dr Masri Ayob - TK21236

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

ARITHMETIC OPERATIONS

Prepared by: Dr Masri Ayob - TK21237

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

ADDITION

Prepared by: Dr Masri Ayob - TK21238

Addition takes several forms in the 8085 Addition takes several forms in the 8085 microprocessor: microprocessor: 8-bit binary, 8-bit binary, 16-bit binary, 16-bit binary, and two-digit binary-coded-decimal (BCE)) and two-digit binary-coded-decimal (BCE))

addition. addition. Binary addition functions with either signed Binary addition functions with either signed

or unsigned numbers; or unsigned numbers; BCD addition uses only unsigned numbers.BCD addition uses only unsigned numbers.The instruction set supports additions using The instruction set supports additions using

register addressing, register indirect register addressing, register indirect addressing, and immediate addressing, but addressing, and immediate addressing, but not direct addressing.not direct addressing.

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

ADDITION

Prepared by: Dr Masri Ayob - TK21239

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

ADDITION

Prepared by: Dr Masri Ayob - TK212310

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

ADDITION

Prepared by: Dr Masri Ayob - TK212311

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

Addition with Carry

Prepared by: Dr Masri Ayob - TK212312

Whenever large numbers (numbers wider than 8 bits, or Whenever large numbers (numbers wider than 8 bits, or multiple-byte numbers) are added, the carry must be multiple-byte numbers) are added, the carry must be propagated from one 8-bit segment to the next. propagated from one 8-bit segment to the next.

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

Addition with Carry

Prepared by: Dr Masri Ayob - TK212313

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

Example

Prepared by: Dr Masri Ayob - TK212314

Suppose that the DE register pair contains a 16-bit number that we must 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-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 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 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.of E and C to generate the correct answer when D and B are added.

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

Example

Prepared by: Dr Masri Ayob - TK212315

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

Sixteen-Bit Addition

Prepared by: Dr Masri Ayob - TK212316

The 8085 instruction set does contain special instructions (DAD) that do 16-bit addition.

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

BCD Addition

Prepared by: Dr Masri Ayob - TK212317

BCD addition is like binary addition except that the numbers range in value only from 0 BCD addition is like binary addition except that the numbers range in value only from 0 through 9. through 9.

A special instruction allows BCD addition by using the standard binary addition (ADD) A special instruction allows BCD addition by using the standard binary addition (ADD) instructions. instructions. The The DAADAA instruction appears after a BCD addition (with a binary add instruction) to correct the instruction appears after a BCD addition (with a binary add instruction) to correct the

BCD result. 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.

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

Example

Prepared by: Dr Masri Ayob - TK212318

Example 5-6 illustrates the summation of the packed Example 5-6 illustrates the summation of the packed BCD numbers 11 and 19. BCD numbers 11 and 19.

After this addition, the accumulator contains a 2AH, After this addition, the accumulator contains a 2AH, which is not a BCD number:which is not a BCD number:the answer should be a 30the answer should be a 30BCDBCD The DAA instruction corrects the answer after the addition and The DAA instruction corrects the answer after the addition and

provides a 30provides a 30BCD BCD after the DAA instruction executes. after the DAA instruction executes.

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

BCD Addition

Prepared by: Dr Masri Ayob - TK212319

The DAA instruction changes the result through the two tests listed by adding a OOH, The DAA instruction changes the result through the two tests listed by adding a OOH, 06H, 60H, or 66H to the accumulator.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 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.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 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.adds a 60H to the accumulator.

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

Increment

Prepared by: Dr Masri Ayob - TK212320

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-The increment command is either an 8-bit (INR) increment or a 16-

bit (INX) Increment instruction.bit (INX) Increment instruction. The INR instructions affect all the flags except carry, and the INX The INR instructions affect all the flags except carry, and the INX

instructions affect no flags.instructions affect no flags.

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

Prepared by: Dr Masri Ayob - TK2123

21

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

Subtraction

Prepared by: Dr Masri Ayob - TK212322

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 It also supports a subtraction instruction that allows a borrow to be

propagated through additional bytes of a number. propagated through additional bytes of a number. The subtract-with-borrow instruction aids in the subtraction of The subtract-with-borrow instruction aids in the subtraction of

multiple-byte numbers. multiple-byte numbers. . .

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

Subtraction

Prepared by: Dr Masri Ayob - TK212323

The 8085 performs subtraction by using the The 8085 performs subtraction by using the method of 2’s complement. 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 the instruction SUB to subtract contents of a

source register or source register or the instruction SUI to subtract an 8-bit number the instruction SUI to subtract an 8-bit number

from contents of the accumulator.from contents of the accumulator.In either case, the accumulator contents are In either case, the accumulator contents are

regarded as minuend (the number from which to regarded as minuend (the number from which to subtract).subtract).

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

Subtraction

Prepared by: Dr Masri Ayob - TK212324

Various subtraction instructions: register, register indirect, and Various subtraction instructions: register, register indirect, and immediate addressing. 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 Each of these instructions affects the flag bits, so they reflect

various conditions about the difference after a subtraction.various conditions about the difference after a subtraction.

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

Subtraction

Prepared by: Dr Masri Ayob - TK212325

The 8085 performs the following steps internally to execute the instruction SUB The 8085 performs the following steps internally to execute the instruction SUB (or SUI):(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.

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

Illustrative Program: Subtraction of Two Numbers

Prepared by: Dr Masri Ayob - TK212326

PROBLEM STATEMENTPROBLEM STATEMENTWrite a program to do the following:Write a program to do the following:

Load the number 3011 in register B and 3911 in Load the number 3011 in register B and 3911 in register C.register C.

Subtract 39H from 3011.Subtract 39H from 3011.Display the answer at PORT I. Display the answer at PORT I.

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

Illustrative Program: Subtraction of Two Numbers

Prepared by: Dr Masri Ayob - TK212327

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

Illustrative Program: Subtraction of Two Numbers

Prepared by: Dr Masri Ayob - TK212328

PROGRAM DESCRIPTIONPROGRAM DESCRIPTIONRegisters B and C are loaded with 30H and Registers B and C are loaded with 30H and

39H, respectively. 39H, respectively. The instruction MOV A,B copies 30H into the The instruction MOV A,B copies 30H into the

accumulator (shown as register contents). accumulator (shown as register contents).

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

Illustrative Program: Subtraction of Two Numbers

Prepared by: Dr Masri Ayob - TK212329

To execute the instruction SUB C the microprocessor performs To execute the instruction SUB C the microprocessor performs the following steps internally:the following steps internally:

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

Illustrative Program: Subtraction of Two Numbers

Prepared by: Dr Masri Ayob - TK212330

PROGRAM DESCRIPTIONPROGRAM DESCRIPTIONThe number F7H is a 2’s complement of the The number F7H is a 2’s complement of the

magnitude (39H-30H)=09H.magnitude (39H-30H)=09H.This ‘Cy’ flag is set, indicating the answer is in 2’s This ‘Cy’ flag is set, indicating the answer is in 2’s

complement.complement.The instruction OUT displays F7 at PORT1.The instruction OUT displays F7 at PORT1.

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

Subtraction

Prepared by: Dr Masri Ayob - TK212331

P= 0 Odd parity

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

Subtract with Borrow

Prepared by: Dr Masri Ayob - TK212332

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

Subtract with Borrow

Prepared by: Dr Masri Ayob - TK212333

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

Example

Prepared by: Dr Masri Ayob - TK212334

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.

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

LOGIC INSTRUCTIONS

Prepared by: Dr Masri Ayob - TK212335

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 Why does a microprocessor instruction set contain logic instructions? contain logic instructions? Logic instructions sometimes replace discrete Logic instructions sometimes replace discrete

logic gates. logic gates. Today, program storage costs about of a cent Today, program storage costs about of a cent

per byte. per byte. System control software usually requires bit System control software usually requires bit

manipulation — a logic operation. manipulation — a logic operation.

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

Inversion (NOT)

Prepared by: Dr Masri Ayob - TK212336

The CMA instruction (2FH in machine language), The CMA instruction (2FH in machine language), one’s-complements or inverts the contents of the one’s-complements or inverts the contents of the accumulator.accumulator.

This operation, which affects none of the flag bits, This operation, which affects none of the flag bits, causes each bit of the accumulator to be inverted causes each bit of the accumulator to be inverted (changed from 1 to 0 or 0 to 1). (changed from 1 to 0 or 0 to 1).

The CMA instruction causes the accumulator to The CMA instruction causes the accumulator to appear as eight inverters (or NOT). appear as eight inverters (or NOT). This means that this one-byte instruction replaces eight This means that this one-byte instruction replaces eight

discrete inverters if the speed required is not too great. discrete inverters if the speed required is not too great. The amount of circuitry replaced by the CMA instruction is The amount of circuitry replaced by the CMA instruction is 14 of a 7404 TTL hex inverter.14 of a 7404 TTL hex inverter.

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

The AND Operation

Prepared by: Dr Masri Ayob - TK212337

The AND instruction, has two separate functions in The AND instruction, has two separate functions in a microprocessor-based system. a microprocessor-based system. The AND instruction selectively clears bits of the The AND instruction selectively clears bits of the

accumulator or replaces discrete AND gates.accumulator or replaces discrete AND gates.The AND instruction, functions as eight The AND instruction, functions as eight

independent two-input AND gates. This instruction independent two-input AND gates. This instruction replaces two 7408 quad two-input AND gates.replaces two 7408 quad two-input AND gates.

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

The AND Operation

Prepared by: Dr Masri Ayob - TK212338

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

The AND Operation

Prepared by: Dr Masri Ayob - TK212339

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

The OR Operation

Prepared by: Dr Masri Ayob - TK212340

Has two separate functions in a Has two separate functions in a microprocessor-based system:microprocessor-based system:It selectively sets bits of the accumulator orIt selectively sets bits of the accumulator orReplaces discrete OR gates.Replaces discrete OR gates.

The inclusive-OR instruction functions as eight The inclusive-OR instruction functions as eight independent two-input OR gates.independent two-input OR gates.

This instruction replaces two 7432 quad two-This instruction replaces two 7432 quad two-input OR gates. input OR gates.

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

The OR Operation

Prepared by: Dr Masri Ayob - TK212341

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

The OR Operation

Prepared by: Dr Masri Ayob - TK212342

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

Prepared by: Dr Masri Ayob - TK212343

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

Illustrative Program: ORing Data from Two Input Ports

Prepared by: Dr Masri Ayob - TK212344

PROBLEM STATEMENTPROBLEM STATEMENTTwo input port with eight switches (each Two input port with eight switches (each

port) at address 00H and OIH (Figure 6.9) is port) at address 00H and OIH (Figure 6.9) is connected to the microcomputer to control connected to the microcomputer to control the same appliances and lights from the the same appliances and lights from the bedroom as well as from the kitchen. bedroom as well as from the kitchen.

Write instructions to turn on the devices from Write instructions to turn on the devices from any of the input ports.any of the input ports.

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

Illustrative Program: ORing Data from Two Input Ports

Prepared by: Dr Masri Ayob - TK212345

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

Illustrative Program: ORing Data from Two Input Ports

Prepared by: Dr Masri Ayob - TK212346

PROBLEM ANALYSISPROBLEM ANALYSISTo turn on the appliances from any one of the input To turn on the appliances from any one of the input

ports, the microprocessor needs to read the switches at ports, the microprocessor needs to read the switches at both ports and logically OR the switch positions.both ports and logically OR the switch positions.

Assume that the switch positions in one input port Assume that the switch positions in one input port (located in the bedroom) correspond to the data byte (located in the bedroom) correspond to the data byte 9lH and the switch positions in the second port (located 9lH and the switch positions in the second port (located in the kitchen) correspond to the data byte A8H. in the kitchen) correspond to the data byte A8H.

The person in the bedroom wants to turn on the air The person in the bedroom wants to turn on the air conditioner, the radio, and the bedroom light; and the conditioner, the radio, and the bedroom light; and the person in the kitchen wants to turn on the air-person in the kitchen wants to turn on the air-conditioner, the coffeepot, and the kitchen light.conditioner, the coffeepot, and the kitchen light.

By ORing these two data bytes, the MPU can turn ON By ORing these two data bytes, the MPU can turn ON the necessary appliances.the necessary appliances.

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

Illustrative Program: ORing Data from Two Input Ports

Prepared by: Dr Masri Ayob - TK212347

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

Exclusive OR (XOR)

Prepared by: Dr Masri Ayob - TK212348

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

ROTATE INSTRUCTIONS

Prepared by: Dr Masri Ayob - TK212349

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

SHIFT INSTRUCTIONS

Prepared by: Dr Masri Ayob - TK212350

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

COMPARE INSTRUCTIONS

Prepared by: Dr Masri Ayob - TK212351

CMP CMP R/M: Compare (Register or Memory) with Acc. 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 It compares the data byte in register or memory with the contents of the

acc..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 No contents are modified; however, all remaining flags (S, P, AC) are

affected according to the result of the subtraction.affected according to the result of the subtraction. CPICPI 8-bit: Compare Immediate with Accumulator 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, No contents are modified; however, all remaining flags (S, P, AC) are affected all remaining flags (S, P, AC) are affected

according to the result of the subtraction.according to the result of the subtraction.

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

EXAMPLE 1

Prepared by: Dr Masri Ayob - TK212352

Write a program to load the accumulator with the data byte Write a program to load the accumulator with the data byte 64H, and verify whether the data byte in memory location 64H, and verify whether the data byte in memory location 2050H is equal to the accumulator contents. If both data bytes 2050H is equal to the accumulator contents. If both data bytes are equal, jump to memory location BUFFER.are equal, jump to memory location BUFFER.

LXI H,2050H

MVI A,64H

CMP M

JZ BUFFER

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

EXAMPLE 2

Prepared by: Dr Masri Ayob - TK212353

Write a program to load the accumulator with the data byte 64H, Write a program to load the accumulator with the data byte 64H, and verify whether the data byte in memory location 2050H is and verify whether the data byte in memory location 2050H is greater than the accumulator contents. If yes, jump to memory greater than the accumulator contents. If yes, jump to memory location BUFFER1. Else jump to memory location BUFFER2.location BUFFER1. Else jump to memory location BUFFER2.

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

SUMMARY

Prepared by: Dr Masri Ayob - TK212354

Most arithmetic and logic instructions affect Most arithmetic and logic instructions affect the flag bits, whereas the data transfer the flag bits, whereas the data transfer instructions did not affect the flags.instructions did not affect the flags.

Many arithmetic and logic instructions gate the Many arithmetic and logic instructions gate the result into the accumulator.result into the accumulator.

The arithmetic and logic instructions use The arithmetic and logic instructions use register, immediate, and register indirect register, immediate, and register indirect addressing.addressing.

Addition is available as add 1 to any register or Addition is available as add 1 to any register or register pair, 8- and 16-bit binary, 8-bit binary register pair, 8- and 16-bit binary, 8-bit binary with carry, and binary-coded decimal (BCD).with carry, and binary-coded decimal (BCD).

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

SUMMARY

Prepared by: Dr Masri Ayob - TK212355

Subtraction is available as subtract 1 from any Subtraction is available as subtract 1 from any register or register pair, 8-bit binary, 8-bit binary register or register pair, 8-bit binary, 8-bit binary with borrow, and as a compare, which is a form of with borrow, and as a compare, which is a form of subtraction.subtraction.

The logic operations are AND, OR, exclusive-OR, The logic operations are AND, OR, exclusive-OR, and invert.and invert.

The logic instructions are ideal for control because The logic instructions are ideal for control because AND clears bits, OR sets bits, and exclusive-OR AND clears bits, OR sets bits, and exclusive-OR complements bits. This gives the programmer complements bits. This gives the programmer complete control over each bit of a number.complete control over each bit of a number.

Programmed logic replaces discrete logic circuits at Programmed logic replaces discrete logic circuits at a substantial cost advantage.a substantial cost advantage.

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

SUMMARY

Prepared by: Dr Masri Ayob - TK212356

Some 8085 instructions have hidden functions: Some 8085 instructions have hidden functions: ADD A shifts the accumulator left, and DAD H ADD A shifts the accumulator left, and DAD H shifts HL left. shifts HL left.

Both ANA A and ORA A test the accumulator, Both ANA A and ORA A test the accumulator, and SUB A and XRA A clear the accumulator.and SUB A and XRA A clear the accumulator.

The rotate commands create the shift-right The rotate commands create the shift-right functions: logical and arithmetic shift right.functions: logical and arithmetic shift right.

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

Thank youQ&A

Prepared by: Dr Masri Ayob - TK212357