Mais conteúdo relacionado
Calling_Assembly_Functions_From_C_in_ARM_Programming
- 2. Introduction
• About the ARM Compiler tool chain
o The ARM Compiler tool chain enables you to build applications for the ARM family of
processors from C, C++, or ARM assembly language source. The tool chain comprises:
o armcc - The ARM and Thumb® compiler. This compiles your C and C++ code. It supports
inline and embedded assemblers.
o armasm - The ARM and Thumb assembler. This assembles ARM and Thumb assembly
language sources.
o armlink -The linker. This combines the contents of one or more object files with selected
parts of one or more object libraries to produce an executable program.
o armar - The librarian. This enables sets of ELF format object files to be collected together
and maintained in archives or libraries. You can pass such a library or archive to the linker in
place of several ELF files. You can also use the archive for distribution to a third party for
further application development.
o fromelf - The image conversion utility. This can also generate textual information about the
input image, such as disassembly and its code and data size.
©rangineni balasubramanyam
- 3. Introduction
• Using the compilation tools
• A typical application development might involve the following:
C/C++ source code for the main application (armcc)
Assembly source code for near-hardware components (armasm), such as interrupt
service routines
Linking all objects together to generate an image (armlink)
Converting an image to flash format in plain binary, Intel Hex, and Motorola-S formats
(fromelf).
©rangineni balasubramanyam
- 4. Introduction
• The ARM architecture, like most 32-bit architectures, is well-suited to a using a C or C++
compiler. The majority of control code is written using high-level programming languages
like C and C++ instead of assembly language.
• However, it is sometimes necessary to use a little bit of assembly code to use of target
processor features that cannot normally be accessed directly from C or C++. For example:
– saturating arithmetic
– custom coprocessors
– the Program Status Register (PSR).
• Inline and embedded assembler are built into the ARM compiler to enable above features.
• The inline assembler supports interworking with C and C++. Any register operand can be an
arbitrary C or C++ expression. The inline assembler also expands complex instructions and
optimizes the assembly language code.
• The embedded assembler enables you to use the full ARM assembler instruction set,
including assembler directives. Embedded assembly code is assembled separately from the C
and C++ code. A compiled object is produced that is then combined with the object from the
compilation of the C and C++ source.
©rangineni balasubramanyam
- 5. Mixing C and Assembly
• You can mix calls between C and C++ and assembly language routines provided you comply
with the Procedure Call Standard for the ARM Architecture (AAPCS).
• We can use the inline assembler or embedded assembler, which are provided by the C/C++
compiler.
• The __asm keyword can be used to declare or define an embedded assembly function.
For example:
__asm void my_strcpy(const char *src, char *dst);
• The __asm keyword can be used to incorporate inline assembly into a function. For example:
int qadd(int i, int j)
{
int res;
__asm { QADD res, i, j }
return res;
}
©rangineni balasubramanyam
- 6. Compiler, Assembler and Linker
• This presentation is going to discuss about creating functions in assembly language and calling
those functions from C.
• Function written in assembly goes through the ARM assembler(armasm) and generates the
object code.
• Functions written in C goes through the ARM C/C++ compiler(armcc) and generates the
object code
• The ARM linker(armlink) combines the contents of one or more object files with selected
parts of one or more object libraries to produce an executable program.
• Finally the fromelf image converter process ARM ELF object and image files produced by the
compiler, assembler, and linker.
• Convert ELF images into other formats that can be used by ROM tools or directly loaded into
memory. The formats available are:
– Plain binary
– Motorola 32-bit S-record
– Intel Hex-32
– Byte oriented (Verilog Memory Model) hexadecimal
– ELF. You can resave as ELF, for example, to remove debug information from an ELF image.
©rangineni balasubramanyam
- 9. Calling from C
/* main.c – calling function */
extern void strcopy(char *d, const char *s);
int main(void)
{
const char *srcstr = "First string - source ";
char dststr[] = "Second string - destination ";
/* function calling - function defined in assembly in other source file*/
strcopy(dststr, srcstr);
return (0);
}
©rangineni balasubramanyam
- 10. Function declaration
• In the above program we are calling a function
strcopy()
which was defined in the assembly language, example provided in next slides.
• We are informing the compiler that a function with the name strcopy is defined with the
arguments of particular data types, the order of arguments and the return type of the function
by using declaration
extern void strcopy(char *d, const char *s);
• By using 'extern', you are telling the compiler that whatever follows it will be found (no
technically, every function in a library public header is 'extern', however labeling them as
such has very little to no benefit, depending on the compiler. Most compilers can figure that
out on their own. As you see, those functions are actually defined somewhere else.n-static) at
link time.
©rangineni balasubramanyam
- 11. Function in Assembly
/* function.s – string copy function implemented in assembly */
AREA FUNC, CODE, READONLY
strcopy PROC
EXPORT strcopy
LDRB R2, [R1],#1 ; Load byte and update address.
STRB R2, [R0],#1 ; Store byte and update address.
CMP R2, #0 ; Check for null terminator.
BNE strcopy ; Keep going if not.
BX lr ; Return.
ENDP
END
©rangineni balasubramanyam
- 12. Code explained
• AREA
– The AREA directive instructs the assembler to assemble a new code or data section. Sections are
independent, named, indivisible chunks of code or data that are manipulated by the linker.
Syntax
AREA sectionname {,attr}{,attr}...
Sectionname is the name to give to the section.
You can choose any name for your sections
• CODE specified that the code Contains machine instructions. READONLY is the default.
• READONLY Indicates that this section must not be written to. This is the default for Code areas.
• The following example defines a read-only code section named FUNC.
AREA FUNC, CODE, READONLY
• The END directive informs the assembler that it has reached the end of a source file.
©rangineni balasubramanyam
- 13. Code explained
• The FUNCTION directive marks the start of a function. PROC is a synonym
for FUNCTION.
Syntax
label PROC [{reglist1} [, {reglist2}]]
Where:
o reglist1 is an optional list of callee saved ARM registers. If reglist1 is not present, and
your debugger checks register usage, it will assume that the AAPCS is in use.
o reglist2 is an optional list of callee saved VFP registers.
• Use PROC to mark the start of functions. The assembler uses PROC to identify the start of a
function when producing DWARF call frame information for ELF.
• Each PROC directive must have a matching ENDP directive
• Explanation of function logic is above this presentation.
©rangineni balasubramanyam
- 14. Code explained
• The EXPORT directive declares a symbol that can be used by the linker to resolve symbol
references in separate object and library files. GLOBAL is a synonym for EXPORT.
• Syntax
EXPORT symbol {[SIZE=n]}
• Use EXPORT to give code in other files access to symbols in the current file.
• Symbol is the symbol name to export. The symbol name is case-sensitive. If symbol is
omitted, all symbols are exported.
• SIZE = n
o Specifies the size and can be any 32-bit value. If the SIZE attribute is not specified, the
assembler calculates the size:
o For PROC and FUNCTION symbols, the size is set to the size of the code until
its ENDP or ENDFUNC.
o For other symbols, the size is the size of instruction or data on the same source line. If
there is no instruction or data, the size is zero.
©rangineni balasubramanyam
- 15. Parameter passing
• The standard ARM calling convention allocates the 16 ARM registers as:
o r0 to r3: used to hold argument values passed to a subroutine, and also hold results returned
from a subroutine.
o r15 is the program counter.
o r14 is the link register. (The BL instruction, used in a subroutine call, stores the return
address in this register).
o r13 is the stack pointer. (The Push/Pop instructions in "Thumb" operating mode use this
register only).
o r12 is the Intra-Procedure-call scratch register.
o r4 to r11: used to hold local variables.
o If the type of value returned is too large to fit in r0 to r3, or whose size cannot be
determined statically at compile time, then the caller must allocate space for that value at
run time, and pass a pointer to that space in r0.
• In the above code, the parameters are passed through r0 and r1, that’s the reason we are able to
access the parameters passed by the callee by using r0, r1
©rangineni balasubramanyam
- 16. References
• ARM info centre
• Code can be obtained from the git repository
https://github.com/ranginenibalu/assem_func.git
• http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0477c/index.
html
• http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/BABF
DCGD.html
• http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.set.swdev/index.
html
©rangineni balasubramanyam