This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

From C, inline assembly LJMP to Numerical Address

How do I perform a long jump to a numerical address from my C code?
I'm pretty sure I have to do this with inline assembly - however that
seems complicated for C51. I figured in C I could do this:

typedef void (*jmpPtr)(void);
jmpPtr jmp;

...

unsigned int address = 0x4000U;
jmp = (jmpPtr) address;
(*jmp)();

Except for the call differences, I think this would be the same as doing
a jump to the numerical address? Keil, is this correct?

Parents
  • unsigned int address = 0x4000U;
    unsigned char idata *p;
    
    p = SP;
    SP += 2;
    p[0] = address & 0xFF;
    p[1] = address >> 8;

    Won't work. C will indirect through R0 pointing you into IDATA space and SP lives in DATA space.

    unsigned char data *p;
    won't work either, sorry.

    And yes, as your own follow-up indicates. The 8051 treats 16-value as little-endian (LSB in lower numbered address) as is the case with the SP.

    - Mark


Reply
  • unsigned int address = 0x4000U;
    unsigned char idata *p;
    
    p = SP;
    SP += 2;
    p[0] = address & 0xFF;
    p[1] = address >> 8;

    Won't work. C will indirect through R0 pointing you into IDATA space and SP lives in DATA space.

    unsigned char data *p;
    won't work either, sorry.

    And yes, as your own follow-up indicates. The 8051 treats 16-value as little-endian (LSB in lower numbered address) as is the case with the SP.

    - Mark


Children
  • Huh? The difference between IDATA (0x80-0xFF) and DATA (0x00-0x7F) is that IDATA is only accessible through SP, R0, and R1. DATA is accessible through SP, R0, R1, and using direct addressing. What you're suggesting is that R0 can't access DATA space, and we know that's not right (e.g., STARTUP.A51 uses exactly that technique to clear DATA, as well as IDATA).

    My followup has nothing to do with endianness. My byte ordering is correct. It has to do with the SP's pre-increment behavior. For example, reset initializes SP to 0x07 (&RB0/R7) the first LCALL writes the return address addresses 0x08-9.

    Now, I'll admit, that my approach may very well *not* work, but I don't think it will be for the reasons you give. I've not tried it, since I'm not at my development system, but I intend to later. Then when it doesn't work I'll tell everone why and take the heat for my lame suggestion :->

    --Dan Henry

  • What you're suggesting is that R0 can't access DATA space, and we know that's not right (e.g., STARTUP.A51 uses exactly that technique to clear DATA, as well as IDATA).

    Actually, we're both right. Using R0 as an indirection register startup.a51 clears out the entire IDATA space from 0x00 to 0xFF (on 8052's). Note that the IDATA space overlap's the DATA space. The I stands for Indirect, thus, whenever you Indirect through R0 or R1 you are by definition in IDATA.

    Your byte ordering was correct, I didn't say otherwise. The 8051 is little-endian for 16-bit values.

    Regards,

    - Mark

  • I've tested the following code and verified that it works as advertised.

    unsigned address;
    
    void JumpedTo( void )
    {
        while (1)
            ;
    }
    
    void main( void )
    {
        unsigned char idata *p;
    
        /*  Set stack pointer to 0x7E, forcing the
         *  "pushed" address to straddle the data/idata
         *  boundary at 0x80.  NOTE: Only for 8052
         *  derivatives.
         */
        SP = 0x7E;
        address = JumpedTo;
    
        p = SP + 1;
        SP += 2;
        p[0] = address & 0xFF;
        p[1] = address >> 8;
        return;
    }

    idata pointers and data pointers are equivalent, and in fact, have the "generic pointer" memory type. Declaring "p" as an idata pointer makes it more obvious (to me) that the pointer could access the full 256 byte range. I went ahead and tested with "p" declared as a "data *" also, just to be certain I'm not misleading anyone.

    --Dan Henry

  • I've tested the following code and verified that it works as advertised.<br>
    <br>

    unsigned address;
    
    void JumpedTo( void )
    {
        while (1)
            ;
    }
    
    void main( void )
    {
        unsigned char idata *p;
    
        /*  Set stack pointer to 0x7E, forcing the
         *  "pushed" address to straddle the data/idata
         *  boundary at 0x80.  NOTE: Only for 8052
         *  derivatives.
         */
        SP = 0x7E;
        address = JumpedTo;
    
        p = SP + 1;
        SP += 2;
        p[0] = address & 0xFF;
        p[1] = address >> 8;
        return;
    }
    <br>
    idata pointers and data pointers are equivalent, and in fact, have the "generic pointer" memory type. Declaring "p" as an idata pointer makes it more obvious (to me) that the pointer could access the full 256 byte range. I went ahead and tested with "p" declared as a "data *" also, just to be certain I'm not misleading anyone.<br>
    <br>
    --Dan Henry<br>

  • Let's try that again.

    Dan, you have inspired me. Here's my "Jump to an absolute address" that I hope solves the "jump from C" issue.

    // 'r_' prefix denotes a CPU 'r'egister.
    sfr r_stackPointer = 0x81;
    
    void main(void)
    {
        // Point so that this will work for 8051's and 52's.
        unsigned char data *p = 0x7E; 
    
        // Little endian, LSB in lower address, MSB in higher address.
        *p = 0x00;  // LSB of address 0x4000.
        ++p;
        *p = 0x40;  // MSB of address 0x4000;
    
        // Now fool the CPU (this is the magic part).
        r_stackPointer = 0x7F;
    
        // "Jump via return"
        return;
    }
    What fun.

    - Mark