x86 Assembly Language: Debugging Survival Guide (x86-DSG) Divyanand Kutagulla
x86 Assembly Language: Debugging Survival Guide (x86-DSG) Divyanand Kutagulla
Language
Debugging Survival Guide
(x86-DSG)
Divyanand Kutagulla
Objective
Understand common IA32 assembly
language constructs used by VS.NET while
it generates code targeting x86 CPUs.
Topic Scope
x86 architecture registers
IA32 instruction set
Used in both Intel and AMD CPUs
Instruction types covered
Basic Integer type instructions
No Floating point, MMX/3DNow! instructions
EBX General
Overflow OV OF
Direction UP DF Indicates the direction of string
processing. 1 means highest address to
lowest. 0 means lowest address to highest
address
Interrupt EI IF Set to 1 if interrupts are enabled. This is
Enable always set to 1 by a user mode debugger
Sign PL SF
Zero ZR ZF
Auxiliary Carry AC AF Indicates a carry/borrow in BCD arithmetic
Parity PE PF
Carry CY CF
IA32 Instruction Format
General format:
[prefix] instruction operands
Prefix used only in String Functions
Operands represent the direction of operands
Single
operand instruction: XXX src
Two operand instruction :XXX dest src
XXX represents the instruction opcode
src & dest represent the source and destination operands
respectively
IA32 Instruction Format (cont’d)
Source operands can be:
Register/Memory reference/Immediate value
Destination operands can be:
Register/Memory reference
Note:
The Intel CPU does NOT allow both source
and destination operands to be memory
references
Memory References
Same as a C/C++ pointer access
Pointer operands appear within square
brackets e.g. Contents of
memory location
MOV EAX, [0x00040222h] 0x00040222
Can also have register names instead of hex
addresses e.g. MOV EAX, [EBX]
Refers to the contents
of register EBX
Memory References (cont’d)
Control the size of memory accessed by
preceding the memory reference with a
size:
BYTE PTR: byte access
WORD PTR two byte access
DWORD PTR four byte access
E.g. MOV EAX, BYTE PTR [0x00001234]
Memory References (cont’d)
Invalid Memory Accesses
Accessing illegal memory: CPU generates a
general protection fault: (GPF)
Access a memory location that does not exist:
CPU generates a page fault
The x86 Stack
Starts from high memory and “grows”
towards low memory
Used by the CPU to:
Store return addresses
Pass parameters to functions
Store local variables
ESP indicates current top of stack
VS.NET Inline Assembler
Can embed x86 assembly language in C/C++ source
E.g.
void foo (void)
{
__asm
{
//x86 assembly
}
POP EAX
POP EBX
}
}
int g_iVal = 0 ;
void AccessGlobalMemory ( void )
{
__asm
{
// Set the global variable to 48,059.
MOV g_iVal , 0BBBBh
Caller code pushes iParam onto the stack before calling the function code above:
// AccessParameter(0x42);
push 66 ; 00000042H
call ?AccessParameter@@YAXH@Z ; AccessParameter
Local Variable Access
Local Variables occur as negative offsets from the EBP
(stack frame pointer) register
e.g.
void AccessLocalVariable ( void ) {
int iLocal ;
__asm {
// Set the local variable to 23.
MOV iLocal , 017h