Now that we're 64-bit: Let's interact with a C program! A minor compatibility issue: Executable stacks We don't actually need one Could set as a linker option with -z Could also put a .section .note.GNU-stack in the assembly C libary used from Assembly: Write the library in a c file Compile it with gcc -c -o library_name.o source_code.c Assemble your program as usual Link it all together! Probably something like this: ld asm_object.o library_name.o -lc -z execstack --dynamic-linker /lib64/ld-linux-x86-64.so.2 Run the binary that comes out of all this Assembly library used from C: Write the library, build with as as usual Write the C program and use the function Make a C prototype for the function, either in the main file or a header Build the program with gcc or clang: gcc c_source.c asm_library.o -z execstack Could we make a build script for this? Of course! Today's topic: Putting assembly into C programs Can you work with the compiler? Reference I used 8 years ago: http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html Reference I was using last year (gcc documentation): https://gcc.gnu.org/onlinedocs/gcc/Using-Assembly-Language-with-C.html basic asm directive: Let's just put some assembly into a C program! We can make it do something extended asm: We can have compiler-assigned registers now! Example: Change a variable that's in our program basic vs. extended asm syntax: %%rax vs. %rax Can have four sections assembler template: - This is assembly code, but will be processed further - Template in CS: Something which will be filled out later - Use %% for regs here - %n for registers identified in input/output section output operands: - which variables are used for output from the template? - Example: "=r"(foo), "=a"(bar) = is a modifier, means that the initial value of the variable is not used - "r" will let the compiler pick a register, "a" picks ax / eax / rax input operands: - Same thing, for input clobbered registers: - clobbering defined - gcc can work with you on the optimization volatile: Might produce side effects the compiler won't know about As in, the optimizer is not allowed to leave it out inline: More accurate size calculation Normally gcc kind of guesses goto: Might jump to one of the listed labels If you just jump, it might confuse the compiler You can jump out of an asm, or to a different one Why wouldn't we need extended asm for anything productive? Let's define a function in a basic asm directive Parameters will be where we expect Clobbers are dictated by calling convention