29
x86 Assembly Language prepared by jonathan lung http://www.cs.toronto.edu/~lungj Winter 2006 too much fun for just one day

x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

  • Upload
    lythuy

  • View
    246

  • Download
    1

Embed Size (px)

Citation preview

Page 1: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

x86 Assembly Language

prepared byjonathan lung

http://www.cs.toronto.edu/~lungj

Winter 2006

too much fun for just one day

Page 2: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Scope of Discussion

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

02

Page 3: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

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

Page 4: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

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

Page 5: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

The Fundamental Fact

• A program is nothing more than asequence of instructions telling acomputer how to move bits around

05

Page 6: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Opcodes

• One-to-one correspondence• Written as mnemonics• Take the form

MNEMONIC target, sourceE.g. ADD AX, BX

06

Page 7: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Targets and Sources

• Immediate• Register• Memory• Stack

07

Page 8: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

targets and sourc es

Immediate

• Constant value• Can act as source

08

stackmemoryregistersimmediate

Page 9: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

targets and sourc es

Registers

• Four general purpose registers– AX– BX– CX– DX

• 16 bits long• Sub-dividable into halves

09

stackmemoryregistersimmediate

Page 10: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

targets and sourc es

Registers

• Four segment registers– CS– DS– ES– SS

0A

stackmemoryregistersimmediate

Page 11: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

targets and sourc es

Memory

• Memory address written asSEGMENT:OFFSET

• Dereference offset with square bracketsCS:[C494]

• DS is implicit when not specified[1337] is the same as DS:[1337]

0B

stackmemoryregistersimmediate

Page 12: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

targets and sourc es

Stack

• First in, last out (FILO)• Top of the stack is at SS:SP• Grows downwards• No bounds checking

0C

stackmemoryregistersimmediate

Page 13: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Operations

• Arithmetic• Logic• Bit manipulation• Comparisons and jumps• Function calls• Other

0D

Page 14: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Arithmetic

• ADD• SUB• MUL• DIV

0E

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

Page 15: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Arithmetic

• ADD• SUB• MUL• DIV

0E

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

ADD AX, 5 AX = 0003

Page 16: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Arithmetic

• ADD• SUB• MUL• DIV

0E

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

ADD AX, 5 AX = 0008

Page 17: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Logic

• AND• OR• XOR• NOT

0F

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

Page 18: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Logic

• AND• OR• XOR• NOT

0F

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

AND CH, DL CH = 11111111 DL = 00000010

NOT DL

Page 19: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Logic

• AND• OR• XOR• NOT

0F

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

AND CH, DL CH = 00000010 DL = 00000010

NOT DL

Page 20: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Logic

• AND• OR• XOR• NOT

0F

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

AND CH, DL CH = 00000010 DL = 11111101

NOT DL

Page 21: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Bit Manipulation

• SHL/SHR– E.G. SHL AL, 1101101010

01101010 ;(SHL by 1)

10

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

Page 22: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Comparisons and Jumps

• JMP• CMP• Jxx

11

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

Page 23: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Function Calls

• CALL• RET

12

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

Page 24: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Other

• MOV– E.g. MOV AX, BX

13

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

AX BX

Page 25: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Other

• MOV– E.g. MOV AX, BX

MOV AX, [BX]

13

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

AX DS:BX EA75DS:BX+1 DEAD

DS:BX-1 C470

DS:BX+2 BEEF

Page 26: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Other

• MOV– E.g. MOV AX, BX

MOV AX, [BX]

• PUSH/POP– E.g. PUSH BX

POP AX• IN/OUT• NOP

13

otherfunction callscomparisonsand jumps

bitmanipulations

logicarithmetic

operati ons

Page 27: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Snakes And Ladders

14

Page 28: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

Snakes And LaddersMOV BX, 0 ;current locationMOV CX, 0 ;# moves so far

NEXT_FLIP: CALL GETNEXTCOINFLIPADD BX, AX ;# spaces to moveADD CX, 1ADD BX, DS:[BX]CMP BX, 64 ;64h=100 base 10JL NEXT_FLIP

HANG: JMP HANG

14

Page 29: x86 Assembly Language - University Of Torontolungj/presentations/x86Asm.pdf · x86 Assembly Language prepared by jonathan lung lungj Winter 2006 too much fun for just one day

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