0
0
Embedded Cprogramming~5 mins

Memory-mapped I/O concept in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Memory-mapped I/O concept
O(n)
Understanding Time Complexity

When using memory-mapped I/O, we want to know how the time to access hardware changes as we do more operations.

We ask: How does the number of hardware reads or writes grow with the number of commands?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#define IO_REG (*(volatile unsigned int*)0x40000000)

void write_values(int* values, int n) {
    for (int i = 0; i < n; i++) {
        IO_REG = values[i];
    }
}
    

This code writes a list of values to a hardware register using memory-mapped I/O.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Writing a value to the hardware register.
  • How many times: Once for each value in the input array, so n times.
How Execution Grows With Input

Each new value adds one more write to the hardware register.

Input Size (n)Approx. Operations
1010 writes
100100 writes
10001000 writes

Pattern observation: The number of operations grows directly with the number of values.

Final Time Complexity

Time Complexity: O(n)

This means the time to write values grows in a straight line as you add more values.

Common Mistake

[X] Wrong: "Writing to a hardware register is instant and does not add to time."

[OK] Correct: Each write takes time because it communicates with hardware, so more writes mean more time.

Interview Connect

Understanding how hardware access scales helps you design efficient embedded programs and shows you know how software and hardware work together.

Self-Check

"What if we buffered values and wrote them in batches? How would the time complexity change?"