CSC167 · 2081 · 10 marks
Explain LHLD and SHLD instruction. Ten 8-bit data are stored at memory location starting from 6000H. Write an assembly program for 8085 microprocessor to calculate the sum of this data array and store the sum and carry starting from 9500H.
Explain LHLD and SHLD instruction. Ten 8-bit data are stored at memory location starting from 6000H. Write an assembly program for 8085 microprocessor to calculate the sum of this data array and store the sum and carry starting from 9500H.[10]
LHLD & SHLD Instructions + Sum of Data Array Program
Given
- Number of data values: 10 (0AH)
- Data type: 8-bit each
- Data starting address: 6000H (so 6000H to 6009H)
- Sum storage address: 9500H
- Carry storage address: implied 9501H (next location)
All required data is present.
Solution
Part 1: LHLD and SHLD Instructions
LHLD (Load H-L pair Direct)
A 3-byte instruction that loads the contents of two consecutive memory locations directly into the HL register pair.
Syntax: LHLD address
Operation:
- $L \leftarrow M[address]$
- $H \leftarrow M[address+1]$
Example: LHLD 2200H - if 2200H = 45H and 2201H = 32H, then after execution $L = 45H$, $H = 32H$.
Size: 3 bytes, Machine cycles: 5, T-states: 16
SHLD (Store H-L pair Direct)
A 3-byte instruction that stores the contents of the HL register pair into two consecutive memory locations.
Syntax: SHLD address
Operation:
- $M[address] \leftarrow L$
- $M[address+1] \leftarrow H$
Example: SHLD 2302H - if $H = 32H$, $L = 45H$, then 2302H = 45H, 2303H = 32H.
Size: 3 bytes, Machine cycles: 5, T-states: 16
Part 2: Assembly Language Program
Algorithm
- Initialize carry register $C = 00H$
- Initialize counter $B = 0AH$ (10 items)
- Point HL to 6000H
- Clear accumulator ($A = 00H$)
- Add memory content to A; if carry occurs, increment C
- Advance pointer, decrement counter, repeat until zero
- Store sum at 9500H, carry at 9501H
Program
; Sum of 10 eight-bit data stored from 6000H
; Sum -> 9500H, Carry -> 9501H
MVI C, 00H ; carry counter = 0
MVI B, 0AH ; counter = 10
LXI H, 6000H ; HL points to first data
SUB A ; A = 0 (clear accumulator, clears CY)
BACK: ADD M ; A = A + [HL]
JNC SKIP ; if no carry, skip
INR C ; else increment carry register
SKIP: INX H ; next memory location
DCR B ; decrement counter
JNZ BACK ; repeat until counter = 0
STA 9500H ; store sum
MOV A, C ; move carry to A
STA 9501H ; store carry
HLT ; halt
Explanation
| Instruction | Operation |
|---|---|
MVI C, 00H | Carry counter initialized to zero |
MVI B, 0AH | Loop counter = 10 |
LXI H, 6000H | HL points to first data location |
SUB A | Clears A to 00H |
ADD M | Adds memory content to accumulator |
JNC SKIP | Skip if no carry produced |
INR C | Increment carry register on overflow |
INX H | Advance pointer |
DCR B | Decrement counter |
JNZ BACK | Loop until all 10 added |
STA 9500H | Store final sum |
MOV A, C | Move carry count to A |
STA 9501H | Store carry byte |
HLT | Stop |
Key Points
- Register C accumulates the number of carries, forming the high byte of the 16-bit result. Since each byte is at most FFH, the maximum sum of 10 bytes is $10 \times 255 = 2550 = 09F6H$, so C never exceeds 09H: a single carry register suffices.
SUB Aclears the accumulator and also resets the carry flag before the loop.
Result: Low byte of sum stored at 9500H, carry (high byte) stored at 9501H.
Note: the problem does not explicitly state 9501H for the carry, but "starting from 9500H" naturally implies the next byte (9501H), so that assumption is reasonable and standard.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.