# How to add inline assembly code in c

### The syntax of the asm statement in C code is:

`asm ("assembly code" : output operand(s) : input operand(s) : clobbered registers);`

The colon `:` is used as a separator to separate the input and output operands from the assembly code.

### REAL CODE example:

```c
int main(int ac, char **av) 
{ 
    int fd = 2;
    asm ("mov %1, %0\n\t" 
            "add $3, %0" 
            : "=r" (fd) 
            : "r" (fd));
/* other code */
}
```

### Let's break it down:

asm: This is a GCC extension that allows assembly code to be inserted into C or C++ programs.

`("mov %1, %0\n\t" "add $3, %0" : "=r" (fd) : "r" (fd)):` This is the actual assembly code that is being executed. Here's what each part of the code does:

`"mov %1, %0\n\t"`: This moves the value of the second argument (represented by %1) into the first argument (represented by %0). The \\n\\t represents a newline followed by a tab character.

`"add $3, %0"`: This adds the value 3 to the first argument (%0).

`: "=r" (fd)`: This is the output operand constraint. It specifies that the result of the assembly code should be stored in the variable fd and that it should be placed in a register that can be read by the C code (the r constraint).

`: "r" (fd)`: This is the input operand constraint. It specifies that the value of the variable fd should be passed as an input to the assembly code and that it should be placed in a register that can be read by the assembly code (the r constraint).

Overall, this code moves the value of the file descriptor fd into a register that can be accessed by the assembly code, adds 3 to that register, and then stores the result back in the variable fd
