Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Starting Address for main funtion in microcontroller

Status
Not open for further replies.

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
In assembly language,
we write ORG 0x100 which indicates starting address of program to be executed

In C, we write main() {... }
Where does microcontroller start executing this prog? I guess linker will provide starting address for code in C, but even linker file didnt have any specified address for main()?

Please let me know, when we write a code in C and prog it in microcontroller memory, how the memory addresses are managed?

Any link, article, book will do to me.
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
in 8051 it is usually in 0x800 location

you can check it in keil after compiling go to debug and see the disassembly window from view option...
go to 0x800,

or open memory window and type c:0x800 and see your code from that location
 

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
hi...
tnx for reply

I meant who decides it and on what basis? Is there any std algorithm available or its just linker which randomly puts the starting address?

I had been asked this question in the interview and couldnt answer :(
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
it is decided by the startup code in the controller.... in 8051 it is by default added, in 32bit controller we have to add it or else the program will not execute
 

bluehole

Full Member level 6
Joined
Sep 1, 2009
Messages
345
Helped
74
Reputation
148
Reaction score
73
Trophy points
1,308
Location
Some where In INDIA
Activity points
3,290
In assembly language,
we write ORG 0x100 which indicates starting address of program to be executed
Well Not in assembly language It is generally depends on Architecture and OS
The ORG 0x100 is generally the Entry-point of MS-DOS COM file.
Where as the MS-DOS EXE file have 0x0
Q) Why the different address?
A) The Entry=point address is used by the loader to load the application executable

In C, we write main() {... }
Where does microcontroller start executing this prog? I guess linker will provide starting address for code in C, but even linker file didnt have any specified address for main()?

Please let me know, when we write a code in C and prog it in microcontroller memory, how the memory addresses are managed?

Any link, article, book will do to me.

Well In C if you are writing for an OS then main() is not the Entry-point. Surprised???
well generally there is another function start() which comes from the start-up code which is linked by the linker, unless you don't say it not to include.

If you are writing any free-standing code then you have to instruct linker which function is your entry-function and where it should be located(address).

If you are a Linux user then you can refer to the gcc docs for the details about linker script.

Next You asked where to load the program for different controllers, well it is different for different processors.
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
in the keil IDE ( evaluation version) lower 1k 0x0000 to 0x7ff is reserved for asm and 0x800 to next 1k is for C..... the asm programs are stored in 0x0000 and c program is stored in 0x800, if you analyse the startup code of 8051, you see that it initialise the peripherals and then after that needs to jump to main function...

see the startup code as startupa51 file.... you will understand
 

bluehole

Full Member level 6
Joined
Sep 1, 2009
Messages
345
Helped
74
Reputation
148
Reaction score
73
Trophy points
1,308
Location
Some where In INDIA
Activity points
3,290
@scorrpeio
Are you asking, When a Processor is powered on How it knows from where it executes?
If so then it depends on the reset address I guess. when a reset signal or power-on happens then the processor sets all it's address pins to a particular location. from there it starts executing.

If you are asking about how a linker sets it then it does it by adding offset to the entry function.
Bellow is the linker script of my toy kernel
Code:
/*//////////////////////////////////////////////////////////*/
/*// This file is a part of Nanos Copyright (C) 2008-2010 //*/
/*// ashok.s.das@gmail.com                                //*/
/*//////////////////////////////////////////////////////////*/
/*// Kernel linker                                        //*/
/*//                                                      //*/
/*//////////////////////////////////////////////////////////*/
/* Link.ld -- Linker script for the kernel - ensure everything goes in the */
/*            Correct place.  */
/*            Original file taken from Bran's Kernel Development */
/*            tutorials: http://www.osdever.net/bkerndev/index.php. */

ENTRY(start) /* start is the real entry to my Kernel but my Kernel is written in C++ it has main() The Main is in turn called from start in an assembly routine startup.asm*/
SECTIONS
{
  .text 0x100000 :     /* the image load address for GRUB compatible kernel this is the real entry address */
  {
	code = .; _code = .; __code = .;
	*(.text)
	
	. = ALIGN(4096);
  }

  .data :
  {
	__CTOR_LIST__ = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2) *(.ctors)   LONG(0) __CTOR_END__ = .;
	__DTOR_LIST__ = .; LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2) *(.dtors) LONG(0) __DTOR_END__ = .;
	data = .; _data = .; __data = .;
	*(.data)
	*(.rodata)
	
	. = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
}

Hope it made your queries clear.
 

krishna1234

