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

Mixing Assembly and C Code in KEIL

Hullo guys , I want to write some performance-critical sections in Assembly to be called from the C code , The C compiler in my KEIL is ARMCC and the Assembler is ARMASM .
Any help or guide to good documentation or examples ?

Parents
  • Thx for ur reply man . Well , it's my first time to write assembly Code for ARM (though this is not my issue , cuz i already wrote Assembly for other MCU) and I also have the documentation for the ARM instruction set .
    The problem is that I do not wanna step in Mixing unless im really sure how things will go , So is it like I make .asm file , then label the function then call it from the C file ? , or it requires more than just that ? >>>>>>>> THX in Advance .

Reply
  • Thx for ur reply man . Well , it's my first time to write assembly Code for ARM (though this is not my issue , cuz i already wrote Assembly for other MCU) and I also have the documentation for the ARM instruction set .
    The problem is that I do not wanna step in Mixing unless im really sure how things will go , So is it like I make .asm file , then label the function then call it from the C file ? , or it requires more than just that ? >>>>>>>> THX in Advance .

Children
  • Read the calling convention documentation (ABI), this will discuss the expectations of what registers do what, which need to be preserved etc.

    For simple routines parameters are passed in R0,R1,R2,R3 for the first four, data is returned in R0. You can use R0..R3 as scratch registers, R4+ would need to be preserved. If you call some other subroutines, you'd need to preserve LR. The rules are more complex than this, but this should get you started.

    ;*****************************************************************************
    ; In C code
    ; Prototype as
    ;  extern void FooAsm(void);
    ; Call as
    ;  FooAsm()
    
    FooAsm          PROC
                    EXPORT  FooAsm
    
    ; Params 1 thru 4 in R0..R3
    ; Returns R0
    
                    bx      lr ; return
                    ENDP ; FooAsm
    
    ;*****************************************************************************
    

  • THX Westonsupermare Pier , I know there r rules , but im actually speaking of building and linking in the first place . I do not want to use inline assembly (except for passing parameters)
    The example u gave is like .asm file and .c file , right?

  • Add the assembler file to the project. The IDE understands what to do with them. And if you aren't happy, then you can modify the rules for individual source files.

    When it gets to linking, the linker will not care if the object file came from a *.c file, a *.cpp file or a *.asm file. All it cares about is that the object files are of the proper format and exports meaningful symbols where all the "I require" can be matched with corresponding "I supply".

    But note that only add assembler if you need it - like having a teacher that requires you to do it, or using a tool chain that requires the startup file to be in assembler or when you need extra performance and also have the required skills to actually be able to produce faster code. Assembler-written routines are not automatically faster than compiled C/C++ code - todays compilers are much better at optimizing code than they were 20 years ago.

  • It's a portion of a .s file, describing a function, with comments on how to call it from a .c file.

    You could paste it into your existing startup.s file