x86 Assembly Programming


Programming Model

Memory

232- bytes

Registers

8 32-bit General Purpose Registers
Register Function 16-bit low end 8-bit
eax Accumulator ax ah, al
ebx (base index) bx bh, bl
ecx (count) cx ch, cl
edx (data) dx dh, dl
edi (destination index) do
esi (source index) si
ebp Frame pointer bp
esp Stack top pointer sp
 
 
 
 
6 16-bit Section Registers
Register Function
cs Code section
ds Data section
ss Stack section
es (extra section)
fs (supplemental section)
gs (supplemental section)
 
EFLAGS Register
S Sign
Z Zero
C Carry
P Parity
O Overflow
32-bit EFLAGS Register

32-bit EIP (Instruction Pointer Register)

AT&T Style Syntax (GNU C/C++ compiler and GAS)

Operand Addressing

Subroutines

The flow of control and the interface between a subroutine and its caller is described by the following:
 
Caller ...
call target Transfer of control from caller to the subroutine by 
  1. saving the contents of the program counter and
  2. the program counter (CS:IP) register to the entry point of the subroutine.
Subroutine
pushl %ebp 
movl %esp, %ebp
Save base pointer of the caller 
New base pointer (activation record/frame) 
Callee ... Body of Subroutine
movl %ebp,%esp 
popl %ebp 
Restore the callers stack top pointer 
Restore the callers base pointer
ret Return of control from the subroutine to the caller by alter the program counter (CS:IP) register to the saved address of the caller.
Caller ...
An alternative is to have the caller save and restore the values in the registers. (Prior to the call, the caller saves the registers it needs and after the return, restores the values of the registers)

Data

Data Representation

Data Definition Directives

Description provided to the assembler of how static data is to be organized.

Constant Definitions

Data Transfer Instructions

Arithmetic Instructions

Logic Instructions

Shift and Rotate Instructions

Control Transfer Instructions

String Instructions

The sring instructions assume that by default, the address of the source string is in ds:esi (section register may be any of cs, ss, es, fs, or gs) and the address of the destination string is in es:edi (no override on the destination section).  Typical code follow the scheme
initialize esi and edi with addresses for source and destination strings
initialize ecx with count
Set the direction flag with cld to count up, with std to cound down
prefix string-operation

Miscellaneous Instructions

Floating Point Instructions

Floating Point  
8 32-bit registers
Register Function
st
st(0)
st(1)
...
st(7)
 

MMX Instructions

System Instructions

Interrupts

Memory Management Unit

Cache

References