For embedded systems, if there is no RTOS running, the main function main() in program development needs to use some mechanism to keep running happily forever, it has no endpoint. If you want to exit from the main function, what happens specifically is determined by the C language compiler used.
1. Introduction to the Problem
Today, I saw an interesting question in the microcontroller LED module definition function. The questioner was conducting a basic C51 programming experiment and wrote a simple C51 program as follows:
#include <REGX51.H>
void test(num) {
switch(num) {
case 1: P2_0=0; P2_1=0;
break;
}
}
void main(void) {
test(1);
}
After the program executes, it can be seen that two LEDs on the experimental board are lit, while the other six are surprisingly dimly lit.

If an infinite loop is added in the main program: while(1);, then the “dimly lit” phenomenon on the circuit board will no longer appear.
#include <REGX51.H>
void test(num) {
switch(num) {
case 1: P2_0=0; P2_1=0;
break;
}
}
void main(void) {
test(1);
while(1);
}

The difference between the two situations above is that in the second program, the main loop main() function never exits, while in the first program, the main() function exits. It seems that the dimly lit LEDs are related to what the microcontroller does after the main function exits.
So there remains a question: For ordinary embedded systems, after the main() function exits in C language programming, where does the program go?
2. Where Does the Program Go?
From the code written by the questioner, it can be seen that he is a C51 enthusiast using the C51 compiler, happily experimenting on a C51 development board. He initially did not install the convention of embedded program development, using an infinite loop in the main program void main(void) to keep the program controlled within the main function, which led to the confusing results in the previous experiment.
“
Note: He is a bold and meticulous person, and his observations are quite careful.
”
2.1 The Beginning of Creation
For C language programming, all user programs start from the main() function. The task of opening the world for user programs is done by a small piece of Pangu code STARTUP.A51.
The execution flow of the 8051 microcontroller program (managed by STARTUP.A51 for the execution of the main function)
Below is an excerpt from the STARTUP.A51 code, where Pangu does some preparatory work (initializing global variables, stack pointer) after the microcontroller RESET, and then directly jumps to:<span>?C_START</span>
NAME ?C_STARTUP
?C_C51STARTUP SEGMENT CODE
?STACK SEGMENT IDATA
RSEG ?STACK
DS 1
EXTRN CODE (?C_START)
PUBLIC ?C_STARTUP
CSEG AT 0
?C_STARTUP: LJMP STARTUP1
RSEG ?C_C51STARTUP
STARTUP1:
IF IDATALEN <> 0
MOV R0,#IDATALEN - 1
CLR A
IDATALOOP: MOV @R0,A
DJNZ R0,IDATALOOP
ENDIF
IF XDATALEN <> 0
MOV DPTR,#XDATASTART
MOV R7,#LOW (XDATALEN)
IF (LOW (XDATALEN)) <> 0
MOV R6,#(HIGH (XDATALEN)) +1
ELSE
MOV R6,#HIGH (XDATALEN)
ENDIF
CLR A
XDATALOOP: MOVX @DPTR,A
INC DPTR
DJNZ R7,XDATALOOP
DJNZ R6,XDATALOOP
ENDIF
IF PPAGEENABLE <> 0
MOV PPAGE_SFR,#PPAGE
ENDIF
IF PDATALEN <> 0
MOV R0,#LOW (PDATASTART)
MOV R7,#LOW (PDATALEN)
CLR A
PDATALOOP: MOVX @R0,A
INC R0
DJNZ R7,PDATALOOP
ENDIF
IF IBPSTACK <> 0
EXTRN DATA (?C_IBP)
MOV ?C_IBP,#LOW IBPSTACKTOP
ENDIF
IF XBPSTACK <> 0
EXTRN DATA (?C_XBP)
MOV ?C_XBP,#HIGH XBPSTACKTOP
MOV ?C_XBP+1,#LOW XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0
EXTRN DATA (?C_PBP)
MOV ?C_PBP,#LOW PBPSTACKTOP
ENDIF
MOV SP,#?STACK-1
LJMP ?C_START
END
The above code has also been gradually debugged and verified in the blog post about the execution flow of the 8051 microcontroller program (STARTUP.A51):

2.2 The End of the World
Since entering the main() function is a long jump, the main function will not normally return to the startup program STARTUP.A51, so where does the program go?
In the blog post about the while(1) problem in microcontroller C language, the author examined the last moments of the main function for the KEIL compiler and the MAPLAB compiler through disassembly.
Keil Compiler
At the end of the main function, the program adds the following few lines of code:
MOV R0, #0x7F
CLR A
MOV @R0, A
DJNZ R0, (3)
MOV SP, #0x0C
LJMP main
These few statements, the first four, clear the first 128 addresses of the microcontroller’s memory, the fifth defines the stack, and the sixth jumps the program back to the first line of the main function for execution.
MAPLAB Compiler
Tracking the PIC microcontroller language program, it was found that the last statement of the main() function is reset, which means that the microcontroller directly resets, a reset statement added by the MAPLAB compiler based on the characteristics of the PIC microcontroller.
Conclusion
For embedded systems, if there is no RTOS running, the main function (main()) in program development needs to use some mechanism to keep running happily forever, it has no endpoint. If you want to exit from the main function, what happens specifically is determined by the C language compiler used.
Previous Recommendations
●
Most Used Software in Embedded Field
●
These Few Lines of Code Are Quite Impressive
●
Bad Habits in Development, Stay Away!
