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

creating assembly functions

How can I create an assembly function?

How can I call it from c?

Thanx..

  • 1) I like to use a text editor, and run the source file through the assembler.

    2) Declare the name of the function PUBLIC with an underscore in front:

    PUBLIC  _MyFunc
    _MyFunc:
            RET
    


    Then in C, you can write a header file with extern references to match, and call the function as you would any C function.

    extern void MyFunc (void);

    If you want to pass parameters, study the manual for the C parameter passing convention, and write your assembler function accordingly.

  • The simplest way to create a C-callable assembly file, is to start with an outline in 'C', then let the compiler turn it into assembly source by using the SRC directive - see the Manual.

    You can be certain that the compiler knows exactly how to name the function, pass parameters, allocate local variables, and return a value in a C-compatible way.

    Once you have created this assembly outline, throw the 'C' file away and just work on that file in assembler.

    Add the assembly file to your project, and uVision will automatically use the assembler to translate it

  • When I make something like this:

    1).src file with the following code

    PUBLIC _koko
    _koko:
    mov A,R7;
    mov B,R5;
    DIV AB;
    mov R7,B;
    RET
    end

    2)a C file as follows:
    #include <AT89X51.H>
    
    extern char koko(char c,char d);
    void main(void)
    {
    
    P0=koko(1,2);
    }

    It gives the following warning:

    *** WARNING L5: CODE SPACE MEMORY OVERLAP
        FROM:    0000H
        TO:      0006H

    How can this be resolved?
    Note that both files are included in the project list with the src file as the first file in the list

  • How can this be resolved?
    by following the following suggestion TO THE LETTER

    The simplest way to create a C-callable assembly file, is to start with an outline in 'C', then let the compiler turn it into assembly source by using the SRC directive - see the Manual

    Erik

  • Assembly language has an "ORG" directive that tells the assembler where the instructions are located. It also lets you declare "segments" to put your code into so that the linker can relocate them. Without the benefit of these mechanisms, your code will go at address 0000h, where it conflicts with the restart vector.

    Take a look at some of the example assembly to see how this works. Or, as Erik suggests, use the SRC directive and have the compiler generate a sample file from C that actually does what you want.

  • "use the SRC directive and have the compiler generate a sample file from C that actually does what you want."

    As I said before, if you let the compiler do it, you can be certain that the compiler will generate code using all the correct conventions for 'C'-compatibility!