ALU 101 Overflow and Error Handling 1 — Questions and Answers
Question 1: What is integer overflow in the context of ALU operations?
- When an operand is too small for the ALU
- When an arithmetic result exceeds the maximum value that can be stored in the destination register (Correct answer)
- When the clock frequency is too high
- When the carry flag is not set
Correct answer: When an arithmetic result exceeds the maximum value that can be stored in the destination register
Integer overflow occurs when a computation produces a value larger (or smaller, for negative) than the register's representable range, causing an incorrect result.
Question 2: In two's complement addition, how can you detect signed overflow using the carry-in and carry-out of the MSB?
- Overflow = carry-in XOR carry-out of the MSB (Correct answer)
- Overflow = carry-in AND carry-out
- Overflow = NOT carry-out
- Overflow = carry-in OR carry-out
Correct answer: Overflow = carry-in XOR carry-out of the MSB
Signed overflow is detected when the carry into the sign bit differs from the carry out of the sign bit, an XOR of those two carry values.
Question 3: What distinguishes unsigned overflow (carry) from signed overflow in ALU results?
- They are the same condition
- Unsigned overflow (CF) means the result exceeded the unsigned range; signed overflow (OF) means it exceeded the two's complement range (Correct answer)
- Unsigned overflow only occurs in subtraction
- Signed overflow sets the carry flag
Correct answer: Unsigned overflow (CF) means the result exceeded the unsigned range; signed overflow (OF) means it exceeded the two's complement range
CF and OF are separate flags: CF indicates the result doesn't fit in the unsigned range, while OF indicates it doesn't fit in the signed range for the same bit width.
Question 4: What happens in most hardware ALUs when integer overflow is not handled explicitly?
- The CPU halts immediately
- The result wraps around modulo 2^n, potentially producing an incorrect silent error (Correct answer)
- The overflow is automatically corrected
- The instruction is retried
Correct answer: The result wraps around modulo 2^n, potentially producing an incorrect silent error
Without overflow trapping, the ALU simply truncates the result to n bits, wrapping around, and the overflow flag is set but often ignored by software.
Question 5: Which programming practice helps prevent integer overflow errors when using ALU results in software?
- Using only unsigned integers
- Checking operand ranges before the operation or using overflow-checking arithmetic libraries (Correct answer)
- Increasing the clock speed
- Storing results in floating-point registers
Correct answer: Checking operand ranges before the operation or using overflow-checking arithmetic libraries
Pre-validating that operands won't cause overflow, or using language-level overflow-checked arithmetic, prevents silent data corruption.
Question 6: What is an arithmetic exception trap in the context of ALU error handling?
- A hardware interrupt triggered when the ALU detects an error condition such as overflow or division by zero (Correct answer)
- A software subroutine that performs division
- A pipeline flush for branch misprediction
- A memory access fault
Correct answer: A hardware interrupt triggered when the ALU detects an error condition such as overflow or division by zero
Many CPUs can be configured to trigger an interrupt (trap) when the ALU sets certain flag conditions, allowing the OS or runtime to handle arithmetic errors gracefully.
What is integer overflow in the context of ALU operations?