IAE Academy

FOR CORE AND IT JOBS · CHAPTER 2 OF 7

Embedded C: registers, bits and memory

This chapter shows how C code talks to a microcontroller's hardware: fixed-width integers, registers reached through pointers, the volatile keyword and bit masks. It then covers the limits of a small memory: flash data, the stack, overflow and fixed-point numbers. You need it to read and write register code.

Updated 30 Oct 2026

What you will be able to do

You will choose an integer type whose size you know, and reach a peripheral register through a pointer to its address. You will explain why volatile is needed and set, clear, toggle and test any bit, and read a multi-bit field, working in hexadecimal. You will also place constant tables in flash and say what a small stack and a fixed-width integer can hold.

Before you start

  1. Chapter 1, What a microcontroller is made of (flash, RAM, peripherals on internal buses, word size)

  2. Binary and hexadecimal numbers: one hex digit is four bits

  3. The C basics of variables, functions, pointers, casts and #define

  4. Bitwise AND, OR, XOR, NOT and the shift operators << and >> in C

  5. Powers of 2, and integer division with floor and remainder (mod)

Types and registers at fixed addresses

In C, int has no fixed size. The language only promises at least 16 bits, and the compiler picks the width to suit the CPU: often 16 bits on a typical 8-bit part and 32 bits on a 32-bit part. Code written with int can therefore change behaviour when it moves to another part. A register is exactly as wide as the hardware made it, so code must say the width. The header stdint.h gives names whose width is part of the name.

Type

Bits

Range

uint8_t

8

0 to 255

int8_t

8

-128 to 127

uint16_t

16

0 to 65535

uint32_t

32

0 to 4294967295

On a typical microcontroller each peripheral register has its own address in the same address space as RAM. C reaches it by turning that address into a pointer and dereferencing it. Take two made-up 8-bit registers: an output register at 0x40, and a status register at 0x41 whose bit 0 the hardware sets when it is ready:

#include <stdint.h>

#define PORT_OUT (*(volatile uint8_t *)0x40)
#define STATUS   (*(volatile uint8_t *)0x41)

void start(void)
{
    PORT_OUT = 0x01;                  /* a write that drives pins */
    while ((STATUS & 0x01) == 0) { }  /* wait for hardware */
}

The addresses are illustrative; a real part's manual gives the real ones.

Now the bug. Without volatile, the compiler may assume STATUS changes only when this code writes it. It can then read STATUS once, keep the value in a CPU register and loop forever. It may also delete the first of two writes in a row to PORT_OUT, keeping only the last. volatile tells the compiler that every read and write of the object is an event the hardware can see or cause. So it performs each one as written, in program order with the other volatile accesses.

WATCH OUT FOR

volatile uint8_t *p makes the register volatile; uint8_t *volatile p makes only the pointer variable volatile. Test: ask what the hardware changes. It changes the register, so volatile goes before the star.

Bit masks and fields

A control register packs many one-bit switches and small fields into one register. You change one switch without disturbing the others by combining the register with a mask: a value with 1 only in the bits you mean. Bit 0 is the least significant bit. Each operation below follows from how OR, AND and XOR treat a single bit.

MASK FOR BIT N

Holds for in an -bit register; is C's << shift. is 1 in bit and 0 elsewhere, since each one-place left shift multiplies by 2. Example: .

SET A BIT

is the register value, the mask, is C's bitwise OR, and means the result is written back into , as C's = does. OR with 1 gives 1 whatever the bit was; OR with 0 leaves it. So bits where is 1 become 1 and the rest stay.

CLEAR A BIT

is C's ~ (bitwise NOT) and is C's AND. is 0 where you want to clear; AND with 0 gives 0 and AND with 1 leaves the bit. In code write ~M rather than a typed-out constant such as 0xF7, so the mask fits the register's width, for registers no wider than unsigned int.

TOGGLE A BIT

