Past exam question: 2081 Chaitra (Regular), Q3
Open in the Padhaiko appQuestion
Write a program in 8085 to transfer 8-bit number from one table to other by setting bit D5 if the number is less than 50 H else transfer the number by resetting bit D6.
Solution
Problem Analysis & Bit Manipulation Logic:
- Source table starting address:
2000H(accessed via HL pointer). - Destination table starting address:
3000H(accessed via DE pointer). - Array length: 10 numbers (loaded into counter register C:
0AH). - Comparison with 50H:
- Load number into accumulator:
MOV A, M. - Compare with 50H:
CPI 50H. - In 8085,
CPI 50Hcomputes(A - 50H)internally without changing A. - If
A < 50H: Carry Flag is set (CY = 1). Jump usingJC LESS_THAN. - If
A >= 50H: Carry Flag is reset (CY = 0).
- Load number into accumulator:
- Bit manipulation operations:
- If Number < 50H: Set Bit D5:
- Bit D5 has binary weight
2^5 = 20H(0010 0000b). - Setting a bit to 1 without altering others is achieved by ORing with a mask containing a 1 at that bit position:
ORI 20H.
- Bit D5 has binary weight
- If Number >= 50H: Reset Bit D6:
- Bit D6 has binary weight
2^6 = 40H(0100 0000b). - Resetting (clearing) a bit to 0 without altering others is achieved by ANDing with a mask containing a 0 at that bit position:
Mask = NOT 40H = BFH(1011 1111b). - Use
ANI BFH.
- Bit D6 has binary weight
- If Number < 50H: Set Bit D5:
Complete 8085 Assembly Language Program:
LXI H, 2000H ; HL = Source table starting pointer
LXI D, 3000H ; DE = Destination table starting pointer
MVI C, 0AH ; C = 10 (loop counter)
LOOP: MOV A, M ; Load 8-bit number from source table into A
CPI 50H ; Compare A with 50H
JC LESS_THAN ; If A < 50H (Carry = 1), branch to set bit D5
; Case: Number >= 50H -> Reset bit D6
ANI BFH ; Reset bit D6 (AND with 1011 1111b)
JMP STORE_DATA
LESS_THAN:
; Case: Number < 50H -> Set bit D5
ORI 20H ; Set bit D5 (OR with 0010 0000b)
STORE_DATA:
STAX D ; Store modified byte at destination [DE]
INX H ; Increment source pointer
INX D ; Increment destination pointer
DCR C ; Decrement loop counter
JNZ LOOP ; Repeat until all 10 numbers are processed
HLT ; Stop program executionExecution Trace for Sample Data:
| Input Byte at 2000H | Decimal | Compare 50H | Operation | Binary Calculation | Stored at 3000H |
|---|---|---|---|---|---|
12H | 18 | < 50H | ORI 20H | 0001 0010b | 0010 0000b = 0011 0010b | 32H |
65H | 101 | >= 50H | ANI BFH | 0110 0101b & 1011 1111b = 0010 0101b | 25H |
50H | 80 | >= 50H | ANI BFH | 0101 0000b & 1011 1111b = 0001 0000b | 10H |
00H | 0 | < 50H | ORI 20H | 0000 0000b | 0010 0000b = 0010 0000b | 20H |
Answer:
The program compares each element with 50H using CPI 50H. If carry is set (A < 50H), it sets bit D5 with ORI 20H; otherwise (A >= 50H), it clears bit D6 with ANI BFH, storing the result at the destination table.