what is the job of startup code after ending main. I read there is a
job for it after application being terminated.
Its job is to ensure that the microcontroller continues to act in a well defined manner after
main() ends. In a freestanding environment the effect of program termination is implementation-defined, so what happens depends on the particular compiler and build environment. In the case of MPLab XC8, the compiler manual says this:-
The compiler inserts special code at the end of main() which is
executed if this function ends, i.e., a return statement inside main()
is executed, or code execution reaches the main()’s terminating right
brace. This special code causes execution to jump to address 0, the
Reset vector for all 8-bit PIC devices. This essentially performs a
Other compilers may act differently. AVR GCC just disables interrupts and halts, so the MCU freezes until physically reset.
I'm confused between bootloader and bootstrap.
'Bootstrapping' is the process of loading code from external storage at startup. It is derived from the phrase "to pull oneself up by one’s own bootstraps". In a typical computer the operating system is much too large to fit into ROM, so it is stored on some media that cannot be directly executed by the CPU. The 'bootstrap' ROM has just enough code in it to access the media and load the first bit of code that runs the OS.
In an MCU the 'bootloader' is usually code which runs at startup to 'burn' a program into Flash ROM. It is only needed when you want to install or update the firmware without having to hook the MCU up to an external programmer.
Some MCUs have a built-in bootloader which is activated when some pins are held at particular logic levels. Others have an option of starting execution from a reserved memory area where the bootloader code is placed. User code can then be placed in the same location as it would be in a non-bootloading system.
If the MCU has no built-in bootloader functionality it may still be able to do it using a program written to do the job, but the user code may need to be modified to work with the bootloader.
I believe there is a code embedded by manufacturer that is responsible
for uploading my image file into my controller. what is it exactly?
A blank PIC16F877A does not have any bootloader code installed, but it can be added by the manufacturer (on request) or by a vendor. If you have to use a programmer such as PICkit 2 to upload your code then your PIC probably doesn't have a bootloader in it.
What it is exactly depends on the particular bootloader, but in general it is some code which runs at startup that reads user code from eg. the MCU's serial port and burns it into the Flash ROM, or if nothing arrives within the timeout period simply jumps into the previously burned user code. Here's an example:-
PIC16BOOTLOAD: PIC16F87x / PIC16F87xA bootloader
You can view the assembly language source to see exactly what the code is.