Transcript Assembly 01

Assembly 01
Outline
•
•
•
•
•
•
•
1
this analogy will make sense…
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
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!!
2
Example Text File
Command-line editor vim showing
simple “Hello World” C++ program
3
Binary Files
• NOT meaningful to humans
• Example binary files:
•
•
•
•
•
4
Executables (i.e., instructions for CPU)
Compressed files (e.g., .zip)
Network I/O
Sensor data
…
Example Text File
Command-line editor vim showing
compiled “Hello World” executable
5
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
6
Text File Contents
Bless Hex Editor
equivalent hexadecimal
representing the binary
stored in memory
text
• Texts are stored in memory as binary, but displayed as humanreadable ASCII characters
7
Outline
•
•
•
•
•
•
•
8
this analogy will make sense…
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
Compiler vs. Assembler
high-level language
compile
assembly language
compile
assemble
machine language
(object code)
9
Compiler
high-level language
compile
assembly language
compile
assemble
machine language
(object code)
10
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
11
Assembler
high-level language
compile
assembly language
compile
assemble
machine language
(object code)
12
Mnemonic
• Example use of mov mnemonic:
mov eax,4 ; place 4 in general 32-bit register eax
13
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)
14
Outline
•
•
•
•
•
•
•
15
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
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:
•
•
•
•
16
mov
add
push
…
Mnemonic
• Example line of assembly:
mov eax,4 ; place 4 in general 32-bit register eax
17
Mnemonic
• Example line of assembly:
mov eax,4 ; place 4 in general 32-bit register eax
mnemonic
18
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
19
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!!
20
Mnemonic
• Example line of assembly:
mov eax,4 ; place 4 in general 32-bit register eax
instruction: mnemonic and operand(s)
21
Mnemonic
• Assembler converts instruction into object code
mov ebp,esp
; save stack pointer to ebp register
assembly
language
instruction
gets assembled into…
0x8BEC
22
machine
language
instruction
Mnemonic
• Machine language instruction gets decoded…
• Execution cycle begins…
0x8BEC = 1000 1011 1110 1100
23
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
24
BREAK TIME!!
• Please stand up, stretch your legs, walk around…
25
Outline
•
•
•
•
•
•
•
26
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
Assembly Process
• How to go from assembly source code to executable
• Two steps:
1. Assemble
2. Link
3. (Execute)
27
Assembly Process
.o
.asm
.o
.asm
executable
program
file
.o
.asm
assembly
source
code
file(s)
executable
assembler
object
file(s)
linker
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
29
.o
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
30
executable
Assembly Process
2) Execute
• Run the assembly code
• Run the machine language instructions…
• Do cool stuff…
31
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!!
32
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
33
Assembly Process
• Step 1: Assemble the source file eatsyscall.asm
UNIX> nasm –f elf –g –F stabs eatsyscall.asm
invoke the
nasm
assembler
34
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
35
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
36
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
37
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
38
Assembly Process
• Step 2: Link the object file(s) to create executable
UNIX> ld –o eatsyscall eatsyscall.o
39
Assembly Process
• Step 2: Link the object file(s) to create executable
UNIX> ld –o eatsyscall eatsyscall.o
invoke the linker
40
Assembly Process
• Step 2: Link the object file(s) to create executable
UNIX> ld –o eatsyscall eatsyscall.o
-o
command line option:
specifies name of
executable
(e.g., eatsyscall)
41
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
42
Assembly Process
• Step 3: Execute the program
UNIX> ./eatsyscall
Eat at Joe’s!
./
(dot slash) indicates
current directory
43
Assembly Process
• Step 3: Execute the program
UNIX> ./eatsyscall
Eat at Joe’s!
eatsyscall
executable program name
44
Assembly Process
• Step 3: Execute the program
UNIX> ./eatsyscall
Eat at Joe’s!
output
45
Outline
•
•
•
•
•
•
•
46
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
Development Process
• General idea for developing assembly code
1.
2.
3.
4.
5.
6.
47
Edit
Assemble
Link
Execute
Debug
Repeat..
.o
.o
Assembler
no errors
.asm
Assembler
errors
.o
previously
assemble
d object
files
Linker
Linker errors
no errors
editor
Debugger
start here
48
doesn’t
work
executable
works perfectly!! you’re done!!
Outline
•
•
•
•
•
•
•
49
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
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
50
Debugging
screensho
t of KDbg
51
Debugging
• To begin debugging
UNIX> kdbg eatsyscall
kdbg
starts KDbg debugger GUI
52
Debugging
• To begin debugging
UNIX> kdbg eatsyscall
name of executable program
e.g., eatsyscall
53
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
54
Outline
•
•
•
•
•
•
•
55
Binary vs. Text Files
Compiler vs. Assembler
Mnemonic
Assembly Process
Development Process
Debugging
Example
Example
• VMWare virtual machine
• running Linux operating system (lubuntu)
• Assemble, link, execute eatsyscall.asm
• Use KDbg debugger to analyze registers..
56