Sei sulla pagina 1di 29

x86 Assembly Language

too much fun for just one day

prepared by
jonathan lung
http://www.cs.toronto.edu/~lungj

Winter 2006
Scope of Discussion
• 16-bit x86 programming
• A little bit of context
• The low down
• A short example
• Questions & Answers

02
Assembly Language
• Early programming language
• Low level
• Assembled by assemblers such as
– Flat assembler (FASM)
– Microsoft Macro Assembler (MASM)
– Netwide Assembler (NASM)
– Borland Turbo Assembler (TASM)
• In-line assembly language support 03
The Scoop
• This lecture is not about
• Computer hardware
• Cracking
• Writing mal-ware
• The merits of assembly language
• Writing optimized assembly code
• This lecture is about
• Understanding system tools
• Demystifying language functions 04
The Fundamental Fact
• A program is nothing more than a
sequence of instructions telling a
computer how to move bits around

05
Opcodes
• One-to-one correspondence
• Written as mnemonics
• Take the form
MNEMONIC target, source
E.g. ADD AX, BX

06
Targets and Sources
• Immediate
• Register
• Memory
• Stack

07
ta rg ets and so urc es
Immediate
immediate registers memory stack

• Constant value
• Can act as source

08
ta rg ets and so urc es
Registers
immediate registers memory stack

• Four general purpose registers


– AX
– BX
– CX
– DX
• 16 bits long
• Sub-dividable into halves
09
ta rg ets and so urc es
Registers
immediate registers memory stack

• Four segment registers


– CS
– DS
– ES
– SS

0A
ta rg ets and so urc es
Memory
immediate registers memory stack

• Memory address written as


SEGMENT:OFFSET
• Dereference offset with square brackets
CS:[C494]
• DS is implicit when not specified
[1337] is the same as DS:[1337]
0B
ta rg ets and so urc es
Stack
immediate registers memory stack

• First in, last out (FILO)


• Top of the stack is at SS:SP
• Grows downwards
• No bounds checking

0C
Operations
• Arithmetic
• Logic
• Bit manipulation
• Comparisons and jumps
• Function calls
• Other
0D
ope ra ti ons
Arithmetic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• ADD
• SUB
• MUL
• DIV

0E
ope ra ti ons
Arithmetic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• ADD
• SUB
• MUL
• DIV
ADD AX, 5 AX = 0003

0E
ope ra ti ons
Arithmetic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• ADD
• SUB
• MUL
• DIV
ADD AX, 5 AX = 0008

0E
ope ra ti ons
Logic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• AND
• OR
• XOR
• NOT

0F
ope ra ti ons
Logic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• AND
• OR
• XOR
• NOT
AND CH, DL CH = 11111111 DL = 00000010
NOT DL

0F
ope ra ti ons
Logic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• AND
• OR
• XOR
• NOT
AND CH, DL CH = 00000010 DL = 00000010
NOT DL

0F
ope ra ti ons
Logic
arithmetic logic bit comparisons function calls other
manipulations and jumps

• AND
• OR
• XOR
• NOT
AND CH, DL CH = 00000010 DL = 11111101
NOT DL

0F
ope ra ti ons
Bit Manipulation
arithmetic logic bit comparisons function calls other
manipulations and jumps

• SHL/SHR
– E.G. SHL AL, 1
101101010
01101010 ;(SHL by 1)

10
ope ra ti ons
Comparisons and Jumps
arithmetic logic bit comparisons function calls other
manipulations and jumps

• JMP
• CMP
• Jxx

11
ope ra ti ons
Function Calls
arithmetic logic bit comparisons function calls other
manipulations and jumps

• CALL
• RET

12
ope ra ti ons
Other
arithmetic logic bit comparisons function calls other
manipulations and jumps

• MOV
– E.g. MOV AX, BX AX BX

13
ope ra ti ons
Other
arithmetic logic bit comparisons function calls other
manipulations and jumps

• MOV
– E.g. MOV AX, BX DS:BX-1 C470
MOV AX, [BX] AX DS:BX EA75
DS:BX+1 DEAD
DS:BX+2 BEEF

13
ope ra ti ons
Other
arithmetic logic bit comparisons function calls other
manipulations and jumps

• MOV
– E.g. MOV AX, BX
MOV AX, [BX]

• PUSH/POP
– E.g. PUSH BX
POP AX
• IN/OUT
• NOP
13
Snakes And Ladders

14
Snakes And Ladders
MOV BX, 0 ;current location
MOV CX, 0 ;# moves so far
NEXT_FLIP: CALL GETNEXTCOINFLIP
ADD BX, AX ;# spaces to move
ADD CX, 1
ADD BX, DS:[BX]
CMP BX, 64 ;64h=100 base 10
JL NEXT_FLIP
HANG: JMP HANG

14
Questions & Answers
• For more information…
– IA-32 Intel Architecture Software
Developer's Manual
– The Peter Norton Programmer’s Guide to
the IBM PC
– Inside the IBM PC

15

Potrebbero piacerti anche