Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ARM calling conventions on WinCE and Linux
The ARM processor architecture has become increasingly popular in embedded systems and mobile devices due to its low power consumption, high performance, and cost-effectiveness. It is widely used in operating systems such as Windows CE and Linux. This article discusses ARM calling conventions on these two platforms.
What is a Calling Convention?
A calling convention is a set of rules that dictate how function calls are made in a program. These rules define how arguments are passed, how return values are handled, and how the stack is managed during function calls. Several different calling conventions exist for ARM architecture, varying depending on the operating system being used.
ARM Calling Convention on Windows CE
Windows CE is a real-time operating system designed for small devices such as mobile phones and PDAs. The ARM calling convention used in Windows CE is called ARM Procedure Call Standard for Windows CE Operating System (APCS-CE).
APCS-CE Rules
Argument passing The first four arguments are passed in registers r0 to r3. Additional arguments are passed on the stack.
Return values Function return values are passed in register r0. For values larger than 32 bits, both r0 and r1 are used.
Stack management The caller pushes the return address onto the stack. The callee saves registers before using them.
Stack alignment The stack must be aligned to 8 bytes.
Example
int add(int a, int b) {
int c;
__asm {
mov r0, a
mov r1, b
bl sum
mov c, r0
}
return c;
}
int sum(int a, int b) {
return a + b;
}
In this example, the add() function takes two arguments (a and b) and returns their sum. The arguments are passed in registers r0 and r1, and the function call is made using the bl instruction.
ARM Calling Convention on Linux
Linux is a popular open-source operating system that runs on a wide range of devices. The ARM calling convention used in Linux is called ARM Procedure Call Standard (APCS).
APCS Rules
The APCS follows similar rules to APCS-CE
Argument passing First four arguments are passed in registers r0 to r3, with additional arguments on the stack.
Return values Function return values are passed in register r0 (or r0 and r1 for larger values).
Stack management Similar stack management with caller responsibility for return address.
Example
int add(int a, int b) {
int result;
asm volatile (
"mov r0, %1
\t"
"mov r1, %2
\t"
"bl sum
\t"
"mov %0, r0
\t"
: "=r" (result)
: "r" (a), "r" (b)
: "r0", "r1"
);
return result;
}
Comparison
| Aspect | APCS-CE (Windows CE) | APCS (Linux) |
|---|---|---|
| Stack Alignment | Required (8 bytes) | Not strictly required |
| Register Usage | r0-r3 for arguments | r0-r3 for arguments |
| Return Value | r0 (must use r0) | r0 (flexible) |
| Stack Management | Caller saves registers | Standard caller/callee |
Key Considerations
Compiler variations Different versions of GCC or other compilers may use different calling conventions for the same operating system.
Architecture modes ARM supports both 32-bit and 64-bit modes. In 64-bit mode (AArch64), arguments are passed in registers x0 to x7 instead of r0 to r7.
Additional conventions ARM architecture has other calling conventions, including those for Microsoft Visual C++ and LLVM compilers.
Conclusion
ARM calling conventions on Windows CE (APCS-CE) and Linux (APCS) share fundamental similarities but differ in stack alignment requirements and register management. Understanding these conventions is crucial for writing efficient and portable code on ARM architecture, especially when working with assembly language or low-level system programming.
