This document provides information about x86 architecture including registers, flags, modes, common instructions, Intel and AT&T syntax, system calls, examples, and references. It defines the purpose of key registers like EAX, EBX, ESP and flags. It explains real and protect modes and differences between Intel and AT&T syntax. Examples demonstrate how to write assembly code and call system calls. References provided can be used to learn more about x86 assembly programming.
8. Modes
? Two Modes, Real Mode and Protect Mode
? Real Mode use two 16 bit registor to represent 20bit address space
? segment:offset => segment << 4 + offset
? Can use up 1MB memory ( 1MB = 220 )
? Protect Mode
? segment:offset => Segment Descriptor + offset
15. Common Instructions
add, sub, mul, div - Arithmetic
inc ,dec - Increment, Decrement
Syntax
? add dest, source
? inc <reg> or <mem>
Example
? add eax, 10
? inc eax
16. Common Instructions
jmp C Jump
? je <label> (jump when equal)
? jne <label> (jump when not equal)
? jz <label> (jump when last result was zero)
? jg <label> (jump when greater than)
? jge <label> (jump when greater than or equal to)
? jl <label> (jump when less than)
? jle <label> (jump when less than or equal to)
17. Common Instructions
cmp C Compare
Example
? cmp DWORD PTR [eax], 10
? je loop
? cmp eax, ebx
? jle done
? jmp DWORD PTR [eax]
18. Intel and AT&T Syntax
? Prefixes
? Direction of Operands
? Memory Operands
? Suffixes
23. System Call
? Syscalls are the interface between user programs and the Linux kernel
? Put value on registers eax, ebx
? eax represent system call number
? ebx, ecx ´´ represent arguments
? Finally, execute int 0x80 instruction
? Return value will put on eax register
? If you want to know more about system call, type man 2 system_call
(ex:open)
? http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
26. Not enough?
Try this one:
http://secprog.cs.nctu.edu.tw/pro
blems/3
Open your terminal and type:
nc secprog.cs.nctu.edu.tw 10003
Hint : open /home/rop/flag ->
read from fd -> write to stdout
Have fun!!!
29. Answer
This code makes sure that the stack is aligned to 16 bytes. After this
operation esp will be less than or equal to what it was before this
operation, so the stack may grow, which protects anything that might
already be on the stack. This is sometimes done in main just in case the
function is called with an unaligned stack, which can cause things to be
really slow (16 byte is a cache line width on x86, I think, though 4 byte
alignment is what is really important here). If main has a unaligned
stack the rest of the program will too.
http://stackoverflow.com/questions/4228261/understanding-the-
purpose-of-some-assembly-statements
35. Answer
Sometimes , compiler will optimize the code by adding some padding
to make it align to word boundary
You have to inspect the assembly code to know the exactly stack
position
37. Example
? Intel and AT&T Syntax
http://asm.sourceforge.net/articles/linasm.html
? hello.asm
http://asm.sourceforge.net/intro/hello.html
? Stack overflow
http://stackoverflow.com/questions/4228261/understanding-the-
purpose-of-some-assembly-statements
38. Reference
? x86 Assembly Guide ( recommended )
http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
? Linux System Call Table
http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
? Wiki
https://en.wikipedia.org/wiki/X86_assembly_language
https://en.wikibooks.org/wiki/X86_Assembly/Interfacing_with_Linux