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

C and Assembly

I want to know how to write assmebly code and c code in same program.... i mean i need to write some function in assembly and some in C code.. i should be able to call both of them from either codes.. how to do this.. help me out..

Parents
  • you MUST have a main() in C to use a mixture.

    I had a case where the "hand optimizing" has to be done in the main and it would not work.

    I finally found out that to make it run I had to make the following in C

    void main(void)
    {
      asm_code():
    )
    

    and it worked.

    I do not consider this a 'defect' of the Keil tools, just a trap

    Erik

Reply
  • you MUST have a main() in C to use a mixture.

    I had a case where the "hand optimizing" has to be done in the main and it would not work.

    I finally found out that to make it run I had to make the following in C

    void main(void)
    {
      asm_code():
    )
    

    and it worked.

    I do not consider this a 'defect' of the Keil tools, just a trap

    Erik

Children
  • C code normally needs a C library.
    The C library needs to be initialized.
    The startup file normally calls the initialization function of the C library.
    The initialization function of the C library normally calls main().

    You could have written main() in assembler. The alternative - not use the C library - can be quite hard, since the compiler needs helper functions for a number of things (division, some pointer arithmetic, large integers, ...)

  • "The startup file normally calls the initialization function of the C library.
    The initialization function of the C library normally calls main()."

    Somewhere in that chain is also the thing that initialises the user's 'C' variables...

    So, back to Erik's point, you really should make sure that you have a main() function in 'C' as your programs "entry point" - which was effectively also my point in saying, "you would generally just be calling from 'C' to assembler; not vice-versa"

  • You could have written main() in assembler.

    did that, no go. it evidently must be C

    Erik

  • I think the problem is that the tools (Linker?) somehow recognise a main() written in 'C', and automatically add all the necessary support.
    This doesn't happen when you write an assembler function that just happens to have the name "main".

    This is the same reason why using the SRC directive on a whole 'C' project stops it building...

  • Just as a follow up on this subject.

    If you wanted to create a 'main' in assembler, the first thing you would have to do is look at the startup code. It doesn't call main, it calls an external function called ?C_START.

    I think you would find that ?C_START is a run-time library function that (in turn) calls the traditional C function main. Almost certainly, the purpose of this mechanism would be to ensure that the critical parts of the run-time library are pulled into the executable.

    So ... As a simple exercise, you can modify the startup code and replace/change the references to ?C_STARTUP to main and hey presto, you can call an assembler version of main.

  • I think the problem is that the tools (Linker?) somehow recognise a main() written in 'C', and automatically add all the necessary support.
    This doesn't happen when you write an assembler function that just happens to have the name "main".

    as it should be, there is no law forbidding a pure assembler program to have a routine named 'main'. for a pure assembler program you do not want the C initializer to be included.

    Erik

    PS so the smoked sardine will not need to spew his bile "C initializer" refer to startup.a51 and the other code it calls as anybody but him would understand

  • I think you would find that ?C_START is a run-time library function that (in turn) calls the traditional C function main. Almost certainly, the purpose of this mechanism would be to ensure that the critical parts of the run-time library are pulled into the executable

    I 'know' (because there is no other place) that ?C_START (also) initializes all global values that are to be initialized to other than zero.

    Erik

  • Follow up to the follow up ...

    If you don't want to modify the startup code, then just be explicit and include the run-time library into the project (or, for the non-ide fraternity, the linker command line).

    Again, hey presto, main in assembler.

  • Erik,
    As a "smoked sardine" myself, I find your references highly offensive :)