Arithmetic Group of Instructions


Arithmetic group of Instructions


Arithmetic group of instruction has total of 14 instruction types which are further extended to 64 instructions.

They are:

Instructions to perform addition.
Instructions to performs subtraction.
Instruction to perform decimal addition.


Instructions to perform Addition

8085 is primitive microprocessor. In its arithmetic group of instructions it has only add and subtract type of instructions. it does not have instructions to multiply or divide numbers. In this section we discuss addition type of instructions.

8085 imposes the restriction that one of the operands must be in the accumulator. The other operand can be one of the following

Contents of an 8-bit register.
Contents of memory location pointed by HL register pair.
Eight bit immediate data.

Instruction Type ADD R
ADD is a mnemonic that stands for Add contents of R to accumulator. In this case R stands for any of the following register pairs or memory location M pointed by HL pair.

R= A,B,C,D,E,H,L or M

This instruction is used to add contents of R and accumulator. Finally the result of addition is stored in accumulator. As R can have any of eight values, there are eight opcodes for this type of instruction. It occupies only 1-byte in memory. ADD E is an example instruction of this type.
The result of execution is shown below
Summary:
ADD R( 1-byte; ADD E; 8 opcode)
Instruction Type ADI d8

ADI is a mnemonic, which stands for ADd Immediate to Accumulator and d8 stands for any 8-bit data. This instruction is used to add 8-bit immediate data to the accumulator. The result of addition will be stored in accumulator. The flags are affected based on the results.

Example of this instruction is ADI F3H


Summary:
ADI d8( 2-bytes; ADI F3H; 1 opcode)

Instruction Type INR R

INR is a mnemonic that stands for INcRement and R stands for any of the following registers or memory location M pointed by HL pair.

R=A,B,C,D,E,H,L or M

This instruction is used to add 1 to the contents of R. The result of increment will be stored in R.

All flags, except CY, flag are affected depending on the results.

Example of this instruction is INR M

Summary:

INR R( 1-byte; INR M; 8 opcodes)

Instruction type ADC R

There are times when a user is required to add tow numbers each of which is several bytes in size.
For example , we are adding 16 bit numbers.
In this example the addition of 56H and F2H results in a sum of 48H with carry of 1. 

Next we have to add 34H and A2H along with this carry value of 1.

To facilitate such operations 8085 provides instructions to add two numbers along with carry.

ADC is mnemonic that stands for Add with carry and R stands for any of the following registers or memory location M pointed by HL pair.

R=A,B,C,D,E,H,L, or M

This instruction is used to add contents of R and accumulator along with the carry value.

The result of addition will be store in the accumulator.

ADC E: The result of execution is shown below


Summary:
                       ADC R( 1-byte; ADC E; 8 opcodes)


Instruction type ACI R

ACI is a mnemonic that stands for Add with Carry Immediate to accumulator and d8 stands for any 8-bit dat. 

This instruction is used to add 8 bit immediate data to the accumulator along with the carry value. 

The result of addition will be stored in the accumulator.


ACI F3H: The result of execution is shown below

Summary:
                       ACI d8( 2-bytes; ACI F3H; 1 opcode)


Instructions to perform subtraction

In the subtraction of two numbers 8085 imposes the restriction that accumulator will have one operand from which the other operand specified by one of the following will be subtracted.

Contents of 8-bit register.
Contents of memory location pointed by HL pair.
8-bit immediate data.

SUB is a mnemonic that stands for SUBtract contents of R from Accumulator.

R=A,B,C,D,E,H,L, or M

The result of the subtraction will be stored in the Accumulator.


SUB E: The result of execution is shown below


Summary:
                       Sub R( 1-byte; SUB E; 8 opcodes)

Why S=1? 

Because the number is negative. 

How ? 

We check in two ways if result is in hex or binary. Look at the left digit and deduce the result as given. 

0-7 <-> 0000-0111 <-> pos

8-F <-> 1000-1111 <-> neg

In previous example result is AE so left digit is A which is >7 hence number comes in range of 8-F the result is negative. Similarly convert whole number AE into binary and check the MSB(Left most digit) if it is 1 number is negative and if it is 0 number positive

Internally , 8085 performs this subtraction by adding the 2’s complement of 45 H to F3H.

To get 2's complement we need to first convert 45H into binary then complement the bits and add 1. This can be seen in picture below. 
Furthermore we have to add this number into F3H.










Instruction type SUI d8

SUI stands for Subtract Immediate from Accumulator and d8 stands for any 8-bit data. 

This instruction is used to subtract 8-bit immediate data from the accumulator.

The result of the subtraction will be stored in the accumulator.

The flags are affected based on the results. 

SUI F3H: The result of execution is shown below


The F3 value is needed to be converted into binary value then complement and add +1 to get two's complement. The result is added to the operand 45H.



Summary:
                       SUI d8( 2-bytes; SUI F3H; 1 opcode)

Instruction type DCR R

DCR is a mnemonic that stands for DeCRement and R stands for following registers or memory location pointed b HL pair.

This instruction is used to subtract 1 from the contents of R. The result of the increment will be stored in R. All flags, except Cy flag are affected depending on the result.

DCR M: The result of execution is shown below

Summary:
                       DCR R( 1-byte; DCR M; 8 opcodes)


Instruction type SBB R

There are times when user is required to subtract two numbers each of which is several bytes in size.

For example, let us say it is needed to perform the following subtraction.

