--- title: Calling assembly code from C visibility: public glu: - bash - bash run.sh --- This is going to be a transcription of [Nir Lichtman's video](https://www.youtube.com/watch?v=d8eDTE_MaEQ). First, the assembly: ```asm ; ext.s .intel_syntax noprefix .global addNumbers .text addNumbers: add rdi, rsi mov rax, rdi ret ``` This should result in one symbol (`addNumbers`), using System V AMD64 calling conventions to consume 2 integers off of registers, and put the result in the RAX register. We're going to call this from C, which is not only the point of the exercise, it makes it a lot simpler to output things to STDOUT. ```c // main.c #include int addNumbers(int a, int b); // Declare without defining int main() { printf("Sum of 5+7: %d\n", addNumbers(5,7)); } ``` Finally, the commands that compile and run our code. ```bash # run.sh set -ex as ext.s -o ext.o # Compile asm first gcc main.c ext.o -z noexecstack # compile main.c linking ext.o ./a.out # Run it ```