Posts

Showing posts with the label Why Series

Why Are Interrupts Better Than Polling?

Image
In embedded systems, peripherals constantly generate events. A UART receives a byte. A timer expires. A GPIO pin changes state. An ADC conversion completes. The processor needs to respond to these events. There are two common approaches: Polling Interrupts At first, polling looks simple. But interrupts usually provide a more efficient way for the CPU to respond to asynchronous events. What Is Polling? In polling, the CPU repeatedly checks whether an event has occurred. For example: while (1) { if (UART_RX_READY) { read_uart_data(); } } The processor keeps asking the UART: "Did data arrive?" If no data has arrived, the CPU checks again. And again. And again. Imagine UART data arrives only once every 100 ms. During those 100 ms, the CPU may check the same status flag thousands of times even though nothing has happened. That CPU time could have been used for something else. What Changes With Interrupts? With interrupt...

Why Does the Stack Usually Grow Downward?

Image
When debugging nested function calls, you may notice the Stack Pointer moving toward lower memory addresses as the call depth increases. For example: SP = 0x20001000 PUSH {R4} SP = 0x20000FFC But why does this happen? Is a downward-growing stack inherently better, or is it simply an architectural convention? 1. The Stack Does Not Have to Grow Downward A processor can support either: Descending stack: SP decreases as data is pushed. or: Ascending stack: SP increases as data is pushed. Both approaches can work efficiently. So the real question is: Why did descending stacks become so common? 2. It Fits Traditional Memory Layouts Well A simplified memory layout often looks like this: High Address +------------------+ | Stack | | ↓ | | | | Free RAM | | | | ↑ | | Heap | +------------------+ Low Address The stack can begin near the upper end of available memory and grow toward lower addr...

🤔 Why Is RAM Access Slower Than CPU Registers in ARM Microcontrollers?

CPU registers are located inside the processor core . RAM is located outside the CPU core . So accessing RAM requires: address generation bus access memory read/write cycles But registers can be accessed almost instantly by the CPU. That is why CPUs first load data into registers before processing. 👉 Faster register access = better execution speed. This is also why: repeated memory access slows firmware efficient register usage improves performance compilers try to keep frequently used variables in registers Understanding this helps in: firmware optimization driver development assembly understanding performance tuning Small low-level concept. Huge embedded impact. #TheWhySeries #EmbeddedSystems #Firmware #ARM #Microcontrollers #EmbeddedC