Kiran Nevaskar
Kiran Nevaskar

Reputation: 11

Absolute function address keil 8051

How to locate and call functions with absolute address in C51 Keil? Background ROM has some utility functions. I want to call those functions in order to optimize code space of flash.

Upvotes: 0

Views: 859

Answers (1)

Helmut
Helmut

Reputation: 46

A simple example:

#pragma SRC

// type of external function
typedef void (code * tExternalFunctionPtr)(void);

// Address of external function in ROM
#define FUNC_ADDR (0x1234)

void callExample(void)
{
    // Simple example with call via function pointer in RAM
    tExternalFunctionPtr funcPtr;
    funcPtr = FUNC_ADDR;
    (*funcPtr)();

    // Direct call without function pointer in RAM
    (* (tExternalFunctionPtr)FUNC_ADDR)();
}

compiles to the following assembly code:

; TEST.SRC generated from: TEST.C


NAME    TEST

?PR?callExample?TEST SEGMENT CODE
EXTRN    CODE (?C_ICALL)
PUBLIC    callExample
; #pragma SRC
; 
; // type of external function
; typedef void (code * tExternalFunctionPtr)(void);
; 
; // Address of external function in ROM
; #define FUNC_ADDR (0x1234)
; 
; void callExample(void)

    RSEG  ?PR?callExample?TEST
    USING    0
callExample:
            ; SOURCE LINE # 9
; {
            ; SOURCE LINE # 10
;     // Simple example with call via function pointer in RAM
;     tExternalFunctionPtr funcPtr;
;     funcPtr = FUNC_ADDR;
            ; SOURCE LINE # 13
;---- Variable 'funcPtr?01' assigned to Register 'R6/R7' ----
    MOV      R7,#034H
    MOV      R6,#012H
;     (*funcPtr)();
            ; SOURCE LINE # 14
    MOV      R2,AR6
    MOV      R1,AR7
    LCALL    ?C_ICALL
; 
;     // Direct call without function pointer in RAM
;     (* (tExternalFunctionPtr)FUNC_ADDR)();
            ; SOURCE LINE # 17
    LCALL    01234H
; }
            ; SOURCE LINE # 18
    RET      
; END OF callExample

    END

That may help to show how it could work in principle.

But that's not all: You have to know the address of the function in ROM you want to call. You also have to know if and how Parameters are expected and return values come back. It is also important to know which Registers the ROM function may change and to make sure that the C51 Compiler does not expect that these Registers are unchanged.

Upvotes: 1

Related Questions