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

inserting assembly code in c language code

Hi
is there any way that i could insert some lines of assembly code in my code which was written in keil( C language).
please tell

  • Yes, Neil. I agree with you. Sorry for forgetting it. Mehboob once you insert your code in between #pragma asm ... #pragma endasm directive, you need to set 2 options for that perticular file, which are:
    1. Generate Assembler SRC file
    2. Assemble SRC file

    Thanks.
    Pankaj

  • Hi thanks for replying but i m still recieving some problems
    here is my code
    void main(void)
    {
    serial_init();
    P2=0X33;
    #pragma asm

    DELAY:
    MOV R4,#4
    DEL:

    MOV R2,#00F3H
    DJNZ R2,$
    DJNZ R4,DEL
    RET

    L_DELAY:
    MOV R1,#10
    LARGE_DELAY:

    MOV R4,#35
    L_DEL:

    MOV R2,#00FFH
    DJNZ R2,$
    DJNZ R4,L_DEL
    DJNZ R1,LARGE_DELAY
    #pragma endasm
    }
    i checked both the options you mentioned but when i build my code it will compile successfully with no error but with one warning.
    warning is:
    L1: unresolved external symbol
    symbol: ?c_startup
    module: test.obj

    what is this i could not be able to find it out. please help

  • Mudassir,

    When you tell uVision to have the compiler generate ASM and then assemble them, some of the things it would automatically do for you don't happen. The upshot of it is this: You must manually copy and add the file INIT.A51 that is included with Keil's package to your project. Then things will compile correctly for you.

  • thanks for replying

    but it could not resolve my problem that warning still occurs, i have added init.a51 to my project and copied it to the project folder.
    please tell me some other way to tackle with this problem.

  • hi,

    ASM and ENDASM directives indicate the beginning of a block of source text to merge into the .SRC file.

    In your case: if you need with delay or long delay and you wish to write it with assembler then either create separate file with assembler code (.a51) or do it like this:

    void my_delay()
    {
    #pragma asm
    
    DELAY:
    MOV R4,#4
    
    DEL:
    MOV R2,#00F3H
    DJNZ R2,$
    DJNZ R4,DEL
    #pragma endasm
    }
    
    then call my_delay() as a function from rest C code.


    If you need to include inline assembler block into C code then do not use RET here:
    void main(void)
    {
    serial_init();
    P2=0X33;
    #pragma asm
    
    DELAY:
    MOV R4,#4
    DEL:
    
    MOV R2,#00F3H
    DJNZ R2,$
    DJNZ R4,DEL
    RET            ; <- here is the error
    ; because it breaks C function and returns to not defined address
    ; due main() is root function which is not called from anywhere.
    

    Regards,
    Oleg

  • Thanks for reply but i followed your adivce but it did'nt work. actually i want to introduce assembly code in my c program because i want to insert a calculated delay in my program. so please any body kindly either tell me the way to do it in c or tell me the way to include assembly lines in my c code.
    Bye

  • tell me the way to do it in c or tell me the way to include assembly lines in my c code.
    Neither
    Create a separate .a51 module and call the assembly code from your C

    Erik

  • Mudassir,

    Did you add STARTUP.A51 as well? I'm unable to reproduce the problem you're having. I can create a new project with only a main() function, set the options mentioned above, and then use pragma asm/endasm to insert some assembly statements. Mine gave the same warning, but when I added INIT.A51 (in addition to the already included STARTUP.A51), everything compiled fine.