githubEdit

amd64

also called x86-64

registers

  • 16 general purpose registers + instruction pointer.

    • x86-32 - 32bit/4byte wide

    • x86-64 - 64bit/8byte wide

naming convention

rflags

eflags extended to 64 bits, but the extended space is not used.

  • status flags

    • zero flag (6) - set if the result of some instruction is zero

    • sign flag (7) - set equal to the most significant bit of the result which is the sign. 1=neg 0=pos

c data type size

usage convention (intel)

intel's suggestion of usage of registers:

  • rax - return value of functions (a=accumulator)

  • rbx - base pointer to data section (b=base)

  • rcx - can be used for loops (c=counter)

  • rdx - i/o pointer (d=data)

  • rsi - points to string for string operation (si=string index)

  • rdi - points to string for string operation (di=destination index)

  • rsp - stack pointer: points to top of the stack

  • rbp - base pointer: maintains base of a function's stack frame

  • rip - instruction pointer - points to next instruction

reading manual

  • r/mX

  • immX

syntax

instructions

nop

push

pop

call

ret

mov

add & sub

multiplication

division

movzx

movsx

movsxd

lea

jmp

cmp

jcc

boolean instructions

inc/dec

test

shift instructions

rep instructions

register convention

caller save register

  • register belongs to the callee

  • caller must save the register, assuming it will be changed by the callee.

callee save register

  • register belongs to the caller.

  • hence, callee must save those register and restore them before returning.

calling conventions

  • compilers use a subset of caller save registers for passing arguments in and out of the function.

  • rax is used to return values from function given the value is 64 bytes or less.

ms x64 abi

  • in most cases, ms abi does not use frame pointer. no ebp register to maintain base of stack frame.

  • if space is dynamically allocated using _alloca(), then frame pointers is used to mark base of the stack frame.

  • ms document doesn't say that it has to be ebp

ms shadow store

  • ms uses 4 register fall call calling convention by default.

  • the caller must allocate space to accommodate 4 function parameter. this is called shadow store.

  • parameter beyond the first 4, should be pushed on the stack before calling the function. the first four is then passed through 4 registers. the callee, can use the space on the shadow store to save the first four register.

  • this is the reason by why a simple function with no variable, by default reserves a space for 0x28 bytes (16 byte padding for return alignment and 4 8 byte chunk of shadow store)

  • eg for 6 parameters passed

    • caller allocated shadow store for 4 parameters

    • caller places the last two parameters on the stack

    • callee passes first 4 parameter over registers

    • callee moves value from these 4 register to shadow store

system V x86-64 abi

  • frame pointers are used to mark the base of the stack frame using rbp register. can be disabled.

32-bit calling convention

  • cdecl

    • default in most c code

    • caller cleans up the stack

  • stdcall

    • used in wind32 apis

    • callee cleans up the stack

  • function parameters are pushed on the stack from right to left.

  • both these calling convention uses something called stack frame pointer.

  • this uses ebp register to maintain a stack base pointer

writing assembly

inline assembly

gcc

inline assembly is supported in gcc using the gas syntax

visual studio

inline assembly was supported in x32 but not anymore in x64. but a some instructin can be used in c code using visual studio instrinsics.

standalone assembly

GCC

AS

  • GNU assembler, integrates with gcc, ld etc.

  • uses at&t syntax by default

  • can be compiled with as and linked with ld

all of this can be simply done directly by gcc

MASM

microsoft assembler, used with visual studio

  • NASM - netwide assembler - cross platform

Last updated