IT lexicon › Hardware › mepc

mepc

Hardware På svenska → Updated: 2026-11-04

Where RISC-V stashes the program counter when a trap takes over — the address mret later jumps back to.

On an exception, mepc points at the instruction that caused it; on an interrupt, at the instruction that has not yet run. The difference matters. After an interrupt, execution should resume exactly where it left off, but after an ecall the register still points at the call itself. The handler has to add 4 before returning, or the same system call runs again, and again, forever.

There is only one of it: if nested interrupts are allowed, the handler must save mepc (and mstatus) to the stack before re-enabling interrupts. Cortex-M does that in hardware; RISC-V leaves it to software, or to the CLIC.

← Back to the lexicon