is C's ^ (XOR). XOR with 1 flips a bit; XOR with 0 leaves it. Applying it twice restores .

TEST A BIT

Holds for a mask with a single 1; means "if and only if". AND keeps only the masked bit, so the result is non-zero exactly when that bit is 1. Compare with zero, not with 1: for the result is 0 or 8.

READ A FIELD

Holds for an unsigned -bit with ; is C's >> shift. The field is bits wide with its lowest bit at position . Shifting right by moves it to bit 0, and is ones, which removes the bits above the field.

In C each operation is one compound assignment on the register, here for bit 3 of a volatile register REG (statements inside a function):

REG |= (1u << 3);              /* set bit 3 */
REG &= ~(1u << 3);             /* clear bit 3 */
REG ^= (1u << 3);              /* toggle bit 3 */
if ((REG & (1u << 3)) != 0) { } /* test bit 3 */

The first three lines each read the register, change the value and write it back; the test only reads. Write 1u, not 1: 1 is a signed int, and on a 32-bit int 1 << 31 shifts into the sign bit, which C leaves undefined. Writing 1u for every mask keeps bit 31 as safe as bit 3.

WORKED EXAMPLE

A register holds R = 0x2C. Find the value after each change, applied to 0x2C each time: set bit 0, clear bit 3, toggle bit 5. Then test bit 2.

Solution
  1. Write 0x2C in binary, one hex digit per four bits: 0010 1100. So bits 5, 3 and 2 are 1.

  2. Set bit 0: the mask is 0x01. 0x2C | 0x01 = 0010 1101 = 0x2D. Bit 0 was 0, so it becomes 1.

  3. Clear bit 3: the mask is 0x08, so = 0xF7 in 8 bits. 0x2C & 0xF7 = 0010 0100 = 0x24. The 0 in 0xF7 clears bit 3 and the ones keep the rest.

  4. Toggle bit 5: the mask is 0x20. 0x2C ^ 0x20 = 0000 1100 = 0x0C. Bit 5 was 1, so it flips to 0.

  5. Test bit 2: the mask is 0x04. 0x2C & 0x04 = 0x04, which is not zero, so bit 2 is set.

Answer: 0x2D, 0x24 and 0x0C; bit 2 is set.

WORKED EXAMPLE

A control register holds 0xB5. Bits 6:4 (bits 6 down to 4) form a 3-bit mode field. Read it, then change the mode to 5 and leave every other bit alone.

Solution
  1. Write 0xB5 as 1011 0101. Bits 6:4 are 011, so the field has and .

  2. Read: . , and 0x0B & 0x07 = 0x03. The mode is 3.

  3. To write 5, first clear the whole field. Its mask is 0x07 << 4 = 0x70, so = 0x8F. 0xB5 & 0x8F = 1000 0101 = 0x85. Clearing first matters because OR alone cannot turn a 1 into a 0.

  4. Place the new value: 5 << 4 = 0x50. Now OR it in: 0x85 | 0x50 = 1101 0101 = 0xD5.

  5. Check: in 1101 0101, bits 6:4 are 101 = 5, and bits 7 and 3:0 are unchanged.

Answer: The mode is 3; after the change the register is 0xD5.

Small memory: flash, stack, overflow

RAM on a small part is often a few kilobytes, so tables that never change belong in flash. On a typical 32-bit Cortex-M part, a file-scope object declared const is placed in flash by the toolchain and read at its address:

const uint8_t lookup[4] = {0, 90, 127, 90};

On a typical 8-bit Harvard part, flash sits in a separate address space, and the compiler may need a special keyword to read it, so check the toolchain manual.

The stack holds the return address, saved registers and local variables of every call still in progress. On a typical small part it shares RAM with your global variables and grows toward them, and by default nothing checks its size: when it grows too far it silently overwrites data. Recursion is risky because its depth can depend on the input, so its worst case is hard to bound. A large local array is risky because the whole array sits on the stack while its function runs. If each call takes bytes and bytes are free for the stack, the depth is at most calls.

