Always pass -mno-red-zone to kernel code (even support libraries such as libc's embedded in the kernel) if interrupts don't respect the red zone. This is not done by the CPU - if interrupts use the current stack (as with kernel code), and the red zone is enabled (default), then interrupts will silently corrupt the stack. This requires the kernel to increase %rsp by an additional 128 bytes upon signals in user-space. Note 2: There is a 128 byte area below the stack called the 'red zone', which may be used by leaf functions without increasing %rsp. Note 1: The called function is allowed to modify the arguments on the stack and the caller must not assume the stack parameters are preserved. It may be useful to write a test function and use gcc -S to see how the compiler generates code, which may give a hint of how the calling convention specification should be interpreted. Look up the specifications if you want to be certain. Note that the calling conventions are usually more complex than represented here (for instance, how is a large struct returned? How about a struct that fits in two registers? How about va_list's?). Here is a quick overview of common calling conventions. Most of the burden for following the calling rules falls upon the assembly program. Rough draft of a book on ARM assembly-language programming.As a general rule, a function which follows the C calling conventions, and is appropriately declared (see below) in the C headers, can be called as a normal C function. Tips for mixed C and assembly programming Try to fill in the remaining missing critical functionality.Revert to the properly-linking version of the assembly code.Invariably, I discover something has broken so badly it doesn't even finish linking. Fill in the remaining missing critical functionality.Commit the properly-linking assembly language file to the version control system (even though some critical functionality is still missing).the unmodified test code to make sure it actually compiles and links and runs the way I expect. Test the system - assemble, compile, link, etc.copy-and-paste that assembly-language skeleton into my assembly language.
compile it with the special option that produces a text file full of assembly-language mnemonics.write a "similar" piece of temporary test code in C - a stub that sends the same types of parameters to the function and getting the same types of return values, but perhaps missing some critical functionality.When I want to call a C function from some assembly language, I If none of those little details helps, you might consider using the following general process: General process Perhaps trying a b c d or e ? if you are lucky, at least one will work for you. Have you considered possibly using something more like the hard fault handlers already written and tested by other people? (It's possible that, like many older C compilers, the compiler you're using now absolutely requires writing things like hard fault handlers and interrupt vector tables in assembly language - have you considered maybe upgrading to a more modern C toolchain such as GNUARM or YAGARTO that uses gcc ?) Today's C compilers often support special syntax that lets you write such handlers in more-or-less pure C - often with something like a "_interrupt" specifier, but alas the exact details vary from one compiler to another. Have you considered maybe writing that bit of code in C, rather than assembly language, and letting the compiler handle the calling convention details? I only use the "extern" keyword on global variables, not on global functions, exactly as Kanwar Saad describes. (The assembly-language name of C++ functions has much more decoration). To refer to a C function that is defined in C without an underscore ("SavePC"). Often assembly language must use a name decorated with an underscore ("_SavePC")
The c file with the assembly includes the header file with the SavePC prototype. Error: Unknown symbol in inline assembly: "IMPORT"Įrror: Unknown symbol in inline assembly: "SavePC"Įrror: Syntax error in inline assembly: "Error: Expression can not be forward" I have tried to IMPORT the function like so. However, when SavePC is placed in another c file I have no luck. This works fine when SavePC is in the same c file. The assembly is basically meant to pass the current stack pointer as a parameter (in R0). I have a bit of assembly in a hard fault handler. Not getting much traction over there either. I have posted a duplicate question in stackoverflow, however this may be just as appropriate here given that it's for an embedded platform and a number of embedded developers frequent this stackexchange.
I don't know if this is recommended or not but I am sure someone will give me a stern warning if not.