dr masri ayob tk 2633: microprocessor & interfacing lecture 5: arithmetic and logic instructions

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

Post on 21-Dec-2015

230 views

Category:

Documents


1 download

TRANSCRIPT

Dr Masri AyobDr Masri Ayob

TK 2633:TK 2633:Microprocessor & Interfacing Microprocessor & Interfacing

Lecture 5: Arithmetic and Logic Lecture 5: Arithmetic and Logic InstructionsInstructions

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212322

OBJECTIVESOBJECTIVES Explain the operation of the 8085 arithmetic and Explain the operation of the 8085 arithmetic and

logic instructions and describe their effect on the logic instructions and describe their effect on the flag bits.flag bits.

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

Write short programs using the arithmetic and Write short programs using the arithmetic and logic operations.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 ORA A Test the accumulator, using the ANA A or ORA A instruction.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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212333

ARITHMETIC OPERATIONSARITHMETIC OPERATIONS

Addition,Addition,subtraction,subtraction, increment, increment, and decrement.and decrement.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212344

ARITHMETIC OPERATIONSARITHMETIC OPERATIONS

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212355

ARITHMETIC OPERATIONSARITHMETIC OPERATIONS

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212366

ARITHMETIC OPERATIONSARITHMETIC OPERATIONS

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212377

ARITHMETIC OPERATIONSARITHMETIC OPERATIONS

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212388

ADDITIONADDITION 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)) addition. and two-digit binary-coded-decimal (BCE)) addition.

Binary addition functions with either signed or Binary addition functions with either signed or unsigned numbers; 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 addressing, register addressing, register indirect addressing, and immediate addressing, but not direct and immediate addressing, but not direct addressing.addressing.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK212399

ADDITIONADDITION

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231010

ADDITIONADDITION

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231111

ADDITIONADDITION

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231212

Addition with CarryAddition with Carry

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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231313

Addition with CarryAddition with Carry

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231414

ExampleExample

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231515

ExampleExample

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231616

Sixteen-Bit Addition Sixteen-Bit Addition

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231717

BCD Addition BCD Addition

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 The DAADAA instruction appears after a BCD addition (with a binary add instruction) to correct the BCD result. 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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231818

Example Example

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

After this addition, the accumulator contains a 2AH, which is After this addition, the accumulator contains a 2AH, which is not a BCD number: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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21231919

BCD AdditionBCD Addition

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232020

IncrementIncrement

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

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232121

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232222

SubtractionSubtraction

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

additional bytes of a number. 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. . .

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232323

SubtractionSubtraction

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 Subtraction can be performed by using either: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 regarded as minuend (the number from which to subtract).to subtract).

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232424

SubtractionSubtraction 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 Each of these instructions affects the flag bits, so they reflect various conditions about the

difference after a subtraction.difference after a subtraction.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232525

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232626

Illustrative Program: Subtraction of Two Illustrative Program: Subtraction of Two Numbers Numbers

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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232727

Illustrative Program: Subtraction of Two Illustrative Program: Subtraction of Two Numbers Numbers

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232828

Illustrative Program: Subtraction of Two Illustrative Program: Subtraction of Two Numbers Numbers

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21232929

Illustrative Program: Subtraction of Two Illustrative Program: Subtraction of Two NumbersNumbers

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:

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233030

Illustrative Program: Subtraction of Two Illustrative Program: Subtraction of Two Numbers Numbers

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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233131

SubtractionSubtraction

P= 0 Odd parity

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233232

Subtract with Borrow Subtract with Borrow

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233333

Subtract with Borrow Subtract with Borrow

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233434

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233535

LOGIC INSTRUCTIONS LOGIC INSTRUCTIONS

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 per Today, program storage costs about of a cent per

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

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233636

Inversion (NOT)Inversion (NOT)

The CMA instruction (2FH in machine language), one’s-The CMA instruction (2FH in machine language), one’s-complements or inverts the contents of the accumulator.complements or inverts the contents of the 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 appear The CMA instruction causes the accumulator to appear as eight inverters (or NOT). as eight inverters (or NOT). This means that this one-byte instruction replaces eight discrete This means that this one-byte instruction replaces eight discrete

inverters if the speed required is not too great. The amount of 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 circuitry replaced by the CMA instruction is 14 of a 7404 TTL hex inverter.inverter.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233737

The AND OperationThe AND Operation

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

replaces discrete AND gates.replaces discrete AND gates.

The AND instruction, functions as eight independent two-The AND instruction, functions as eight independent two-input AND gates. This instruction replaces two 7408 input AND gates. This instruction replaces two 7408 quad two-input AND gates.quad two-input AND gates.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233838

The AND OperationThe AND Operation

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21233939

The AND OperationThe AND Operation

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234040

The OR OperationThe OR Operation

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 The inclusive-OR instruction functions as eight independent two-input OR gates.eight independent two-input OR gates.

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234141

The OR OperationThe OR Operation

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234242

The OR OperationThe OR Operation

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234343

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234444

Illustrative Program: ORing Data from Two Illustrative Program: ORing Data from Two Input Ports Input Ports

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

at address 00H and OIH (Figure 6.9) is at address 00H and OIH (Figure 6.9) is connected to the microcomputer to control the connected to the microcomputer to control the same appliances and lights from the bedroom same appliances and lights from the bedroom as well as from the kitchen. 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.

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234545

Illustrative Program: ORing Data from Two Input Ports Illustrative Program: ORing Data from Two Input Ports

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234646

Illustrative Program: ORing Data from Two Illustrative Program: ORing Data from Two Input Ports Input Ports

PROBLEM ANALYSISPROBLEM ANALYSIS To turn on the appliances from any one of the input ports, the To turn on the appliances from any one of the input ports, the

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

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

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

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234747

Illustrative Program: ORing Data from Two Illustrative Program: ORing Data from Two Input Ports Input Ports

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234848

Exclusive OR (XOR)Exclusive OR (XOR)

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21234949

ROTATE INSTRUCTIONSROTATE INSTRUCTIONS

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21235050

SHIFT INSTRUCTIONSSHIFT INSTRUCTIONS

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21235151

SUMMARYSUMMARY

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

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

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

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21235252

SUMMARYSUMMARY

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

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

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

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21235353

SUMMARYSUMMARY

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

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

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

Prepared by: Dr Masri AyobPrepared by: Dr Masri Ayob - TK2123 - TK21235454

Thank youThank youQ&AQ&A