# Flags

Then we have the flags. They're stored in the status register (SR), a 16 bit register. It's divided into two parts, the system byte (bit 15-8) and the user byte (or flag register) (bit 7-0). Here's a description over the whole SR register:
Bits 15 to 0 from most to least significant.

| T| -| S| -| -| I2,1,0 | -| -| -| X| N| Z| V| C| - the status register

* The System byte
Bit 15: T - The trace bit. If it's set an interrupt will be called after each instruction. Often used in debuggers.
Bit 13: S - Supervisor bit. When this bit is set, you have more "access" to some instructions and also to the system byte. The reason for this is that it prevents programs to disturb the OS with some instructions that you shouldn't use if you're not writing an OS. Is enabled when interrupts are generated.
Bit 8-10: The interrupt mask
The I0, I1 and I2 bits of the system register are used to set the interrupt mask: in fact, it means that they are set to an interrupt level: if the trap generated has a level higher than the interrupt mask, then the trap is executed. Otherwise it is ignored. ( ignoring a trap generally means that another interrupt, with a higher priority is being treated )
Here is how these bits are set:

| I2 | I1 | I0 |
level 0 | 0 0 0 | ---------> lowest priority
level 1 | 0 0 1 |
level 2 | 0 1 0 |
level 3 | 0 1 1 |
level 4 | 1 0 0 |
level 5 | 1 0 1 |
level 6 | 1 1 0 |
level 7 | 1 1 1 | ---------> highest priority

Note: #0=%000; #1=%001; #2=%010; etc.
One should refer to the \system.txt file and \lesson\lesson_3.txt for more on interrupts.
* The flag register
- C-flag (Carry). Works as carry is used to work. If you add two 8 bit numbers, the C-flag will be the 9th bit. Also used with shift and rotation.
- V-flag (oVerflow). Will be set if a result can't be represented. For example, when you add $7F and $01, $80 can't be stored since that means -128 in two complement.
- Z-flag (Zero). Set if the result of an operation is zero.
- N-flag (Negative). If the highest bit in the result is set (in two complement it means the sign bit), N will be set.
- X-flag (eXtended). This flag is a copy of the carry-flag, but it won't be changed in all operations where C is changed. This allows you to first make a check (that will set C and X), then some other instructions that will change the C flag but not the X flag, and THEN you can make the branch according to the flags, which means you can use the X flag.

####