This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
programming:assembly:assembly_programming [2021/03/21 07:37] nanodano |
programming:assembly:assembly_programming [2021/03/22 02:22] (current) nanodano ↷ Page name changed from programming:assembly:home to programming:assembly:assembly_programming |
||
---|---|---|---|
Line 3: | Line 3: | ||
====== Assembly Programming ====== | ====== Assembly Programming ====== | ||
+ | Assembly is about as close as you can get to machine code. | ||
There are many Assembly compilers out there. '' | There are many Assembly compilers out there. '' | ||
Line 8: | Line 9: | ||
Use interrupt '' | Use interrupt '' | ||
- | For a list of system calls in Linux, refer to [[https:// | + | For a list of system calls in Linux, |
Here is an example of compiling with '' | Here is an example of compiling with '' | ||
Line 47: | Line 48: | ||
int 80h ; call the kernel | int 80h ; call the kernel | ||
+ | </ | ||
+ | |||
+ | Compile and run with: | ||
+ | |||
+ | <code bash> | ||
+ | # Compile | ||
+ | nasm -f elf64 hello.asm -o hello.o | ||
+ | |||
+ | # Link | ||
+ | ld hello.o -o hello | ||
+ | |||
+ | # Run | ||
+ | ./hello | ||
</ | </ | ||
===== x86 on DOS ===== | ===== x86 on DOS ===== | ||
Line 59: | Line 73: | ||
For a list of BIOS system calls, refer to [[https:// | For a list of BIOS system calls, refer to [[https:// | ||
- | ===== Writing | + | ===== Create |
You can write and compile libraries that can be linked against by other programs. | You can write and compile libraries that can be linked against by other programs. | ||
Line 91: | Line 105: | ||
</ | </ | ||
- | ===== Call C function from Assembly ===== | ||
- | <code c call_c.asm> | + | ===== Create an executable |
- | // Compile with | + | |
- | // gcc call_c.c static_lib.o | + | |
- | // Automatically links the elf binary .o generated by | + | |
- | // the assembly compiler to resolve references | + | |
- | #include < | + | |
- | + | ||
- | int main(int argc, char *argv[]) { | + | |
- | + | ||
- | // Define external from a separate library | + | |
- | // Defined in something.o from a compiled | + | |
- | // assembly program | + | |
- | extern print_hello(); | + | |
- | + | ||
- | // Call external assembly function | + | |
- | print_hello(); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | ===== Creating a C main function | + | |
+ | By creating a '' | ||
<code asm c_main.asm> | <code asm c_main.asm> | ||
Line 144: | Line 138: | ||
int 80h ; call the kernel | int 80h ; call the kernel | ||
</ | </ | ||
+ | |||
+ | And compile and run with: | ||
+ | |||
+ | <code bash> | ||
+ | nasm -f elf64 c_main.asm | ||
+ | gcc c_main.o | ||
+ | ./a.out | ||
+ | </ | ||
+ | |||
+ | ===== Call Assembly functions from C ===== | ||
+ | |||
+ | <code c example.c> | ||
+ | /* example.c */ | ||
+ | /* Compile and run with `gcc example.c say_hi.o -o hello` */ | ||
+ | #include < | ||
+ | |||
+ | int main(int argc, char *argv[]) { | ||
+ | extern say_hi(); | ||
+ | say_hi(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Next, compile and link the C program with gcc. Do that with: | ||
+ | |||
+ | <code bash> | ||
+ | gcc example.c say_hi.o -o hello | ||
+ | </ | ||
+ | |||
+ | You can then run the '' | ||
+ | |||
+ | <code bash> | ||
+ | ./hello | ||
+ | </ | ||
+ | |||
+ | ===== Call C function from Assembly ===== | ||
+ | |||
+ | |||
+ | You can call C functions from Assembly as well. This example calls printf(). In the main function, we push and pop the stack and put our operations in between. We move all the parameters in to the appropriate registers, and then we call the function. | ||
+ | |||
+ | <code asm printf.asm> | ||
+ | ; printf.asm | ||
+ | ; Define printf as an external function | ||
+ | extern printf | ||
+ | |||
+ | SECTION .DATA | ||
+ | msg: db "Hello world", | ||
+ | fmt: db " | ||
+ | |||
+ | SECTION .TEXT | ||
+ | global main | ||
+ | | ||
+ | main: | ||
+ | push rbp ; Push stack | ||
+ | |||
+ | ; Set up parameters and call the C function | ||
+ | mov rdi,fmt | ||
+ | mov rsi,msg | ||
+ | mov rax,0 | ||
+ | call printf | ||
+ | |||
+ | pop rbp ; Pop stack | ||
+ | |||
+ | mov rax,0 ; Exit code 0 | ||
+ | ret ; Return | ||
+ | </ | ||
+ | |||
+ | Compile and run that with: | ||
+ | |||
+ | <code bash> | ||
+ | nasm printf.asm -f elf64 -o printf.o | ||
+ | gcc printf.o | ||
+ | ./a.out | ||
+ | </ | ||
+ | |||
+ | |||