GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 11(b)

General behavior of the BCD difference program

Computer Organization & Architecture · Data Representation & Arithmetic · Full GATE CSE 2000 paper

GATE 2000, Q11(b) Descriptive
Report issue

Data Representation & Arithmetic

Question 11(b), GATE 2000: B and C contain packed-BCD values 00…99. Program: S1: MVI A,99H; MVI D,00H; SUB C; ADD B; DAA; S2: JC S3; MOV E,A; MVI A,99H; SUB E; MOV E,A; JZ S4; MVI D,FFH; JMP S4; S3: INR A; DAA; MOV E,A; S4: … . GFG prints INR A; GateForum prints INC A, which is not an 8085 mnemonic. Interpret the latter as INR for the architectural analysis.

Task: Express D and E at S4 as functions of the decimal values b,c represented by B,C.
Question 11(b), GATE 2000: B and C contain packed-BCD values 00…99. Program: S1: MVI A,99H; MVI D,00H; SUB C; ADD B; DAA; S2: JC S3; MOV E,A; MVI A,99H; SUB E; MOV E,A; JZ S4; MVI D,FFH; JMP S4; S3: INR A; DAA; MOV E,A; S4: … . GFG prints INR A; GateForum prints INC A, which is not an 8085 mnemonic. Interpret the latter as INR for the architectural analysis.

Task: Express D and E at S4 as functions of the decimal values b,c represented by B,C.
Tap to enlarge
Shared context

B and C contain packed-BCD values 00…99. Program: S1: MVI A,99H; MVI D,00H; SUB C; ADD B; DAA; S2: JC S3; MOV E,A; MVI A,99H; SUB E; MOV E,A; JZ S4; MVI D,FFH; JMP S4; S3: INR A; DAA; MOV E,A; S4: … . Interpret the latter as INR for the architectural analysis.

Answer & solution

Answer: If b≤c: E=BCD(c−b), D=FFH for b<c and 00H for equality. If b>c: D=00H, literal E=BCD(b−c)+60H (byte arithmetic), not the intended BCD(b−c). (no official key was published)

1. Define enc(t)=16⌊t/10⌋+(t mod 10) for 0≤t≤99. Since each digit of 99 exceeds or equals the corresponding digit of C, 99H−C is the packed-BCD nine’s complement enc(99−c).

2. ADD B followed by DAA computes 99−c+b. If b>c its decimal value is 100…198, so CY=1 and A=enc(b−c−1). If b≤c, CY=0 and A=enc(99−(c−b)).

3. When b≤c, the no-carry path subtracts this BCD nine’s complement from 99H, giving E=enc(c−b). Zero leaves D=00H; otherwise D becomes FFH. This branch correctly implements zero or a negative signed magnitude.

4. When b>c let d=b−c∈[1,99]. INR changes enc(d−1) by one and leaves CY=1. DAA performs the required low-digit correction, but also adds 60H because carry was preserved. Therefore the literal E is enc(d)+60H (mod 256), with D=00H. For example d=19 gives 79H and d=40 gives A0H, even an invalid BCD digit.

5. The intended function is D=00H if b≥c else FFH, E=enc(|b−c|). It becomes correct if S3 uses ADI 01H instead of INR A: the ordinary addition resets carry appropriately before the final DAA. Do not label the intended function as the unmodified program’s actual behavior.

Original paper, page 8