In this example the subtraction of 56H and F2H results in 64H with a borrow of 1.

Next we have to subtract 34H and 12H along with this borrow.

To facilitate such an operation, 8085 provides instruction to subtract two numbers along with borrow value.

SBB is mnemonic that stands for SuBtract with Borrow and R stands for any of the following registers or memory location M pointed by HL pair.

This instruction is used to subtract contents of R from accumulator along with carry value.

The result of subtraction will be stored in accumulator.

SBB E: The result of execution is shown below



Summary:
                       SBB R( 1-byte; SBB E; 8 opcodes)


Instruction type SBI d8

SBI is a mnemonic that stands for ‘Subtract with Borrow Immediate from Accumulator’ and d8 stands for any 8-bit data.

This instruction is used to subtract 8-bit immediate data from the accumulator along with the carry value.

The result of subtraction will be stored in accumulator.

SBI F3H: The result of execution is shown below


Summary:
                       SBI d8( 2-bytes; SBI F3H; 1 opcode)

Instruction type INX rp

INX is a mnemonic that stands for Increment eXtended register and rp stands for any of the following register pair rp=BC, DE, or HL.

This instruction is used to add 1 to the contents of rp. The result of increment will be stored in rp.

A register pair is generally used to store a memory address.

INX B: The result of execution is shown below

Flags are not affected in this instruction by default because it causes problems in many cases.

Summary:
                       INX rp( 1-byte; INX B; 3 opcode)


Instruction type DCX rp

DCX is a mnemonic that stands for DeCrement eXtended register and rp stands for any of following register pair rp= BC, DE or HL.

This instruction is used to subtract 1 from the contents of rp. The result of decrement will be stored in rp.

Flags are not affected in this type of instruction.

DCX B: The result of execution is shown below



It may appear that DCX B is same as DCR C, But if content of BC was F900H, it becomes F8FFH after execution, thus DCX decrements 16-bit quantity while DCR decrements 8-bit quantity.

It may appear that DCX B is same as DCR C, But if content of BC was F900H, it becomes F8FFH after execution, thus DCX decrements 16-bit quantity while DCR decrements 8-bit quantity.

Summary:
DCX rp( 1-byte; DCX B; 3 opcode)


Instruction type DAD rp

Intel 8085 is basically an 8-bit microprocessor. But the designers have provided instructions to perform 16-bit additions also. 

As the internal architecture is only 8 bit. This instruction easily takes double the time needed to add two 8-bit numbers.

Dad is a mnemonic which stands for Double Add and rp stands for any of the following register pair Rp= BC, DE, or HL

This instruction is used to add contents of rp to HL. 

The result of addition will be stored in HL.

Thus in this instruction , HL is used as a 16-bit accumulator. 

Only Cy flag is affected depending on the result.

DAD B: The result of execution is shown below


Summary:
                       DAD rp( 1-byte; DAD B; 3 opcodes)

Decimal addition in 8085

In digital computer every thing is represented by 0s and 1s.

For example an instruction will have a code using only 0s and 1s.

Data is also represented using 0s and 1s.

Data can be of different types like unsigned numbers, signed numbers, floating point numbers, binary coded decimal(BCD) numbers.

BCD Numbers

Many time we are required to represent decimal numbers in a computer to perform arithmetic of these numbers.

For example we may be required to total marks a student has obtained in five different subjects where obviously the marks are awarded in decimal notation.

For this purpose BCD code is extensively used.

In BCD notation 4 bits are used to code a digit and so two digits of information is stored in a byte.

For example decimal 39 is represented in BCD so two digit of information is stored in a byte.

Decimal 39 is represented in BCD as 0011, 1001

Codes 1010,1011,1100,1101,1110 and 1111 are illegal in BCD notation

Similarly decimal 1024 is represented in BCD as

0001 0000 0010 0100

Same decimal is represented in binary in binary as

0000 0100 0000 0000

DAA Instruction

Suppose we want to add two decimal numbers 38 and 45.

They will be represented in BCD as 0011 1000 and 0100 0101.

The addition will be 0111 1101. if we interpret this result as BCD number the answer is incorrect as well as illegal.

This is where DAA instruction proves its usefulness.

The working of DAA instruction depends on AL register, Cy and AC flags.

In effect , it adds 00H, 06H, 60H or 66H to accumulator as to get the correct BCD answer in the accumulator with given conditions.

Condition #1

If the LS hex digit in A is <=9 and AC flag is 0, the LS hex digit value will not be altered.

Condition#2

If the LS hex digit is >9 or if AC flag is set to 1, it adds 6 to the LS hex digit of A.

Further it increments the MS hex digit if this addition resulted in carry to the MS digit position.

In this process, the Cy flag will be set to 1 if the MS hex digit was incremented from F to 0.

Condition#3

If the MS hex digit is <=9 and Cy flag is 0. The MS hex digit will not be altered. And CY flag will be reset to 0.

Condition#4

If the MS hex digit is>9 or if Cy flag is set to 1, it adds 6 to the MS hex digit of A and sets Cy flag to 1.

Note: DAA instruction can not be used for decimal subtraction.

Add 45 BCD and 38 BCD and store the result 7DH in A.

In this case, Cy flag=0 and AC flag=0, But a D is an invalid BCD code.

The DAA instruction adds 06 to A. Thus we get 83H in A which will now be interpreted as 83 BCD.



Post a Comment

0 Comments