A general overview of what happens before main()

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I found this link in one of Jack Ganssle's The Embedded Muse (#427) that I thought might be helpful to others and beginning programmers, there are links at the bottom for continued study.

Here is the link: https://embeddedartistry.com/blo...

 

I hope you find it useful.  

 

Jim

Not sure if the Arduino forum is the best place for this.....

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

Last Edited: Tue. Aug 3, 2021 - 02:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Not sure if the Arduino forum is the best place

General Programming, surely?

 

https://www.avrfreaks.net/forum/what-startup-code-and-where-does-it-come

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Aug 3, 2021 - 01:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Feel free to move it, my thoughts were some Arduino users wanting to learn more about bare metal programming may find it here.

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The article seems to be rather biased to a hosted OS environment rather than what actually happens in micros. The CRT for an AVR does not need to concern itself with things like argc/argv or MMUs etc.

 

Obviously the CRT for AVR will be a bit different depending on the compiler in question but on the whole it probably:

 

1) disables I in SREG

2) may handle possible watchdog restart

3) initalises stack

4) optionally clears BSS if used

5) optionally copies initial .data from flash to RAM

6) CALLs main

7) if the CALL to main returns handle it somehow

 

In the case or C++ there are extra steps in there for calling the c'tors before main() and the d'tors after.

 

EDIT: bit of a shame this post I made on 2010 on the old Freaks appears to have gone through the mangler:

 

https://www.avrfreaks.net/commen...

 

It shows the approach used by avr-gcc

 

(kind of a shame I never followed Eric's advice in a subsequent post and made it into a tutorial!)

 

I'm pretty sure I have typed pretty much the same kind of explanation a few times now. If only I could find them!

Last Edited: Tue. Aug 3, 2021 - 03:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
The article seems to be rather biased to a hosted OS environment

It is a General overview.....    although it does cover bare metal(restart vector) and bootloader startups further down the page.....

Thanks for AVR specifics!

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
3) initalises atck

Assume you meant: 3) initializes stack  (typo)

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Assume you meant: 3) initializes stack  (typo)
Yup, corrected above.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 3

Boy, oh, boy. Trying to find something I know I've typed previously among 102,500 posts is fraught!

 

Anyway for avr-gcc I had a go at explaining it's CRT in this post (mainly comments in a disassembly): https://www.avrfreaks.net/commen...

 

EDIT: SAINTS BE PRAISED! I can't believe I finally found the post I was looking for...

 

https://www.avrfreaks.net/commen...

 

That is a fairly recent explanation (so hopefully up to date info) about the CRT that AVR-libC provides to for avr-gcc.

Last Edited: Tue. Aug 3, 2021 - 03:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is a fairly recent explanation (so hopefully up to date info) about the CRT that AVR-libC provides to for avr-gcc.

With all the great info you've provided over time...it is time to get your book on Amazon--print on demand!

But, who has time to read?  sad

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
But, who has time to read?

I have 😏 and probably many others. I already open/read this forum multiple times a day. 

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan
“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint-Exupery

Last Edited: Tue. Aug 3, 2021 - 08:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Surely before main you have appetisers or antipasto (salami, olive, bruschetta etc...)

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
Surely before main you have appetisers or antipasto (salami, olive, bruschetta etc...)

Some of us eat desert first! 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
Surely before main you have appetisers or antipasto (prosciutto, gorgonzola  etc...)

No, thanks. No main, I like to assemble it my way.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


ki0bk wrote:
 eat desert  

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


Beware of a flavors of Main, some are indigestible.