Arithmetic Programs
Addition, subtraction, multiplication buffer.
Arithmetic operations form the foundation of almost every useful program written for the 8085 microprocessor. Understanding how to implement addition, subtraction, and multiplication at the assembly level builds a deep intuition for how the CPU manipulates data, how flags respond to results, and how multi-byte operations are handled. These programs are also standard exercises in university practicals and appear conceptually in GATE questions.
Core Concept Explanation
In the 8085, all arithmetic operations pass through the accumulator (register A). Before performing any operation, the operands must be loaded into appropriate registers. The primary instruction for single-byte addition is ADD r, which adds the contents of register r to the accumulator and stores the result back in A. For subtraction, SUB r subtracts the register r value from the accumulator and uses 2's complement internally to perform the operation.
The 8085 does not have a hardware multiply instruction. Multiplication is implemented using a repeated addition loop. One operand serves as the value to be added repeatedly, and the other operand serves as the loop counter. The process adds the multiplicand to an accumulating register repeatedly, once per count, until the counter reaches zero. The result is the product.
When the result of an addition exceeds 255 (0xFF), the CPU sets the Carry flag (CY). Programs that handle 16-bit results must check this flag and store both the low byte from the accumulator and the carry bit separately. This is the concept of a multi-byte result buffer, where the low byte is stored at one memory address and a 00H or 01H (carry) is stored at the next address.
Mathematical Expression
For addition: Result = A + r. If Result > 0xFF, then CY = 1 and Result = Result mod 256. For subtraction: Result = A - r. If A < r, the result is computed using 2's complement, and CY flag is set to indicate a borrow. For multiplication by repeated addition: Product = Multiplicand added Multiplier times. This is equivalent to P = M x N, computed as P = M + M + M ... N times. The loop decrements the counter (N) each iteration until it reaches zero, which sets the Zero flag and terminates the loop.
Practical Understanding
A critical practical detail is memory addressing for storing results. The instruction STA address stores the accumulator content at a 16-bit memory address. For multi-byte results, the low byte is stored first using STA, and then the carry is moved to the accumulator using MVI A, 00H followed by ADC A (or simply by moving the carry flag value), and stored at the next address.
The multiplication routine relies on the JNZ (Jump if Not Zero) instruction to loop. The counter register is decremented using DCR B, which affects the Zero flag. When the Zero flag becomes 1 (counter reaches 0), the JNZ condition fails and the loop exits. This is the standard pattern for implementing any count-based loop in 8085 programs.
Given:
Multiply 04H by 03H using repeated addition.
Why this formula applies:
8085 has no MUL instruction. Multiplication = repeated addition of multiplicand.
Formula:
Product = Multiplicand added Multiplier times
04H + 04H + 04H = 0CH
Program:
MVI A, 00H ; Clear accumulator (result = 0)
MVI B, 03H ; B = multiplier (loop count)
MVI C, 04H ; C = multiplicand
LOOP: ADD C ; A = A + C
DCR B ; B = B - 1 (sets Zero flag when B = 0)
JNZ LOOP ; repeat if B is not zero
HLT
Calculation:
Iteration 1: A = 00H + 04H = 04H, B = 02H
Iteration 2: A = 04H + 04H = 08H, B = 01H
Iteration 3: A = 08H + 04H = 0CH, B = 00H, Z=1, loop exits
Final Answer:
A = 0CH = 12 decimal = 4 x 3Exam Tip: For subtraction in 8085, the Carry flag acts as a borrow flag. If CY = 1 after SUB, it means the minuend was smaller than the subtrahend and the result is in 2's complement form. This is a frequent source of confusion in GATE questions involving signed subtraction.
Program Mechanics in Key Points
- Addition: Load operands into A and a register using MVI. Execute ADD r. Store result using STA. Check CY for overflow.
- Subtraction: Load minuend into A, subtrahend into B. Execute SUB B. If CY = 1, result is negative and is in 2's complement form.
- Multiplication: Initialize accumulator to 0, counter register to multiplier value, and another register to the multiplicand. Repeatedly add multiplicand to accumulator and decrement counter until counter reaches 0.
- Multi-byte result storage: Low byte is stored at result address, carry is captured and stored at the next address using MVI A, 00H followed by ADC A (or explicit carry extraction).
- DCR instruction affects the Zero flag, making it compatible with JNZ for loop control. Note that DCR does not affect the Carry flag.
Quick Revision
- ADD r: A = A + r. Affects CY, Z, S, P, AC flags. Used for 8-bit addition.
- SUB r: A = A - r. CY = 1 means borrow (result negative). Result in 2's complement.
- Multiplication uses repeated addition loop. No hardware MUL in 8085.
- Loop structure: ADD C, DCR B, JNZ LOOP. Exits when B becomes 0.
- Multi-byte results: Store low byte at address N, carry at address N+1.
- Exam trap: DCR does not set the Carry flag. Use ADD or ADC for carry-related operations only.
- ADI data is used for immediate addition (not ADD). SUI data is used for immediate subtraction.
Arithmetic Programs Quiz
Test your ability to trace and write 8085 programs for addition, subtraction, and multiplication.