GATE 2003 Question | CAO PYQs
This lecture covers a GATE 2003 question on assembly language programming, explaining how a hypothetical processor's code uses flags, compare instructions, and rotate-right-through-carry operations. The instructor walks through the full code execution to show the program counts the number of 1-bits in register A. Two interlinked questions from the same code snippet are solved.
Summary
The lecture begins with an introduction to a GATE 2003 assembly language question involving a hypothetical processor with three 8-bit registers: A, B, and C. The instructor explains that this is the final lecture on assembly language code practice in the 'Nirbhav Series,' which focuses only on important topics in Computer Architecture rather than covering the full course.
Before diving into the code, the instructor takes a detour to explain processor flags. Flags are described as 1-bit memory cells implemented using flip-flops, collectively stored in a Status Register (also called the Flag Register or PSW — Program Status Word). The ALU produces two outputs: the actual result and the status of that result. Key flags discussed include the Zero Flag (set when result is zero), Sign Flag (set when result is negative), Carry Flag (set when a final carry is generated), Auxiliary Carry Flag (used in BCD arithmetic), and Parity Flag. The instructor clarifies that in the 8085 microprocessor, only 5 of the 8 bits in the status register are used as flags, with 3 bits left unused.
The CMP (Compare) instruction is then explained: it performs subtraction of the two operands internally through the ALU and sets flags accordingly. If the Zero Flag is set, the two operands are equal. If the Sign Flag is set, the source operand is greater than the destination.
The instructor then traces through the assembly code step by step. Register B is initialized to 0, register C to 8. The code enters a loop at label Z: it compares C with 0, and if equal (Zero Flag set), jumps to label X — ending the loop. Otherwise, it decrements C by 1 and performs a Rotate Right Through Carry on register A by 1 bit (RRC A, #1). If the Carry Flag is set after the rotation (meaning the LSB of A was 1), it jumps to label Y where B is incremented by 1, then unconditionally jumps back to Z. If Carry is not set, it jumps directly back to Z without incrementing B.
The instructor explains that RRC moves the least significant bit (LSB) of the register into the Carry Flag each iteration. Since C starts at 8 and decrements to 1 before the loop exits (running 8 times), all 8 bits of register A are individually examined through the Carry Flag. B is incremented only when a bit is 1. Therefore, after program execution, B contains the count of 1-bits in the initial value of register A — answering Question 1 (option B).
For Question 2, the instructor asks what instruction placed at label X would restore register A to its original value after execution. Since RRC operates on a 9-bit circular shift (8 bits of register A + 1 carry bit), 8 rotations (as performed by the loop) do NOT return to the original value — 9 rotations are needed. Therefore, placing one additional RRC A, #1 instruction at location X ensures the register A returns to its initial value. The answer is option A: RRC A, #1. The instructor explicitly warns against choosing NOP (no operation), which would be correct only for a plain 8-bit rotation without the carry bit.
Key Insights
- The instructor explains that the ALU produces two outputs — the actual computation result and the status of that result — and the status is stored in the Status Register (Flag Register/PSW), which is made up of flip-flops acting as 1-bit memory cells.
- The instructor clarifies that in the 8085 microprocessor, the 8-bit status register contains only 5 active flags (Carry, Parity, Auxiliary Carry, Zero, Sign) with 3 bits unused, warning students not to assume 8 flags just because the register is 8 bits wide.
- The instructor demonstrates that Rotate Right Through Carry (RRC) on an 8-bit register effectively operates as a 9-bit circular shift (8 register bits + 1 carry flag bit), meaning 9 rotations — not 8 — are needed to restore the original register value.
- The instructor reveals that the assembly program's core function is counting the number of 1-bits in register A: each RRC operation shifts the LSB into the Carry Flag, and register B is incremented only when the Carry Flag is 1, effectively counting all set bits over 8 loop iterations.
- The instructor draws a direct parallel between the assembly CMP instruction behavior (returning zero when two values are equal) and the C language `strcmp` function, which also returns 0 when two strings are equal, to help students remember the concept through cross-subject linking.
Topics
Full transcript available for MurmurCast members
Sign Up to Access