There are two main ways to program in assembler. One is to create the entire program with assembler, and the other is to write only some functions with assembler. The former is not realistic considering the low readability of the assembler. That is why the latter becomes the mainstream. Maintainability and high effective speed can be achieved by writing some functions in assembler and others in C.
This article assumes a Linux environment. Assembler is highly environment dependent and will only work on Linux. Please prepare the following software in the Linux environment.
Write the C program main.c below and the assembler program add.asm with an editor and save them.
main.c
#include <cstdio>
extern int asm_add(int a, int b);
int main()
{
int a, b;
a = 1;
b = 2;
int ans = asm_add(a, b);
printf("%d\n", ans);
return 0;
}
add.asm
section .text
global _sm_add
asm_add:
enter 0,0
mov eax, edi
mov ebx, esi
add eax, ebx
leave
ret
Compile main.c to generate an object file.
gcc -c -o main.o main.c
Compile add.asm to generate an object file.
#### **` nasm -g -f elf64 -o add.o add.asm `**
Link the generated object file.
gcc -o add -lc -g main.o add.o -o add
Now you have the executable add.
When executed
``` 3 ```
Is displayed on the screen.
# C program side commentary
I will explain the source of C. Very simple code. First of all, the function `` `int add (int a, int b)` `` is declared in extern.
In assembly files, header files cannot handle connections between objects as you normally would in C. Therefore, declare it with extern and specify the external link.
The other parts are the same as a normal C program.
Similarly, in C ++, you can call functions created in assembler.
# Assembler side commentary
Since it is a short code, it is explained line by line.
#### **` section .text Means the beginning of the code..Besides text.data(Declaration of initialized data)、.bss(Declaration of uninitialized data)There is a section of, but it is omitted in this code.`**
global _asm_add
Marker_asm_Add is specified globally so that it can be referenced from other codes.
Is a marker. Below this is the function asm_The body of add.
``` enter 0,0 ```Is an instruction to build a stack frame. The stack frame is the storage destination of the data expanded in the memory. It stores the variables used in the function and the addresses of the instructions to return after the function ends.
mov eax, edi mov ebx, esi
Moves the arguments a and b to general-purpose registers. In the Linux environment, function arguments are stored in registers and passed. This is to reduce the frequency of memory access and speed up the process. There is a one-to-one decision as to which register the data for which argument of the function is stored. The correspondence table is shown below.
|register|argument|
|:-------:|:------:|
| edi |First argument|
| esi |Second argument|
| edx |Third argument|
| ecx |Fourth argument|
| r8d |Fifth argument|
| r9d |Sixth argument|
From the 7th argument, it is exchanged via the stack. However, considering execution speed and readability, you probably don't need more than the 7th argument.
add eax, ebx
The contents of ebx and the contents of eax are added. In the Linux environment, the value of eax is used as the return value of the function.
``` leave
ret ```
```leave```Releases the stack.```enter```It is an instruction paired with.``` ret ```Then, it returns to the address of the function caller pushed to the stack by the call instruction.
# At the end
You can now call the assembly from c. This method allows the compiler to manually perform optimizations that are difficult to do automatically.
**Happy Hacking!!**
# References
* "System V Application Binary Interface AMD64 Architecture Processor Supplement"
http://x86-64.org/documentation/abi.pdf
* th0x4c Note: [GDB] Check the Linux X86-64 calling Convention with Gdb
http://th0x4c.github.io/blog/2013/04/10/gdb-calling-convention/
* Guide to Assembly Language Programming in Linux
http://www.amazon.co.jp/Guide-Assembly-Language-Programming-Linux/dp/0387258973
↓ ↓ Any comments will be encouraging ↓ ↓
Recommended Posts