📦 Register Organization
Understand the different types of CPU registers, their purposes, and how they work together to execute instructions efficiently.
What are CPU Registers?
Fastest Memory in the System
CPU registers are categorized into two broad types: programmer-visible (general-purpose and special-purpose) and internal (temporary storage used during execution).
Types of Registers
Status Register / Flags
Condition Code Register (CCR)
Z (Zero)
Set when ALU result is zero
C (Carry)
Set when addition produces a carry out or subtraction needs a borrow
N (Negative)
Set when ALU result is negative (MSB = 1)
V (Overflow)
Set when signed arithmetic overflows
Register File Architecture
MIPS Register File (32 × 32-bit)
Register Convention
Register Windows (SPARC Architecture)
SPARC Innovation
N
Number of windows (typically 8-32)
M
Number of global registers (typically 8-16)
C
Registers per window (typically 24-32)
Overlapping
Adjacent windows share registers for parameter passing
Register Usage Code Example
mips
# MIPS register usage example
# Function: sum array elements
# a0 = array pointer, a1 = count
# v0 = return value (sum)
sum_array:
li $v0, 0 # sum = 0
li $t0, 0 # i = 0
loop:
beq $t0, $a1, done # if i == count, done
sll $t1, $t0, 2 # t1 = i * 4 (byte offset)
add $t2, $a0, $t1 # t2 = &array[i]
lw $t3, 0($t2) # t3 = array[i]
add $v0, $v0, $t3 # sum += array[i]
addi $t0, $t0, 1 # i++
j loop
done:
jr $ra # return to callerInterview Questions
What is the difference between general-purpose and special-purpose registers?
General-purpose registers (GPRs) can hold either data or addresses and are used by arithmetic/logic instructions. Special-purpose registers like PC, IR, MAR, and MDR have dedicated roles: PC tracks the next instruction, IR holds the current instruction, MAR holds memory addresses, and MDR buffers data to/from memory.
Explain register windows in SPARC architecture.
Register windows allow fast context switching by providing multiple overlapping sets of registers. Each window has local registers and shares registers with adjacent windows for parameter passing. The current window pointer (CWP) selects the active window. On function call, the CWP rotates to a new window without saving/loading registers to memory, unless all windows are in use (window overflow trap).
How does the Stack Pointer (SP) work during function calls?
SP points to the most recently pushed item on the stack. On function call: arguments may be pushed, return address is saved, old frame pointer is saved, local variables are allocated (SP decremented). On return: stack is unwound (SP restored), return address loaded, and execution resumes.
What are condition code flags and how are they used?
Flags like Zero (Z), Carry (C), Negative (N), and Overflow (V) are set by ALU operations and stored in the status register. Conditional branch instructions (e.g., BEQ, BNE) check these flags to make decisions. For example, BEQ branches if Z=1 (last result was zero).