Interfacing Assembly Language with C
There are situations when it is necessary to mix assembly language with C. For example, there are programs that may require particular performance-critical routines to run at optimum speed. These routines must be hand-coded. To interface assembly language with C, you have to follow a few programming rules. A programmer can call assembly routines from C and vice-versa. Additionally, the public variable declared in assembly modules can be made available to a C program.
There are a plethoraof reasons why you can call assembly routines from C. Some of them include:
- Access to SFRs directly using assembly
- Faster program execution
As a competent programmer, you must know how to pass arguments or parameters to function and get return values from a function. This will enable any assembly routine to be called from a C program. Programming in assembly can sometimes be inefficient. To use C/C++ for first prototyping. The first thing to do is to prepare the GNU toolchain for ARM (assembler, compiler, linker, etc.).
AOF object files can be generated from assembly language and C using appropriate tools. The files can then be linked with one or more libraries to produce an executable file. The arm software development kit supports this as shown below:
It doesn’t matter the language in which routines that make cross-calls to other modules are written. They need to observe a common convention of result passing and argument. This convention is called the Arm Procedure Call Standard (APCS) for the ARM.
How the Arm Procedure Call Standard is used
APCS is a set of instructions that govern calls between functions in separately assembled or compiled code fragments. It defines the following:
- Stack conventions
- Result return and argument passing
- Support for the shared library mechanism shared by the ARM
- Constrains on registers use
- Stack backtraces data structure format
All codes produced by compilers are expected to adhere to the APCS at all times. In other words, these codes should be strictly conforming. Moreover, handwritten codes should conform to the APCS when making calls to extremely visible functions. The APCS comprises a family of variants. Each of these variants is exclusive. This means that one can only be used with the code that conforms to it. The choice of a variant will depend on if:
- The program counter is 26-bit or 32-bit
- The stack checking limit is implicit (memory management hardware) or explicit (performed by the code)
- The code is non-reentrant or reentrant
- Floating-point values are passed in floating point registers.
This article assumes that you have prior knowledge of assembly language and 8086 microprocessors. If you are not well-versed in these areas then we recommend that you get the help of our programming experts or attend classes. Nonetheless, “CALL” mnemonic is the simplest form of subroutine invocation. Additionally, you should note that the stack frame is quite different for “NEAR CALL” and “FAR CALL”.
Parameters are passed on the stack by high-level languages like C. This is because such languages use memory differently and a common area is needed for subroutine interfacing. As a result, the accepted standard is stack. “PUSH” mnemonic is used to store 16-bit data while “POP” is used to retrieve data on stack. The data value of a short numeric integer is usually pushed on stack. Also, a pointer to the string is passed if the parameter is an alphanumeric string.
We can pass floating-point format numbers either by direct value or pointer.
The following ways are used to return values
The methods mentioned above are not automatic. As a result, we must predetermine the method we are going to use. This must be made clear in assembly subroutine documentation.
General guidelines for interfacing assembly language with C/C++
We can use two ways to interface assembly language with C or C++:
- Specifying a language using assembler directives
This is a feature available with TASM (Turbo Assembler) and MASM (Microsoft Macro Assembler)
- Do manually what can be done automatically
This is an old method.
The following are necessary points that are absolutely necessary for assembly language interfacing:
- If you are using Turbo C or Microsoft C compilers, then you must give the code segment name _TEXT.
- Also, if you are using the aforementioned compilers, then you must give the data segment name _DATA. Remember that these names vary from compiler to compiler.
- Understand how parameters are passed in C.
- The routines of assembly language that are to be called from a C program must start with an underscore ( _ ) character. For example, _myfunct
- You should not forget to save any special purpose registers like DS, SS, CS, etc. Failing to do this may lead to undesired effects when control is returned to the C program.
Hire our assembly language homework experts and get to submit accurate solutions for your assignments.