Keil Logo

C51: EXPECT LCALL BUT SEE LJMP


Information in this article applies to:

  • C51 All Versions

QUESTION

When 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?

ANSWER

The 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

  • Refer to OPTIMIZE in the Cx51 User's Guide.
  • Refer to LJMP in the 8051 Instruction Set Manual.
  • Refer to LCALL in the 8051 Instruction Set Manual.

Last Reviewed: Thursday, September 22, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.