Consider this embedded C code snippet that enables global interrupts. What will be the value of the status variable after execution?
volatile unsigned char status = 0; void enable_interrupts() { __asm__("sei"); // Set Interrupt Enable status = 1; } int main() { enable_interrupts(); return status; }
The sei assembly instruction sets the global interrupt enable flag. The status variable is set right after.
The sei instruction enables interrupts, but it does not affect the status variable. Since status = 1; is executed immediately after, the value of status is 1.
What error will this code produce when compiled?
void disable_interrupts() {
__asm__("cli"); // Missing semicolon fixed
}
Check the syntax carefully after the inline assembly statement.
The missing semicolon after the inline assembly causes a compilation error expecting a semicolon before the closing brace.
Which of the following statements about enabling interrupts in embedded C is correct?
Recall what sei and cli instructions do.
The sei instruction sets the global interrupt enable flag, enabling interrupts immediately. cli disables interrupts. Interrupts can be enabled anywhere in code, not just main.
flag after this interrupt disable sequence?Given this code snippet, what is the value of flag after disable_interrupts() is called?
volatile int flag = 0; void disable_interrupts() { __asm__("cli"); flag = 42; } int main() { disable_interrupts(); return flag; }
The cli instruction disables interrupts, but does not affect variable assignments.
The cli disables interrupts, but the assignment flag = 42; executes normally. So flag becomes 42.
You want to temporarily disable interrupts, perform a critical task, then restore the previous interrupt state. Which code snippet correctly does this?
Think about saving and restoring the interrupt state to avoid unintended side effects.
Option B saves the status register (SREG) which holds the interrupt flag, disables interrupts with cli(), runs critical code, then restores the original state. This preserves the previous interrupt state safely.
Option B always enables interrupts at the end, which may not restore the original state. Option B enables interrupts before disabling them, which is incorrect. Option B enables interrupts before critical code, which defeats the purpose.