WRAP-AROUND OF UNSIGNED INTEGERS

Holds when any integer result is stored in an unsigned type of bits: the stored value lies from 0 to , with no warning. So 3 - 5 stored in a uint8_t gives 254. C first converts uint8_t operands to int (integer promotion), so the sum is formed in int and wraps only when stored. Overflow in signed arithmetic, such as int, is undefined: C gives it no result, so never rely on it.

FIXED-POINT VALUE

The integer stands for the real value with bits after the binary point, so the step is . needs a type wide enough for its integer and fraction bits: with , 1.5 is stored as , which needs 16 bits. Values with the same add directly. A product carries fraction bits, so form it in a wider type and shift right by .

IN ONE LINE

For a fixed number of bits, fixed-point trades range for resolution: more fraction bits give finer steps but a smaller maximum.

Check yourself

CHECK YOURSELF 1

A volatile 8-bit register holds 0x5A. You run REG &= ~(1u << 4); What does the register hold afterwards?

  1. (A)

    0x4A

  2. (B)

    0x5B

  3. (C)

    0x1A

  4. (D)

    0x4B

Answer and solution

Answer: (A)

  1. Write 0x5A in binary: 0101 1010. Bit 4 is the 1 in the upper nibble's lowest place.

  2. The mask is 1u << 4 = 0x10, so ~M in 8 bits is 0xEF.

  3. 0x5A & 0xEF = 0101 1010 & 1110 1111 = 0100 1010.

  4. 0100 1010 is 0x4A. Only bit 4 changed, from 1 to 0.

CHECK YOURSELF 2One or more options are correct

Which statements about volatile are true? Choose all that apply.

  1. (A)

    Without volatile, the compiler may read a status register once and loop forever on the saved value.

  2. (B)

    volatile uint8_t *p makes the register that p points to volatile.

  3. (C)

    volatile makes the statement REG |= mask one indivisible step.

  4. (D)

    Without volatile, the compiler may delete the first of two writes in a row to the same output register.

Answer and solution

Answer: (A), (B), (D)

  1. A is true: with no volatile the compiler may assume the register changes only when this code writes it, so it can keep one read in a CPU register.

  2. B is true: volatile before the star qualifies the object pointed to, which is the register.

  3. C is false: REG |= mask still reads the register, changes the value and writes it back as separate steps.

  4. D is true: without volatile the compiler may keep only the last of two writes in a row.

  5. So the correct choices are A, B and D.

CHECK YOURSELF 3

A control register holds R = 0xE6. Bits 6:3 (bits 6 down to 3) form a field. Using F = (R >> s) & (2^w - 1), what is the value of the field, as a decimal number?

Answer and solution

Answer:

  1. Write 0xE6 in binary: 1110 0110. Bit 7 is 1, bit 6 is 1, bit 5 is 1, bit 4 is 0, bit 3 is 0, then bits 2, 1, 0 are 1, 1, 0.

  2. The field has its lowest bit at position s = 3 and is w = 4 bits wide (bits 6, 5, 4, 3).

  3. The mask is 2^4 - 1 = 15 = 0x0F. 0xE6 >> 3 = 1 1100 = 0x1C.

  4. 0x1C & 0x0F = 0x0C = 12. Reading bits 6:3 directly gives 1100, which is also 12.

CHECK YOURSELF 4

A small microcontroller has 512 bytes of RAM free for the stack. Each call of a recursive function takes 24 bytes of stack. At most how many calls can be in progress at once without overflowing the stack?

Answer and solution

Answer:

  1. The depth is at most floor(B / c), with B = 512 bytes and c = 24 bytes.

  2. 512 / 24 = 21.33.

  3. The floor is 21. Check: 21 x 24 = 504 bytes fits, and 22 x 24 = 528 bytes does not.

Used in these GATE topics