Status register: Difference between revisions
Corrected spelling for the plural form of the word meaning four bits, or half a byte: According to my references, either nybbles or nybles are correct, with the later preferred. |
Dmitry Brant (talk | contribs) m Reverted edits by 122.173.24.250 (talk) to last version by Adakiko |
||
(42 intermediate revisions by 26 users not shown) | |||
Line 1: | Line 1: | ||
{{short description|Register containing flags giving additional information concerning a result in a processor}} |
|||
A '''status register''', '''flag register''', or '''condition code register''' is a collection of status [[Flag (computing)|flag]] [[bit]]s for a [[Central processing unit|processor]]. |
A '''status register''', '''flag register''', or '''condition code register''' ('''CCR''') is a collection of status [[Flag (computing)|flag]] [[bit]]s for a [[Central processing unit|processor]]. Examples of such registers include [[FLAGS register (computing)|FLAGS register]] in the [[x86 architecture]], flags in the [[program status word]] (PSW) register in the [[IBM System/360 architecture]] through [[z/Architecture]], and the application program status register (APSR) in the [[ARM Cortex-A]] architecture.<ref>{{Cite web|url=http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473c/Babcfejg.html|title=ARM Information Center|website=infocenter.arm.com|access-date=2019-05-18}}</ref> |
||
The status register is a [[hardware register]] that contains information about the state of the [[Central processing unit|processor]]. Individual bits are implicitly or explicitly read and/or written by the [[machine code]] instructions executing on the processor. The status register lets an instruction take action contingent on the outcome of a previous instruction. |
The status register is a [[hardware register]] that contains information about the state of the [[Central processing unit|processor]]. Individual bits are implicitly or explicitly read and/or written by the [[machine code]] instructions executing on the processor. The status register lets an instruction take action contingent on the outcome of a previous instruction. |
||
Line 28: | Line 29: | ||
| align="center" |'''S''' / '''N''' |
| align="center" |'''S''' / '''N''' |
||
| Sign flag<br/> [[Negative flag]] |
| Sign flag<br/> [[Negative flag]] |
||
| Indicates that the result of a mathematical operation is negative. In some processors,<ref> |
| Indicates that the result of a mathematical operation is negative. In some processors,<ref>{{cite web |title=Toshiba 900 Operation Manual, chap. 3 |url=http://www.semicon.toshiba.co.jp/eng/prd/micro/td/900family/900l/kyoutu/pdf/e_900l_chap3_cpu.pdf |archive-url=https://web.archive.org/web/20060115084555/http://www.semicon.toshiba.co.jp/eng/prd/micro/td/900family/900l/kyoutu/pdf/e_900l_chap3_cpu.pdf |archive-date=2006-01-15 |url-status=dead}}</ref> the N and S flags are distinct with different meanings and usage: One indicates whether the last result was negative whereas the other indicates whether a subtraction or addition has taken place. |
||
|- style="vertical-align:top" |
|- style="vertical-align:top" |
||
| align="center" |'''V''' / '''O''' / '''W''' |
| align="center" |'''V''' / '''O''' / '''W''' |
||
Line 46: | Line 47: | ||
| align="center" |'''H''' / '''A''' / '''DC''' |
| align="center" |'''H''' / '''A''' / '''DC''' |
||
| [[Half-carry flag]]<br/> Auxiliary flag<br/> Digit Carry<br/> Decimal adjust flag |
| [[Half-carry flag]]<br/> Auxiliary flag<br/> Digit Carry<br/> Decimal adjust flag |
||
| Indicates that a bit carry was produced between the |
| Indicates that a bit carry was produced between the [[nibble]]s (typically between the 4-bit halves of a byte operand) as a result of the last arithmetic operation. Such a flag is generally useful for implementing [[binary-coded decimal|BCD]] arithmetic operations on binary hardware. |
||
|- style="vertical-align:top" |
|- style="vertical-align:top" |
||
| align="center" |'''P''' |
| align="center" |'''P''' |
||
Line 54: | Line 55: | ||
| align="center" |'''I''' |
| align="center" |'''I''' |
||
| [[Interrupt flag]] |
| [[Interrupt flag]] |
||
| On some processors, this bit indicates whether interrupts are enabled or masked.<ref>http:// |
| On some processors, this bit indicates whether interrupts are enabled or masked.<ref>{{cite web|url=http://ww1.microchip.com/downloads/en/devicedoc/atmel-8271-8-bit-avr-microcontroller-atmega48a-48pa-88a-88pa-168a-168pa-328-328p_datasheet_complete.pdf|title=Atmel 8-Bit Microcontroller With 4/8/16/32KBytes In-system Programmable Flash - Datasheet|website=[[Microchip Technology]]}}</ref> If the processor has multiple [[interrupt]] priority levels, such as the [[PDP-11 architecture#Interrupts|PDP-11]], several bits may be used to indicate the priority of the current thread, allowing it to be interrupted only by hardware set to a higher priority. On other architectures, a bit may indicate that an interrupt is currently active, and that the current thread is part of an [[interrupt handler]]. |
||
|- style="vertical-align:top" |
|- style="vertical-align:top" |
||
| align="center" |'''S''' |
| align="center" |'''S''' |
||
Line 62: | Line 63: | ||
==CPU architectures without arithmetic flags== |
==CPU architectures without arithmetic flags== |
||
Status flags enable an instruction to act based on the result of a previous instruction. In [[Instruction pipeline|pipelined]] processors, such as [[superscalar]] and [[Speculative execution|speculative]] processors, this can create [[Hazard (computer architecture)|hazards]] that slow processing or require extra hardware to work around them.<ref name="carry-trap">{{cite web|url=http://yarchive.net/comp/carry_bit.html|title=Carry bits; The Architect's Trap|last=Mashey|first=John|date=1996-06-04| |
Status flags enable an instruction to act based on the result of a previous instruction. In [[Instruction pipeline|pipelined]] processors, such as [[superscalar]] and [[Speculative execution|speculative]] processors, this can create [[Hazard (computer architecture)|hazards]] that slow processing or require extra hardware to work around them.<ref name="carry-trap">{{cite web|url=http://yarchive.net/comp/carry_bit.html|title=Carry bits; The Architect's Trap|last=Mashey|first=John|date=1996-06-04|access-date=2013-10-05}}</ref> |
||
Some [[very long instruction word]] processors dispense with the status flags. A single instruction both performs a test and indicates on which outcome of that test to take an action, such as Compare ''a'' with ''b'' and Jump to ''c'' if Equal. The result of the test is not saved for subsequent instructions. |
Some [[very long instruction word]] processors dispense with the status flags. A single instruction both performs a test and indicates on which outcome of that test to take an action, such as Compare ''a'' with ''b'' and Jump to ''c'' if Equal. The result of the test is not saved for subsequent instructions. |
||
Line 68: | Line 69: | ||
Another alternative to the status register is for processor instructions to deposit status information in a general-purpose register when the program requests it. [[MIPS architecture|MIPS]], [[AMD 29000]], [[DEC Alpha]], and [[RISC-V]] are examples of architectures that provide comparison instructions that store the comparison result in a general-purpose register, as a single bit or a numeric value of 0 or 1. Conditional branches act based on the value in the general-purpose register. |
Another alternative to the status register is for processor instructions to deposit status information in a general-purpose register when the program requests it. [[MIPS architecture|MIPS]], [[AMD 29000]], [[DEC Alpha]], and [[RISC-V]] are examples of architectures that provide comparison instructions that store the comparison result in a general-purpose register, as a single bit or a numeric value of 0 or 1. Conditional branches act based on the value in the general-purpose register. |
||
Usually, comparison instructions test equality or signed/unsigned magnitude. To test for other conditions, a program uses an equivalence formula. For example, MIPS has no " |
Usually, comparison instructions test equality or signed/unsigned magnitude. To test for other conditions, a program uses an equivalence formula. For example, MIPS has no "carry bit" but a program performing multiple-word addition can test whether a single-word addition of registers overflowed by testing whether the sum is lower than an operand:<ref name="carry-trap"/> |
||
< |
<syntaxhighlight lang="asm"> |
||
# alow = blow + clow |
# alow = blow + clow |
||
addu alow, blow, clow |
addu alow, blow, clow |
||
Line 77: | Line 78: | ||
addu ahigh, bhigh, chigh |
addu ahigh, bhigh, chigh |
||
addu ahigh, ahigh, tmp |
addu ahigh, ahigh, tmp |
||
</syntaxhighlight> |
|||
</source> |
|||
The <code>sltu</code> instruction sets <code>tmp</code> to 1 or 0 based on the specified comparison of its two other operands. (Here, the general-purpose register <code>tmp</code> is not used as a status register to govern a conditional jump; rather, the possible value of 1, indicating |
The <code>sltu</code> instruction sets <code>tmp</code> to 1 or 0 based on the specified comparison of its two other operands. (Here, the general-purpose register <code>tmp</code> is not used as a status register to govern a conditional jump; rather, the possible value of 1, indicating carry from the low-order addition, is added to the high-order word.) |
||
This scheme becomes less convenient when adding three or more words, as there are two additions when computing <code>b + c + tmp</code>, either of which may generate a carry, which must be detected with two <code>sltu</code> instructions. Fortunately, those two carries may be added to each other without risk of overflow, so the situation stabilizes at five instructions per word added. |
|||
==See also== |
==See also== |
||
Line 84: | Line 87: | ||
* [[CPU flag (x86)]] |
* [[CPU flag (x86)]] |
||
* [[Flag field]] |
* [[Flag field]] |
||
* [[FLAGS register (computing)]] |
|||
==References== |
==References== |
||
{{Reflist}} |
|||
<references/> |
|||
{{DEFAULTSORT:Status Register}} |
{{DEFAULTSORT:Status Register}} |
Latest revision as of 02:24, 20 December 2022
A status register, flag register, or condition code register (CCR) is a collection of status flag bits for a processor. Examples of such registers include FLAGS register in the x86 architecture, flags in the program status word (PSW) register in the IBM System/360 architecture through z/Architecture, and the application program status register (APSR) in the ARM Cortex-A architecture.[1]
The status register is a hardware register that contains information about the state of the processor. Individual bits are implicitly or explicitly read and/or written by the machine code instructions executing on the processor. The status register lets an instruction take action contingent on the outcome of a previous instruction.
Typically, flags in the status register are modified as effects of arithmetic and bit manipulation operations. For example, a Z bit may be set if the result of the operation is zero and cleared if it is nonzero. Other classes of instructions may also modify the flags to indicate status. For example, a string instruction may do so to indicate whether the instruction terminated because it found a match/mismatch or because it found the end of the string. The flags are read by a subsequent conditional instruction so that the specified action (depending on the processor, a jump, call, return, or so on) occurs only if the flags indicate a specified result of the earlier instruction.
Some CPU architectures, such as the MIPS and Alpha, do not use a dedicated flag register. Others do not implicitly set and/or read flags. Such machines either do not pass implicit status information between instructions at all, or they pass it in an explicitly selected general purpose register.
A status register may often have other fields as well, such as more specialized flags, interrupt enable bits, and similar types of information. During an interrupt, the status of the thread currently executing can be preserved (and later recalled) by storing the current value of the status register along with the program counter and other active registers into the machine stack or some other reserved area of memory.
Common flags
[edit]This is a list of the most common CPU status register flags, implemented in almost all modern processors.
Flag | Name | Description |
---|---|---|
Z | Zero flag | Indicates that the result of an arithmetic or logical operation (or, sometimes, a load) was zero. |
C | Carry flag | Enables numbers larger than a single word to be added/subtracted by carrying a binary digit from a less significant word to the least significant bit of a more significant word as needed. It is also used to extend bit shifts and rotates in a similar manner on many processors (sometimes done via a dedicated X flag). |
S / N | Sign flag Negative flag |
Indicates that the result of a mathematical operation is negative. In some processors,[2] the N and S flags are distinct with different meanings and usage: One indicates whether the last result was negative whereas the other indicates whether a subtraction or addition has taken place. |
V / O / W | Overflow flag | Indicates that the signed result of an operation is too large to fit in the register width using two's complement representation. |
Other flags
[edit]On some processors, the status register also contains flags such as these:
Flag | Name | Description |
---|---|---|
H / A / DC | Half-carry flag Auxiliary flag Digit Carry Decimal adjust flag |
Indicates that a bit carry was produced between the nibbles (typically between the 4-bit halves of a byte operand) as a result of the last arithmetic operation. Such a flag is generally useful for implementing BCD arithmetic operations on binary hardware. |
P | Parity flag | Indicates whether the number of set bits of the last result is odd or even. |
I | Interrupt flag | On some processors, this bit indicates whether interrupts are enabled or masked.[3] If the processor has multiple interrupt priority levels, such as the PDP-11, several bits may be used to indicate the priority of the current thread, allowing it to be interrupted only by hardware set to a higher priority. On other architectures, a bit may indicate that an interrupt is currently active, and that the current thread is part of an interrupt handler. |
S | Supervisor flag | On processors that provide two or more protection rings, one or more bits in the status register indicate the ring of the current thread (how trusted it is, or whether it must use the operating system for requests that could hinder other threads). On a processor with only two rings, a single bit may distinguish Supervisor from User mode. |
CPU architectures without arithmetic flags
[edit]Status flags enable an instruction to act based on the result of a previous instruction. In pipelined processors, such as superscalar and speculative processors, this can create hazards that slow processing or require extra hardware to work around them.[4]
Some very long instruction word processors dispense with the status flags. A single instruction both performs a test and indicates on which outcome of that test to take an action, such as Compare a with b and Jump to c if Equal. The result of the test is not saved for subsequent instructions.
Another alternative to the status register is for processor instructions to deposit status information in a general-purpose register when the program requests it. MIPS, AMD 29000, DEC Alpha, and RISC-V are examples of architectures that provide comparison instructions that store the comparison result in a general-purpose register, as a single bit or a numeric value of 0 or 1. Conditional branches act based on the value in the general-purpose register.
Usually, comparison instructions test equality or signed/unsigned magnitude. To test for other conditions, a program uses an equivalence formula. For example, MIPS has no "carry bit" but a program performing multiple-word addition can test whether a single-word addition of registers overflowed by testing whether the sum is lower than an operand:[4]
# alow = blow + clow
addu alow, blow, clow
# set tmp = 1 if alow < clow, else 0
sltu tmp, alow, clow
addu ahigh, bhigh, chigh
addu ahigh, ahigh, tmp
The sltu
instruction sets tmp
to 1 or 0 based on the specified comparison of its two other operands. (Here, the general-purpose register tmp
is not used as a status register to govern a conditional jump; rather, the possible value of 1, indicating carry from the low-order addition, is added to the high-order word.)
This scheme becomes less convenient when adding three or more words, as there are two additions when computing b + c + tmp
, either of which may generate a carry, which must be detected with two sltu
instructions. Fortunately, those two carries may be added to each other without risk of overflow, so the situation stabilizes at five instructions per word added.
See also
[edit]References
[edit]- ^ "ARM Information Center". infocenter.arm.com. Retrieved 2019-05-18.
- ^ "Toshiba 900 Operation Manual, chap. 3" (PDF). Archived from the original (PDF) on 2006-01-15.
- ^ "Atmel 8-Bit Microcontroller With 4/8/16/32KBytes In-system Programmable Flash - Datasheet" (PDF). Microchip Technology.
- ^ a b Mashey, John (1996-06-04). "Carry bits; The Architect's Trap". Retrieved 2013-10-05.