Regarding startup code

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Regarding startup code

926 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sherrysamuel on Sun Jul 10 19:08:48 MST 2011
Hi all,
I am new to LPC xpresso.

I was going through the start up code. In the Reset routine I could find the loop where the data is copied to RAM. But below that I could find the comment regarding the initialization of variables in BSS segment:
"// Zero fill the bss segment.  This is done with inline assembly since this
    // will clear the value of pulDest if it is not kept in a register."

Can you please explain why ?
What puzzles me more is why the code for copying the data variables to RAM is in C and other code for initializing BSS segment is in inline assembly ?
0 Kudos
Reply
3 Replies

904 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Tue Jul 12 00:03:58 MST 2011
Hi Sherry,


Quote: sherrysamuel
But "pullDest" is a local variable and as far I know it will not be allocated in .bss section.



Yes indeed.
[I][B]As far as i know[/B][/I] you are right.
Good to see that there are still people who know how a compiler behaves (should behave...) :D

Regards,

Rob
0 Kudos
Reply

904 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by sherrysamuel on Mon Jul 11 19:04:07 MST 2011
Hi Rob
Thx for your quick reply.

[PHP][/CODE]But depending on the compiler and compiler optimizations, pullDest may be one of the variables that is places in the bss segment. So this could mean that pullDest is being reset to 0, resulting in invalid memory access.
That is the reason why this piece of code is written in Assembly and not in C.[/PHP]

But "pullDest" is a local variable and as far I know it will not be allocated in .bss section. It will be allocated in stack as it is local to function.Please correct me if I am wrong. So how will it result in invalid memory access.


Regards,
Sherry
0 Kudos
Reply

904 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Sun Jul 10 23:14:43 MST 2011

Quote: sherrysamuel

What puzzles me more is why the code for copying the data variables to RAM is in C and other code for initializing BSS segment is in inline assembly ?



Assembly is not the easiest language to use.
Maintainability of assembly is not always easy - I have programmed quite a bit of ARM (32-bits and Thumb1) assembly and I used to be the only person who was able to maintain the assembly code we wrote in a team with 10 software engineers.
So we tried to create C-code as much as possible within our projects.

Writing the code to clear the BSS segment would loook like this in C-code:
    for(pulDest = &_bss; pulDest < &_ebss; )
    {
        *pulDest++ = 0;
    }
But depending on the compiler and compiler optimizations, pullDest may be one of the variables that is places in the bss segment. So this could mean that pullDest is being reset to 0, resulting in invalid memory access.
That is the reason why this piece of code is written in Assembly and not in C.

Regards,

Rob
0 Kudos
Reply