Assembly

Assembly

  • 32 bits = 4 bytes = 1 DWORD

Registers #

General Purpose #

Name32-bit16-bit8-bitDescription
Accumulator RegisterEAXAXAH/ALCommonly for the results of addition and multiplication instructions.
Base RegisterEBXBXBH/BL
Counter RegisterECXCXCH/CLCommonly used for counting (e.g. loops).
Data RegisterEDXDXDH/DL
Base PointerEBPBP-For referencing arguments and local variables.
Stack PointerESPSP-Points to the last item on the stack.
Source PointerESISI-
Destination IndexEDIDI-

Special Registers #

RegisterNameDescription
EIPExtended Instruction PointerPoints to the next instruction to be executed
EFLAGSRepresent the outcome of computations and control CPU
CS, DS, ES, FS, GS, SSSegment registersDescribe segments of memory

GDB Reference #

  • list main - show high level source code
  • disas main - disassemble, showing assembly code
  • x/s 0x4005a4 - show content of given memory address
  • break *0x40052f - add breakpoint at given address
  • run - run the program
  • x $rbp -4 - print the RBP register

Instructions #

Intel-style assembly has destination first (<mnemonic> <dest>,<src>), AT&T-style assembly has source first (<mnemonic> <src>,<dest>). <loc> represents a memory location to jump to if the condition is met.

Some of the instructions below have multiple variants, the assembler tries to work out the right instruction to use (e.g. mov may become mvol for moving a long value or movb for moving a byte value).

InstructionDescription
Moving
mov eax,ebxCopy contents of EBX to EAX
mov eax,0x42Copy value 0x42 into EAX
mov eax,[0x4037c4]Copy 4 bytes at the given location in memory to the EAX
mov eax,[ebx]Copy 4 bytes in the EBX register into the EAX (pointer)
mov eax,[ebx+esi*4]Copy 4 bytes at memory location specified by equation into the EAX
Arithmetic
sub eax,0x10Subtract 10 from the EAX
add eax,ebxAdd EBX to the EAX and store result in EAX
inc edxIncrement EDX by 1
dec edxDecrement EDX by 1
mul 0x50Multiply EAX by 0x50 and store result in EDX:EAX
div 0x75Divides EDX:EAX by 0x75 and store result in EAX and remainder in EDX
Bitwise
xor eax,eaxClear EAX register (XOR EAX with EAX, store result in EAX)
or eax,0x7575Perform logical OR on EAX and 0x7575
mov eax 0xA
shl eax,2
Shift EAX register left by 2 bits
mov bl,0xA
ror bl,2
Rotate BL register right by 2 bits
Comparisons
cmp <dst>,<src>Set zero flag (ZF) and carry flag (CF):
  • dst=src? ZF=1, CF=0
  • dst<src? ZF=0, CF=1
  • dst>src? ZF=0, CF=0
test <dst>,<src>Returns 1 if matching bits from operands are 1, else 0
jz <loc>Jump if ZF=1 (equal)
jnz <loc>Jump if ZF=0 (not equal)
je <loc>Jump if ZF=1 (equal, equivalent to jz)
jne <loc>Jump if ZF=0 (not equal, equivalent to jnz)
jg <loc>dest > src (signed)
jge <loc>dest >= src (signed)
ja <loc>dest > src (unsigned)
jae <loc>dest >= src (unsigned)
jl <loc>dest < src (signed)
jle <loc>dest <= src (signed)
jb <loc>dest < src (unsigned)
jbe <loc>dest <= src (unsigned)
jo <loc>Jump if OF=1 (overflow)
js <loc>Jump if SF=1 (sign flag)
jecxz <loc>Jump if ECX=0