SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Calling Assembly functions from C in
ARM programming
By Rangineni Balasubramanyam
©rangineni balasubramanyam
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
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
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
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
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
The flow
main.c
main.o
Function.s
Function.o
armcc ( C/C++ compiler) armasm ( arm assembler)
armlink
(linker)
FromELF
.hex
©rangineni balasubramanyam
Now, lets see an example
©rangineni balasubramanyam
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
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
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
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
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
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
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
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
©rangineni balasubramanyam

Weitere ähnliche Inhalte

Kürzlich hochgeladen

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Kürzlich hochgeladen (20)

%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Calling_Assembly_Functions_From_C_in_ARM_Programming

  • 1. Calling Assembly functions from C in ARM programming By Rangineni Balasubramanyam ©rangineni balasubramanyam
  • 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
  • 7. The flow main.c main.o Function.s Function.o armcc ( C/C++ compiler) armasm ( arm assembler) armlink (linker) FromELF .hex ©rangineni balasubramanyam
  • 8. Now, lets see an example ©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