REP Instruction (Reset Processor Status Flag)

Clears the specified status flags.

REP Overloads

OpcodeSyntaxAddressing modeBytesCyclesNotes
C2REP #constImm23

Flag changes

nvmxdizc

nvmxdizc: Flags specified by the operand are cleared; the others are unchanged.

Explanation

The REP instruction clears the status flags corresponding to the bits that are 1 in the value given as the operand.
This is the only instruction that directly clears the memory mode flag and the index flag (they also change when restored from the stack by PLP or RTI). It is therefore normally used to change register widths.
Note that in emulation mode the memory mode flag and the index flag are always 1 and cannot be cleared with this instruction.

Examples

REP and SEP are used to switch register widths in the middle of a routine.

    REP #$20        ; make A 16-bit
    LDA.w #$1234
    STA $00         ; $34 is written to $00 and $12 to $01
    SEP #$20        ; switch A back to 8-bit
    LDA.b #$56
    STA $02         ; $56 is written to $02
    REP #$30        ; make A, X and Y 16-bit
    SEP #$20        ; switch only A back to 8-bit (X and Y stay 16-bit)

The byte count of instructions that use an immediate value (#) depends on the m and x flags at run time. Many assemblers do not track the flag changes made by REP and SEP, so you need to state the immediate size explicitly with .b or .w as in the example. If the flag state and the immediate size disagree, the CPU misreads where instructions begin and end, and the rest of the program will not run correctly.

See also

Flag instructions
CLC Instruction
CLD Instruction
CLI Instruction
CLV Instruction
SEP Instruction
SEC Instruction
SED Instruction
SEI Instruction
XCE Instruction