Mr_M_from_G
Mr_M_from_G

Reputation: 49

Why is this loop not executed?

I compile with GCC 5.3 2016q1 for STM32 microcontroller. Right at the beginning of main I placed a small routine to fill stack with a pattern. Later I search the highest address that still holds this pattern to find out about stack usage, you surely know this. Here is my routine:

uint32_t* Stack_ptr = 0;
uint32_t Stack_bot;
uint32_t n = 0;
  asm volatile ("str sp, [%0]" :: "r" (&Stack_ptr)); 
  Stack_bot = (uint32_t)(&_estack - &_Min_Stack_Size);
//*
  n = 0;
  while ((uint32_t)(Stack_ptr) > Stack_bot)
  {
    Stack_ptr--;
    n++;
    *Stack_ptr = 0xAA55A55A;
  } // */

After that I initialize hardware, also a UART and print out values of Stack_ptr, Stack_bot and n and then stack contents. The results are 0x20007FD8 0x20007C00 0 Stack_bot is the expected value because I have 0x400 Bytes in 32k RAM starting at 0x20000000. But I would expect Stack_ptr to be 0x20008000 and n somewhat under 0x400 after the loop is finished. Also stack contents shows no entries of 0xAA55A55A. This means the loop is not executed. I could only manage to get it executed by creating a small function that holds the above routine and disable optimization for this function. Anybody knows why that is? And the strangest thing about it is that I could swear it worked a few days ago. I saw a lot of 0xAA55A55A in the stack dump.

Thanks a lot

Martin

Upvotes: 0

Views: 59

Answers (1)

vlk
vlk

Reputation: 2769

Probably problem is with the assembler function, In my code I use this:

// defined by linker script, pointing to end of static allocation
extern unsigned _heap_start;

void fill_heap(unsigned fill=0x55555555) {
    unsigned *dst = &_heap_start;
    register unsigned *msp_reg;
    __asm__("mrs %0, msp\n" : "=r" (msp_reg) );
    while (dst < msp_reg) {
        *dst++ = fill;
    }
}

it will fill memory between _heap_start and current SP.

Upvotes: 1

Related Questions