/* inline asm in c */
int main(void) {
  asm("xor %rax, %rax");
}
/* exit syscall*/
int main(void) {
  /* reset rax */
  asm("xor %rax, %rax");

  /* set rax to 1 (exit syscall) */
  asm("mov $1, %rax");

  /* reset rdi */
  asm("xor %rdi, %rdi");

  /* set rdi to 0 the first argument */
  asm("mov $0, %rdi");

  /* interruption */
  asm("syscall");
}
; void t(int a)
mov    %edi,-0x4(%rbp)

; void t(long a)
mov    %rdi,-0x8(%rbp)

; void t(short a)
mov    %di,%ax
mov    %ax,-0x2(%rbp)

; void t(int *a)
mov    %rdi,-0x8(%rbp)

; int t(int *a)
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    (%rdi),%eax

; int t(int *a, int size)
mov        %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    (%rdi),%eax

; int t(int *a, int s) { int b = *a; ... }
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    (%rdi),%esi
mov    %esi,-0x10(%rbp)
mov    -0x10(%rbp),%eax

; t(int *p, int s)
sub    $0x1010,%rsp
mov    $0x400,%esi
lea    -0x1000(%rbp),%rdi
callq  0x400710 <t>
/* printf function call*/
main:
  pushq %rbp
  leaq .string, %rdi
  leaq .value, %rsi
  movb $0, %al
  callq printf
  popq %rbp
  movl $0, %eax
  ret

.string:
  .asciz "hello %s \n"
  .size .string, 10

.value:
  .asciz "world"
  .size .value, 5
/* write function call */
main:
  pushq %rbp
  movl $1, %edi    # first argument (stdout)
  leaq .msg, %rsi  # second argument (string)
  movl $4, %edx    # third argument (size of string)
  movb $0, %al
  callq write
  popq %rbp
  ret

.msg:
  .asciz "test"
  .size .asciz, 4
/* simple loop in inline asm with c */
int main(void) {
  /* reset %rbx to 0 */
  asm("xor %rbx, %rbx");

  /* create label loop */
  asm("loop:");

  /* increment %rbx with 1 */
  asm("inc %rbx");

  /* compare value 3 with %rbx */
  /* if equal, set cf flag */
  asm("cmp $3,%rbx");

  /* if cf flag is not set, call exit label */
  asm("jnc exit");

  /* call loop label */
  asm("call loop");

  /* create label exit */
  asm("exit:");
  exit(0);
}

# How to find a database with all opcodes?

  • https://github.com/Maratyszcza/Opcodes/blob/master/opcodes/x86_64.xml
  • http://ref.x86asm.net/coder32.html

# registers conventions (x86)

  • AX: Accumulator register → arithmetic operation
  • BX: Base register →pointer to data
  • CX: Counter Register → loop and shift/rotate operation
  • DX: Data Register → arithmetic and I/O operations
  • SP: Stack Pointer → point to the top of the base stack
  • SI: Source Index →Stream operation
  • DI: Destination Index → Stream operation

# memory segments

  • .text: text segment
  • .data: initialized part of data segment
  • .bss: block starting symbol, unitialized part of data segment

# References and Resources

  • http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-2a-manual.html
  • http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-2b-manual.html
  • http://support.amd.com/us/Processor_TechDocs/24594_APM_v3.pdf
  • http://www.mathemainzel.info/files/x86asmref.html
  • http://ref.x86asm.net/
  • https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture
  • http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
  • http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch10s04.html
  • http://www.enchantedlearning.com/sparc/segments/segmentsassembly.shtml
  • https://software.intel.com/sites/default/files/m/d/4/1/d/8/Introduction_to_x64_Assembly.pdf
  • http://www.x86-64.org/documentation/assembly.html
  • http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html
  • https://en.wikipedia.org/wiki/MIPS_instruction_set
  • https://imgtec.com/mips/architectures/mips32/
  • https://www.lri.fr/~de/MIPS.pdf
  • https://imgtec.com/mips/architectures/mips64/
  • http://equipe.nce.ufrj.br/gabriel/arqcomp2/MIPS64_Instruction_Set_v0.95.pdf
  • https://imagination-technologies-cloudfront-assets.s3.amazonaws.com/documentation/MIPS_Architecture_MIPS64_InstructionSet_%20AFP_P_MD00087_06.05.pdf
  • http://asm.sourceforge.net/articles/linasm.html
  • http://ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

# Calling Convention

  • http://wiki.osdev.org/Calling_Conventions
  • https://en.wikipedia.org/wiki/Calling_convention
  • https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions
  • https://en.wikipedia.org/wiki/X86_calling_conventions
  • http://www.agner.org/optimize/calling_conventions.pdf
  • https://www.freebsd.org/doc/en/books/developers-handbook/x86-system-calls.html
  • http://stackoverflow.com/questions/2535989/what-are-the-calling-conventions-for-unix-linux-system-calls-on-x86-64
  • http://www.int80h.org/bsdasm/#alternate-calling-convention
  • http://wiki.osdev.org/System_V_ABI
  • https://refspecs.linuxbase.org/elf/x86-64-abi-0.99.pdf
  • https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI