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

How to call a C function from assembly

I am writing an ISR using assembly language .
I want to know how to call a C function from assembly.I am using keil compiler and 8051 microcontroller .Reply as soon as possible.

  • Please refer to the chapter 9 in the A51.pdf

  • Thanks for ur suggestion.But can u tell me how can get that A51 pdf as I am new to this site.

  • http://www.keil.com/support/man/docs/c51/c51_ap_ctoasm.htm

    contains the calling conventions and, although written more from a perspective of calling assembly functions from C, all the information necessary to call C functions from assembly.

  • Hello Mr.franck,

    Thanks for ur help.But in that ,How to call an assembly function from C is there but how to call a C function from assembly they have not explained.I have tried using LCALL instruction by specifying the C function name directly but its giving syntax error. So please let me know how can I proceed using LCALL instruction.

    Regards
    namratha.

  • The Conventtion is exactly the same.

    If you can do one, you can do the other!

    "I have tried using LCALL instruction by specifying the C function name directly but its giving syntax error."

    SO your problem is in your assembler syntax, not in the C-to-Assmebler (or vice-versa) calling mechanism.

    You need to fix your syntax.

    If you want help with your syntax, you will obviously have to post your code and the full text of the error message.

    Be sure to check the basics in the Manual first.

    Have you checked your examples folder for an example on this?

    Take care to follow the instructions for posting code:
    http://www.keil.com/forum/tips.asp

    and use copy-and-paste - do not re-type stuff into the forum!

  • Instead of writing

    LCALL function_name

    I replaced the above with the follwing statement

    LCALL base_address_of_function_name

    like LCALL 0x58DC

    After the above modifications its not giving any syntax error but logical errors i am facing means my code is not working.

    I have put that particular address by seeing in keil compiler tools " code " tab which shows base addresses of all functions.

    After compiling the modified code(I mean converted code)I checked the code tab the base addresses of the functions are changed.

    Now I think u might have got my problem.So what to do?

  • After the above modifications its not giving any syntax error but logical errors i am facing means my code is not working.

    Simply calling the C function is not enough - your assembly code must also follow the calling conventions for C functions, which are described in the link I posted earlier. It deals with things like:

    * How are the function arguments passed to the function ?
    * How is the return value passed back to the caller ?
    * Which registers have to be saved by the caller, and which are saved by the function ?

    and so on.

  • "LCALL function_name"

    So, presumably, the original syntax error was because the assembler didn't recognise the function_name?

    Did you provide an appropriate EXTERN declaration for function_name?

    If you didn't, how do you expect the assembler to recognise the function name?
    This is exactly the same as if you were calling an external assembler function!!


    "I replaced the above with the follwing statement

    LCALL base_address_of_function_name

    like LCALL 0x58DC"


    You really don't want to do that, do you?

    "After compiling the modified code(I mean converted code)I checked the code tab the base addresses of the functions are changed."

    Of course it did! That's the whole point of using symbolic names, isn't it?!

  • Thanks for ur suggestion.But can u

    then you seem surprised

    So, presumably, the original syntax error was because

    How come?

    the assemble and compiler require correct writing.

    Erik