What is Assembly?

It is the language written for all x86 CPU’s. The syntax of assembly language depends upon the assembler used. For example: NASM or GAS. Also, x86 assembly is fundamentally different from other architectures like ARM or MIPS, which have distinct instruction sets and cannot run x86 code natively. The two major syntax are as follows

  • AT&T syntax
  • Intel syntax

Major syntax difference

In the AT&T syntax, which has a different notation than the Intel syntax, the % sign is prefixed to the register names. Another difference is that the direction of operation in MOV instruction is from left to right.


Addressing Modes

Addressing modes are the techniques used by a CPU to identify where the operands for an instruction are stored, determining how the effective address of data is calculated.

Register Addressing

In this addressing, registers are used as operands.

MOV EAX, EBX
Immediate Addressing

A fixed value is used as the operand in this addressing

MOV EAX, 0x0
Memory Addressing

The memory address is used as the operand in this addressing

MOV EAX, DWORD PTR ESP

CPU Instructions

Arithmetic Instructions:

ADD

It perform the addition.

ADD ESP,0x08
SUB

It performs subtraction

SUB ESP,0x08
DIV

div operation divides (unsigned) the value in the AX, DX:AX, or EDX:EAX registers (dividend) by the source operand (divisor) and stores the result in the AX (AH:AL), DX:AX, or EDX:EAX registers.

To divide the value in AL by a register BL:

MOV AH, 0      ; Clear AH to set 16-bit dividend in AX
MOV BL, 7      ; Load divisor
DIV BL         ; AL = AX / BL (Quotient), AH = AX % BL (Remainder)

After that AL will contain quotient and AH will contain remainder.

MUL

It performs multiplication.

mov ax, 5 ; Multiplicand in AX 
mov cx, 10 ; Multiplier in CX 
mul cx ; DX:AX = AX * CX (Result: DX=0, AX=50)
INC

It increments the value of the operand by 1.

INC EBC
DEC

It decrements the value of the operand by 1.

DEC EBX

Branch Instructions:

Branch instructions are those that performs the comparison and/or branching. They are vital with respect to follow the program flow.

JMP

It allows branching unconditionally. It takes the memory address as an operand and will jump to that address.

JMP 0x56789020
JZ/JE

These are among the conditional branching instructions. The condition for branching to the memory address in the operand is that the zero flag is set to “1” in the form of ZF=1.

  • JE = Jump if equal
  • JZ = Jump if zero
JE 0x56789020 <main+288>
JNZ/JNE

These are among the conditional branching instructions. The condition for branching to the memory address in the operand is that the zero flag is set to “0” in the form of ZF=0.

  • JNE = Jump if not equal
  • JNZ = Jump if not zero
JE 0x56789020 <main+288>
CALL

It is used for function call. It takes a function address as an operand. Two basic operations with this instruction

  • The address of the instruction after the CALL instruction in the program flow is pushed to the stack (Return address)
  • The value of the EIP register is set as the function address, so that the program flow branches to the corresponding function.
CALL 0x7898792
CMP

It is used for comparison operations. Takes 2 values to compare as operands.

CMP EDX, EAX

Depending on the result of the comparison, Zero Flag(ZF)andCarry Flag(CF) may be changed. The below table shows the conditions that will cause these changes.

cmp dst,srcZFCF
dst=src10
dst < src01
dst > src00

Data Transfer Instructions

MOV

It is the most basic data transfer instruction used to assign a value to a register or to an address in the memory.

MOV EAX, 0x0 ; EAX register is set to the value zero
LEA

LEA instruction is called the “Load Effective Address”. It is used to assign a memory address to the target.

LEA ECX, esp+0x4
XCHG

It allows us to exchange values in 2 registers. For example:

XCHG EAX, EBX
PUSH

It adds data to the stack

PUSH EDX
POP

It extracts data from the stack (the topmost value).

POP EDX

Logical Instructions

These are similar to the bitwise operation section. The output will be same as mentioned in the truth tables. I wrote in C - Basics

AND

It is used to implement the logical and operation.

AND ESP, 0xfffff
OR

It is used to implement the logical OR operation.

OR EAX, 0xfffffff0
XOR

It is used to implement the logical XOR operation.

XOR EAX, 0xfffffff0
NOT

It enables to implement the bitwise inversion operation.

NOT EAX

NOP Instruction

It means no operation that allows us to move on to the next instruction without any operation being executed. It is used alone without the operand.


Referenced From