| ||||||||
Technical Support Support Resources Product Information | C51: EXPECT LCALL BUT SEE LJMPInformation in this article applies to:
QUESTIONWhen I compile my program, and look at the disassembly, I see an LJMP at a location where I call a function, instead of an LCALL. Why, and what effect does this have on the execution of my program? ANSWERThe optimizer can rearrange code to minimize resource usage. For example, in order to avoid the use of two bytes of stack space, the compiler uses an LJMP into a function, if it's the last instruction in another function. unoptimized
94: unsigned char x=0;
95:
96: void fn1(void)
97: {
98: x++;
C:0x04E8 900028 MOV DPTR,#x(0x0028)
C:0x04EB E0 MOVX A,@DPTR
C:0x04EC 04 INC A
C:0x04ED F0 MOVX @DPTR,A
99: fn2();
C:0x04EE 1204F2 LCALL fn2(C:04F2)
100: }
C:0x04F1 22 RET
101:
102: void fn2(void)
103: {
104: x++;
C:0x04F2 900028 MOV DPTR,#x(0x0028)
C:0x04F5 E0 MOVX A,@DPTR
C:0x04F6 04 INC A
C:0x04F7 F0 MOVX @DPTR,A
105: }
C:0x04F8 22 RET
optimized
94: unsigned char x=0;
95:
96: void fn1(void)
97: {
98: x++;
C:0x04E7 900028 MOV DPTR,#x(0x0028)
C:0x04EA E0 MOVX A,@DPTR
C:0x04EB 04 INC A
C:0x04EC F0 MOVX @DPTR,A
99: fn2();
C:0x04ED 0204F0 LJMP fn2(C:04F0)
100: }
101:
102: void fn2(void)
103: {
104: x++;
C:0x04F0 900028 MOV DPTR,#x(0x0028)
C:0x04F3 E0 MOVX A,@DPTR
C:0x04F4 04 INC A
C:0x04F5 F0 MOVX @DPTR,A
105: }
C:0x04F6 22 RET
The first example uses four bytes of stack space, two for the return address from fn1, and two for the return address from fn2. The optimized code uses only two bytes of stack space for the return address pushed onto the stack when fn1 gets called. fn2 will return to the address that called fn1 when it finishes. This optimized code also executes faster and uses less program space. MORE INFORMATION
Last Reviewed: Thursday, September 22, 2005 | |||||||
| ||||||||