Computers use rules to talk. One part asks another part to do a job. They must follow the same rules to work. This helps them share things. It is like a game with rules. Do you like games with rules?
Computers use rules to talk. One part asks another part to do a job. They must follow the same rules to work. This helps them share things.
These rules tell the computer where to put information. It is like a set of directions. The rules say where to send items. They also say where to find the answer.
Some rules use small spots to hold data. These spots are called registers. Other rules use a list called a stack. This stack helps keep things in order.
Rules can also decide the order of things. They might send items from left to right. They might even send them from right to left.
If everyone follows the rules, the program works well. It makes sure the computer does not get lost. Following rules keeps everything running smoothly.
Computers use special rules to share work. These rules are called calling conventions. When one part of a program asks another part to do a job, they must follow these rules. This helps the program run in a reliable way.
These rules act like a contract. They decide how to pass information back and forth. This information is called parameters. The rules also decide how to send back an answer. We call this a return value.
There are many ways to move this data. Some rules use small storage spots called registers. Other rules use a list called a stack. The rules also decide the order of the items. They might send them from left to right or right to left.
Different computer systems use different rules. For example, the x86 system uses a stack for many tasks. The ARM system uses many registers to pass data. Some rules even decide how to clean up after a job is done. This makes sure the computer is ready for the next task.
In computer science, a calling convention is a set of rules for how different parts of a program talk to each other. When one part of a program, called the caller, asks another part, called a function, to do a job, they must follow a specific plan. This plan decides how to pass information, known as parameters, to the function. It also decides how the function sends its answer, called a return value, back to the caller. Without these rules, the different parts of a program would not understand each other. Using the right convention ensures that the whole program runs in a reliable way.
These rules work like a contract between the two parts of the program. The convention decides where to put the data being shared. Some rules use small, fast storage spots called registers. Other rules use a list in the computer's memory called a stack. The rules also decide the order of the parameters, such as passing them from left to right. They even decide who is responsible for cleaning up the mess after the job is done. This includes restoring the computer's environment so the caller can continue its own work.
Different types of computer hardware use different sets of rules. For example, the 32-bit x86 architecture often uses the stack to pass information. This is because it has a small number of registers available. However, the 64-bit x86-64 version has more registers to use. Because of this, both Microsoft's Windows convention and the AMD64 System V convention pass some arguments in registers. This change can make the computer work faster.
Other systems like ARM have very specific ways of using their registers. In the 32-bit ARM system, registers r0 through r3 are used to pass arguments and return results. The system also uses a special register called the link register, r14, to remember where to go back to. The 64-bit ARM system, or AArch64, uses even more registers, such as x0 through x7 for arguments. In the RISC-V system, the rules try to pass arguments in registers whenever it is possible. Some systems, like POWER, have so many registers that they can pass almost all arguments without using the stack.
Even though these rules are very low-level, they connect to things you might know. They are part of the application binary interface, or ABI. You can think of the ABI as the master set of rules for how software and hardware fit together. While a programmer might not see these rules every day, they are working in the background. They allow different languages and different parts of an operating system to work as one team. This teamwork is what allows your computer to run many different tasks at once.
In computer science, a calling convention is a low-level scheme for managing communication between subroutines. A subroutine, or function, is a specific block of code designed to perform a task. When one part of a program, known as the caller, triggers a function, they must follow a strict set of rules. These rules dictate how parameters are passed to the function and how results are returned. This process ensures that different parts of a program can interact reliably. Without a shared convention, the caller and the function might look for data in different places, causing the program to fail.
These conventions act as a contract between the caller and the called function. This contract is typically considered part of the application binary interface (ABI). The ABI defines how software interacts with the underlying hardware and operating system. It is different from an application programming interface (API). An API defines the names and meanings of parameters and return values. In contrast, the ABI handles the actual physical transfer of data. This transfer usually happens through specific registers or within a stack frame on the call stack. The call stack is a specialized region of memory used to manage function calls.
Calling conventions involve many specific design choices regarding data movement. One major choice is where parameters are placed. They can be stored in registers, which are fast storage locations inside the CPU, or on the stack in main memory. Some systems use a mix of both. Another choice involves the order of parameters, such as passing them from left-to-right or right-to-left. Conventions also determine how return values are delivered. A function might return a result in a register or by placing it on the stack. Even the handling of complex values is decided here, such as splitting a large value across multiple registers.
Another critical aspect is the division of labor between the caller and the callee. The callee is the function being called. The convention decides who is responsible for preparing the environment before the call and cleaning it up afterward. This includes restoring the stack frame so the caller can continue its work. This involves managing the frame pointer, which helps track the current state of the stack. Some conventions also specify which registers must be preserved. These are called non-volatile registers, meaning their values must remain unchanged after the function finishes. This ensures the caller does not lose its own data during the process.
Different hardware architectures use very different calling conventions. The 32-bit x86 architecture has a limited number of registers. Because of this, many x86 conventions pass arguments on the stack. However, the 64-bit x86-64 architecture has more general-purpose registers. Consequently, both the Microsoft convention used in Windows and the AMD64 System V ABI used in Unix-like systems pass some arguments in registers. Using registers instead of the stack can significantly improve performance. This is especially true for "leaf routines," which are simple functions that do not call any other functions.
ARM architectures provide even more detailed examples of register allocation. In 32-bit ARM (A32), registers r0 through r3 are used for arguments and results. The system also uses a special link register, r14, to store the return address. This allows the function to know where to go once it is finished. In 64-bit ARM (AArch64), the system uses 31 general-purpose registers. For instance, x0 through x7 are dedicated to arguments and results. ARM also mandates a "full-descending stack" and requires the stack pointer to be aligned to specific byte boundaries. These strict rules allow for highly efficient execution on mobile and embedded devices.
Other architectures like RISC-V and POWER also follow specific patterns. RISC-V aims to pass arguments in registers whenever possible to maximize speed. The POWER architecture features a very large number of registers. This allows most functions to pass all arguments through registers for single-level calls. Even when registers are full, POWER architectures allocate space on the stack for these arguments as a convenience. This helps in complex scenarios, such as recursive calls where a function calls itself multiple times. These diverse methods show how calling conventions are tailored to the specific strengths of different computer designs.
More to explore
✨ What else?
Related topics you might enjoy
🔬 Go deeper
More advanced topics to explore
🪜 Step back
Simpler topics to build understanding
What is Nepedia?
A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.