Member level 2
Joined
Dec 18, 2010
Messages
49
Helped
9
Reputation
18
Reaction score
9
Trophy points
1,298
Location
mumbai,india
Activity points
1,616
embeeded c is build for easiness. using this we can write programs very easily compared to that of assembly because we don't have to think about the starting location of a particular chip.so if you start a program for a particular chip by default it will assign the starting according to the chip that you are selected.(you can also change this default location in some compiler ).if you write some instruction in c the compiler has the optimized code structure ( machine language ) for that instruction. (even it knows how to reduce the whole program memory and where to write.so in c you don't have to be a chip master in order to program.
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
so if you start a program for a particular chip by default it will assign the starting according to the chip that you are selected.(you can also change this default location in some compiler ).if you write some instruction in c the compiler has the optimized code structure ( machine language ) for that instruction. (even it knows how to reduce the whole program memory and where to write.so in c you don't have to be a chip master in order to program.

1. without startup code compilers cannot give any address to program....

2. compiler does not know how to reduce the program memory automatically unless it has optimization techniques inbuilt.....
3. more importantly............ it should be cross compiler and not compilers
 

krishna1234

Member level 2
Joined
Dec 18, 2010
Messages
49
Helped
9
Reputation
18
Reaction score
9
Trophy points
1,298
Location
mumbai,india
Activity points
1,616
1. without startup code compilers cannot give any address to program....

2. compiler does not know how to reduce the program memory automatically unless it has optimization techniques inbuilt.....
3. more importantly............ it should be cross compiler and not compilers

i am not saying every compiler have the capability.your points are so important thanks for the reply.
 

bluehole

Full Member level 6
Joined
Sep 1, 2009
Messages
345
Helped
74
Reputation
148
Reaction score
73
Trophy points
1,308
Location
Some where In INDIA
Activity points
3,290
1. without startup code compilers cannot give any address to program....
How compiler gives address? Do you mean the ORG instruction , entry point???
Compiler doesn't assigns any address. But Linker Does. The Actual entry point address is defined in start-up code which the linker
links automatically in ideal condition.
2. compiler does not know how to reduce the program memory automatically unless it has optimization techniques inbuilt.....
3. more importantly............ it should be cross compiler and not compilers

rest 2 points are correct.
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
Compiler doesn't assigns any address. But Linker Does. The Actual entry point address is defined in start-up code which the linker
links automatically in ideal condition.

linker does the allocation of data to actual memory, but the startup code is compiled and its data is also given to linker for further processing
 

bluehole

Full Member level 6
Joined
Sep 1, 2009
Messages
345
Helped
74
Reputation
148
Reaction score
73
Trophy points
1,308
Location
Some where In INDIA
Activity points
3,290
linker does the allocation of data to actual memory, but the startup code is compiled and its data is also given to linker for further processing

Sorry, No data :wink:
refere wikipedia
specially the overview section. It is nice.

For memory location and address space the bellow para is Good I guess :grin:

Code:
The linker also takes care of arranging the objects in a program's address space.
This may involve relocating code that assumes a specific base address to another base.
Since a compiler seldom knows where an object will reside, it often assumes a fixed
base location (for example, zero). Relocating machine code may involve re-targeting of
absolute jumps, loads and stores.

For selfstanding code the address space doesn't have any meaning. so if we are writing any code which is not executed on any OS means it is self standing.

Next the entrypoint for retargeting is Purely processor/controller specific.
 
Last edited:

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
Hello,
your advice is worthy to me. I have got to know many things.
tnx for it
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
For memory location and address space the bellow para is Good I guess :grin:

For selfstanding code the address space doesn't have any meaning. so if we are writing any code which is not executed on any OS means it is self standing.

Next the entrypoint for retargeting is Purely processor/controller specific.


sorry we in our professional foeld very rarely use wikipedia... ITS JUST FOR NOT PROFESSIONALS......... AT LEAST NOT IN MY 10YEARS IF EXPERIENCE ..... we usually go by data sheet of manufacturer...

take case of ARM... you have linker and loader... but without startup code the controller fails to execute IF THERE IS NO START UP CODE... WHY.??????????????
 

bluehole

Full Member level 6
Joined
Sep 1, 2009
Messages
345
Helped
74
Reputation
148
Reaction score
73
Trophy points
1,308
Location
Some where In INDIA
Activity points
3,290
sorry we in our professional foeld very rarely use wikipedia... Its just for not professionals......... At least not in my 10years if experience .....
i have 15 years.:wink:
i don't say you use wikipedia, there is nothing wrong to refer. even proffesors refer.
By the way it was for everyone Nothing personal.
we usually go by data sheet of manufacturer...
me too :grin:
Take case of arm... You have linker and loader... But without startup code the controller fails to execute if there is no start up code... Why.??????????????

Most controller will fail.
My point was the process of compiler and how it is related with low-level stuffs.
 

bobsanjose

Advanced Member level 1
Joined
Jan 13, 2006
Messages
416
Helped
56
Reputation
112
Reaction score
21
Trophy points
1,298
Location
Silicon Valley
Activity points
5,228
Let me give a shot at it.
The hardware start address, also called reset address is given by the architecture, some start from address 0, other from the upper end of the address range, as it is stated in the architecture reference manual.
The start address set by the C-Compiler for main is a different thing and the branch to this main is not the first thing that is executed.
1. The code operation starts from the reset address
2. There is usually a branch to the Startup code located at the reset address.
3. Startup code includes basic initialization of the device, e.g. interrupt controller init, bus configurations, timer setup for a minimum system. This code will be provided by the C-Compiler vendor and can be modified to your requirements.
4. At the end of the Start-up sequence, there is a branch to main.
5. Your code that is written in "C" will now be executed.

Hope this helps a bit to clarify things.

Bob
 

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
Let me give a shot at it.
The hardware start address, also called reset address is given by the architecture, some start from address 0, other from the upper end of the address range, as it is stated in the architecture reference manual.
The start address set by the C-Compiler for main is a different thing and the branch to this main is not the first thing that is executed.
1. The code operation starts from the reset address
2. There is usually a branch to the Startup code located at the reset address.
3. Startup code includes basic initialization of the device, e.g. interrupt controller init, bus configurations, timer setup for a minimum system. This code will be provided by the C-Compiler vendor and can be modified to your requirements.
4. At the end of the Start-up sequence, there is a branch to main.
5. Your code that is written in "C" will now be executed.

Hope this helps a bit to clarify things.

Bob

Ok...
so can I put an analogy as startup code does the same function in embedded C as done by BIOS/BootStrap in OS ??
(Just asking for better understanding.)

Also,
Datasheet of controller mentions about the reset values of different FSR and default port config at reset.
Doesnt this mean, controller itself takes care of FSR, intr reg and port pin at reset?
If yes, then what kind of setting does start up code do? I have seen startup code in MPLAB for PIC. There was some stack pointers declared(didnt understand whole code though) and some other memory related stuff.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top