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

Parents
  • 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

Reply
  • 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

Children