assembly 01. outline binary vs. text files compiler vs. assembler mnemonic assembly process...

57
Assembly 01

Upload: isaac-sartell

Post on 15-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

Assembly 01

Page 2: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

2

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

this analogy will make sense…

Page 3: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

3

Text Files

• Meaningful to humans when displayed

• Contains 95 visible characters and white space• White space includes spaces, tabs, and newlines

• You compile or assemble text files into binary files• Old school computer scientists wrote binary instructions (yuck!)• Thank your compiler and/or assembler!!

Page 4: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

4

Example Text File

Command-line editor vim showing simple “Hello World” C++ program

Page 5: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

5

Binary Files

• NOT meaningful to humans

• Example binary files:• Executables (i.e., instructions for CPU)• Compressed files (e.g., .zip)• Network I/O• Sensor data• …

Page 6: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

6

Example Text File

Command-line editor vim showing compiled “Hello World” executable

Page 7: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

7

Text Files

• Text files are stored as binary in computer’s memory• How else would contents be stored?!?!

• Text files are ASCII characters• 95 meaningful characters and white space• ASCII character is a byte• E.g., ‘A’ is 0x41, decimal 65, binary sequence 0100 0001

Page 8: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

8

Text File Contents

Bless Hex Editor

textequivalent hexadecimal

representing the binary stored in

memory

• Texts are stored in memory as binary, but displayed as human-readable ASCII characters

Page 9: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

9

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

this analogy will make sense…

Page 10: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

10

Compiler vs. Assembler

high-level language

assembly language

compile

compile

assemble

machine language (object code)

Page 11: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

11

Compiler

high-level language

assembly language

compile

compile

assemble

machine language (object code)

Page 12: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

12

Compiler

• Translates high-level language into object code • Assembly code may be intermediate step

• Programmer DOES NOT have full control of object code• Compiler decides what instructions go into machine code• Compiler decides the order of instructions in machine code• E.g., code snippet “ x = 4; “ could be compiled into 4 or 5 instructions

Page 13: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

13

Assembler

high-level language

assembly language

machine language (object code)

compile

compile

assemble

Page 14: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

14

Mnemonic

• Example use of mov mnemonic:

mov eax,4 ; place 4 in general 32-bit register eax

Page 15: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

15

Assembler

• Translates assembly language into machine language

• Programmer has FULL CONTROL of object code• Must define every instruction to be executed

• “Long journey in very small steps”• Each “step” is instruction for CPU• (many lines of code)

Page 16: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

16

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

Page 17: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

17

Mnemonic

• Assembler sees at least one line of assembly source code for every machine instruction it generates

• Assembly language has a mnemonic for each machine instruction available for that architecture

• Example mnemonics for x86 architecture:• mov• add• push• …

Page 18: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

18

Mnemonic

• Example line of assembly:

mov eax,4 ; place 4 in general 32-bit register eax

Page 19: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

19

Mnemonic

• Example line of assembly:

mov eax,4 ; place 4 in general 32-bit register eax

mnemonic

Page 20: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

20

Mnemonic

• Example line of assembly:

mov eax,4 ; place 4 in general 32-bit register eax

operands

note: • some instructions have zero operands• other instructions have 1 operand• other instructions have 2 operands

Page 21: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

21

Mnemonic

• Example line of assembly:

mov eax,4 ; place 4 in general 32-bit register eax

comment: starts at ; ends at EOL

best practice: comment EVERY line of assembly code!!

Page 22: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

22

Mnemonic

• Example line of assembly:

mov eax,4 ; place 4 in general 32-bit register eax

instruction: mnemonic and operand(s)

Page 23: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

23

Mnemonic

• Assembler converts instruction into object code

mov ebp,esp ; save stack pointer to ebp register

0x8BEC

assembly language instructio

ngets assembled into…

machine language

instruction

Page 24: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

24

Mnemonic

• Machine language instruction gets decoded…• Execution cycle begins…

0x8BEC = 1000 1011 1110 1100

Page 25: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

25

Mnemonic

• You will become familiar with x86 mnemonics• Practice, practice, practice writing x86 assembly code

• Same idea for MIPS and ARM assembly.. • Slightly different mnemonics and operands

• Flip through Appendix A in the book…• Taste of x86 mnemonics • Don’t worry about details, yet

Page 26: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

26

BREAK TIME!!

• Please stand up, stretch your legs, walk around…

Page 27: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

27

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

Page 28: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

28

Assembly Process

• How to go from assembly source code to executable

• Two steps:

1. Assemble2. Link3. (Execute)

Page 29: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

Assembly Process

.asm

.asm

.asm

assembler

.o

.o

.o

linker

executable

assembly source code

file(s)

object file(s)

executable

program file

Page 30: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

30

Assembly Process

