In school I was accustomed to programming various registers when working with microcontrollers to manipulate their behavior. At my job registers are now referred to purely as bit fields. The word "register" is only used to refer to a hex value, which I'm guessing is the address location. The words "bit fields" are used to indicate the portion of the register which pertains to a certain functionality of our product. Looking at the Atmel documentation here (page 3) they indicate that 2 smaller registers make up one larger register. This wiki document indicates that bits are referred to flags. I guess I am confused on the nomenclature. Is there an industry standard for defining what the differences between the terms are or when they should be used?

  • 2
    \$\begingroup\$ 'At my job registers are now referred to purely as bit fields. The word "register" is only used to refer to a hex value' That's the only part that doesn't make sense, and may very well be a habit unique to your company. \$\endgroup\$ – Chris Stratton Aug 14 '12 at 21:00
  • \$\begingroup\$ @Chris Stratton - Thank you Chris, please post this as an answer so I can accept it. \$\endgroup\$ – thomas.cloud Aug 14 '12 at 21:53

I believe you're referring to this drawing:

enter image description here

The register file is an array of 32 general purpose registers which resides in the AVR's CPU. The registers are tightly coupled with the ALU (Arithmetic and Logic Unit) so that operations on the register data can be performed much faster than data which has to be fetched from (external) RAM. A microcontroller will have special addressing modes for working with the registers. If you want to add 5 to a variable in RAM the instruction will have to include the RAM address, for which it may need 16 bit. Since the register file is only 32 bytes long you only need 5 bit to address a register, and the instruction set is designed in such a way that these 5 bit will fit together with the instruction's opcode in a 16-bit instruction. So no second fetch for the address is required.

That's what the drawing shows: the instruction is 16 bit wide, and 5 bit of that is the register designator. They even went a step further. There are instructions which operate on two registers, and again they constructed the instructions in such a way that the two register designators fit in the 16-bit instruction, so again only 1 fetch is required.

enter image description here

So it doesn't show two registers combined into one of double length, although there are microcontrollers which allow this.

Flags are single bit data which indicate some status: on or off, true of false.

enter image description here

This is the programming model of the good old MC6809 (a beautiful microprocessor, which more than 30 years ago was way ahead of its time). The condition code register consists of 8 flags, with names like "zero", "carry", "overflow", "IRQ mask", etc. You wouldn't access these bits directly like another register, but use them for conditional branch instructions, like BEQ, for "branch if equal", which will branch if the "zero" flag is set.

But programmers needed much more flags in their programs. So they assigned bytes for the task, and they would address individual bits by masking the others by AND-ing the byte with a bit mask.

bit fields
This was a bit clumsy, and especially with higher level language something more advances was needed. C lets you define bit fields which you can address directly in your code:

struct packed_struct {
     unsigned int f1:1;
     unsigned int f2:1;
     unsigned int f3:1;
     unsigned int f4:1;
     unsigned int type:4;
     unsigned int funny_int:9;
} pack;

Note that you can't just define single bit fields, but also groups of several bits, like 4 bit and 9 bit in the example.

bit banding
Internally the C code will still be compiled into code that uses the masks to get at 1 particular bit, so it's still not very efficient. The ARM Cortex-M3 has a solution for this. It has a 4 GB addressing space, most of which isn't used. So they use part of it for what's called bit banding.

enter image description here

It means that parts of memory and I/O have copies at 1 bit per address elsewhere in memory. The 1MB SRAM bit band region holds 32 Mb, and each of these is mapped to a 32 MB address in the bit banding block. So masking is no longer required.

Further reading
LPC17xx User Manual at NXP.

  • \$\begingroup\$ Great explanation, thanks for elucidating all of the differences. \$\endgroup\$ – thomas.cloud Aug 15 '12 at 14:45
  • \$\begingroup\$ @thomas - no problem, the pleasure is mine. Thanks for the accept. \$\endgroup\$ – stevenvh Aug 15 '12 at 14:46

A register is typically one word (i.e. could be 8, 16, 32, 64 bits) in length and is used as a handy data location for the CPU. It's typically the data location that can be accessed the fastest by the CPU. Some architectures declare all the registers to be the same and others (e.g. Intel) have different registers that are supposed to be for certain operations (e.g. the AX register is for accumulating values after a math operation). Some architectures do not use registers at all (they use only the stack... notable examples are the RS600 and the JVM).

A Flag is a data location similar to a register but is used differently. Rather than the CPU using all (or sometimes half) of the register, only one bit at a time is used when a certain event occurs (e.g. addition overflow).

A bit field is a programming construct and (to the best of my knowledge) isn't part of the CPU architecture. It's a useful way to contruct one word values that you can push to a register. Think of it as an array that let's you access each bit of the word.

  • 2
    \$\begingroup\$ "bit field" is kind of an abstract term. It definitely applies to programming constructs as you say, but it can crop up in CPU architecture; sometimes you will see groups of bits within an instruction referred to as a bit field. Such a group might indicate an addressing mode, or identify a register operand, for example. Really it's any set of bits that have meaning when considered as a group. But +1 \$\endgroup\$ – JustJeff Aug 15 '12 at 0:39

A register can either mean a CPU register, or register for I/O and hardware control.

CPU registers are for storing values that are part of the current computation. They can be accessed very quickly, but there are only a few of them.

Hardware registers are typically memory-mapped (though for x86 there is a separate I/O address space for them), so they are accessed like RAM at a certain address (the address might be the hex value you mention). Contrary to RAM they don't just store data, rather you should think of a read or write access as an action on the hardware, depending on the register (read it up in the data sheet).

Often such hardware registers contain more than one item. Such an item may be a single bit, in this case it is often called a flag. A bitfield is a unit consists of several bits, either for storing a small number or for choosing among more than two alternatives.

The bit fields in C allow to pack several small numbers into a machine word. But beware, the order is implementation dependent, so most of the time it is not a good idea to use C bit fields to access bit fields in registers. Better use shifting and masking. (It is OK to use C bit fields if the compiler/hardware combination explicitly supports them, though it may still cause portability problems to other compilers.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.