2

I am doing an energy profile of an ARM processor for an academic project. I managed to measure the power consumption of several assembly instructions by running them in a ~200 insts loop, e.g.:

.rept 200  
add r1, r2, r3
.endr

However, when coming to stack instructions, I wondered: what happens if I do something like this?

.rept 200  
pop {r1}
.endr

Notice that the control flow isn't affected, since the loop uses an absolute jump at the end, and the code is running bare metal, without an OS.

What happens when the stack finishes? Does the sp simply overflow and wrap around, going to 0x00000000 and starting popping from there?

My power sensor doesn't see anything strange; it is a constant power drain like any other instruction. Then, the question also arises: what if I keep pushing instead?

10
  • 3
    The stack pointer is just a register. So yeah, it'll just overflow eventually. Though it's more likely that your code generates an exception when attempting to access undefined memory. There might also be some sort of stack underflow protection kicking in (depends on CPU model).
    – fuz
    Commented Apr 19, 2023 at 11:35
  • Since there's no OS running, can an exception occur? The CPU is a Cortex-M4 (ARMv7 iirc). Commented Apr 19, 2023 at 11:42
  • 2
    Of course. Exceptions are something the hardware does automatically. I'm not even sure if CPSID would turn them off.
    – fuz
    Commented Apr 19, 2023 at 11:44
  • There are alternatives to push and pop. Such as ldr r1, [sp,#4]! and ldr r1, [sp], #4. You can substitute another register for 'sp'. Also, you can save 'sp' on the routine entry and point it to an array of know size. An issue is that the power consumption may include the memory access (DDR/SRAM,internal SRAM, bus type, etc). Also, the forms of the instructions maybe consume more/less power. Especially consult TRM for cycle time advice as the extra cycles will often consume more power as well. Commented Apr 19, 2023 at 13:56
  • 1
    IF you keep popping you'll have logical stack underflow, but whether this is detected by the hardware depends on the specifics of the processors and the operating environment. If you keep pushing, you'll have logical stack overflow, with the same above caveats about detection, however, unlike popping, pushing writes to memory so on some systems that don't detect overflow, you have the potential to wipe out all your data, and even your code when the machine keeps the code in the same address space memory as data.
    – Erik Eidt
    Commented Apr 19, 2023 at 16:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.