1) Assembler translates assembly source code into object file • Assembly source code file(s) end in .asm• Object file(s) end in .o

• Object file(s) cannot be executed by CPU• Modern operating systems prevent object file execution

.asm .o

Page 31: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

31

Assembly Process

2) Linker (or loader) creates executable program file• Linker “links” object file(s) into executable• Linker creates image of how executable will be stored in memory

.o executable

Page 32: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

32

Assembly Process

2) Execute• Run the assembly code• Run the machine language instructions…• Do cool stuff…

Page 33: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

33

Assembly Process

• Example: Assemble, load, and execute “eatsyscall.asm”

• Note: “UNIX>” will indicate the command prompt

• This example is available to download from book’s website:• http://www.copperwood.com/pub/• “asmsbs3e.zip” contains all examples in book!!

Page 34: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

34

Assembly Process

• Step 1: Assemble the source file eatsyscall.asm• We’ll discuss what goes into assembly source files in the coming weeks

UNIX> nasm –f elf –g –F stabs eatsyscall.asm

Page 35: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

35

Assembly Process

• Step 1: Assemble the source file eatsyscall.asm

UNIX> nasm –f elf –g –F stabs eatsyscall.asm

invoke the nasm

assembler

Page 36: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

36

Assembly Process

• Step 1: Assemble the source file eatsyscall.asm

UNIX> nasm –f elf –g –F stabs eatsyscall.asm

-f elf command line option: .o files (produced by

nasm) will be elf format

Page 37: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

37

Assembly Process

• Step 1: Assemble the source file eatsyscall.asm

UNIX> nasm –f elf –g –F stabs eatsyscall.asm

-g command line option: include

debug information in .o file

Page 38: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

38

Assembly Process

• Step 1: Assemble the source file eatsyscall.asm

UNIX> nasm –f elf –g –F stabs eatsyscall.asm

-F stabs command line option: debug information in “stabs” format

Page 39: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

39

Assembly Process

• Step 1: Assemble the source file eatsyscall.asm

UNIX> nasm –f elf –g –F stabs eatsyscall.asm

filename of assembly source

code to be assembled

Page 40: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

40

Assembly Process

• Step 2: Link the object file(s) to create executable

UNIX> ld –o eatsyscall eatsyscall.o

Page 41: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

41

Assembly Process

• Step 2: Link the object file(s) to create executable

UNIX> ld –o eatsyscall eatsyscall.o

invoke the linker

Page 42: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

42

Assembly Process

• Step 2: Link the object file(s) to create executable

UNIX> ld –o eatsyscall eatsyscall.o

-ocommand line

option:specifies name of

executable (e.g., eatsyscall)

Page 43: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

43

Assembly Process

• Step 2: Link the object file(s) to create executable

UNIX> ld –o eatsyscall eatsyscall.o

name of object file(s) to be linked

together

Page 44: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

44

Assembly Process

• Step 3: Execute the program

UNIX> ./eatsyscallEat at Joe’s!

./ (dot slash) indicates

current directory

Page 45: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

45

Assembly Process

• Step 3: Execute the program

UNIX> ./eatsyscallEat at Joe’s!

eatsyscallexecutable program

name

Page 46: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

46

Assembly Process

• Step 3: Execute the program

UNIX> ./eatsyscallEat at Joe’s!

output

Page 47: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

47

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

Page 48: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

48

Development Process

• General idea for developing assembly code

1. Edit2. Assemble3. Link4. Execute5. Debug6. Repeat..

Page 49: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

49

.asm.o

executable

Assembler

Linker

Debugger

.o .o

start here

no errors

Assembler errors

no errors

Linker errors

works perfectly!! you’re done!!

doesn’t work

previously

assembled object

files

editor

Page 50: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

50

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

Page 51: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

51

Debugging

• We are going to use KDbg• Easy to use• View register contents, output, etc.• GUI front-end to gdb

• Book uses insight (Chapter 6+)• It would not install on VMs!!

• Other options out there• e.g., ddd

Page 52: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

52

Debugging

screenshot of KDbg

Page 53: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

53

Debugging

• To begin debugging

UNIX> kdbg eatsyscall

kdbg starts KDbg debugger

GUI

Page 54: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

54

Debugging

• To begin debugging

UNIX> kdbg eatsyscall

name of executable program

e.g., eatsyscall

Page 55: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

55

Debugging

• Using a debugger will save you time and frustration!!!• Use breakpoints to check flow of execution

• Register contents• Output• etc.

• KDbg is a visual debugger, easier than command line only• gdb command-line debugger is clunky and hard to learn

Page 56: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

56

Outline

• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example

Page 57: Assembly 01. Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 this analogy will

57

Example

• VMWare virtual machine• running Linux operating system (lubuntu)

• Assemble, link, execute eatsyscall.asm

• Use KDbg debugger to analyze registers..