Upload
isaac-sartell
View
219
Download
0
Embed Size (px)
Citation preview
Assembly 01
2
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
this analogy will make sense…
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!!
4
Example Text File
Command-line editor vim showing simple “Hello World” C++ program
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• …
6
Example Text File
Command-line editor vim showing compiled “Hello World” executable
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
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
9
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
this analogy will make sense…
10
Compiler vs. Assembler
high-level language
assembly language
compile
compile
assemble
machine language (object code)
11
Compiler
high-level language
assembly language
compile
compile
assemble
machine language (object code)
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
13
Assembler
high-level language
assembly language
machine language (object code)
compile
compile
assemble
14
Mnemonic
• Example use of mov mnemonic:
mov eax,4 ; place 4 in general 32-bit register eax
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)
16
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
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• …
18
Mnemonic
• Example line of assembly:
mov eax,4 ; place 4 in general 32-bit register eax
19
Mnemonic
• Example line of assembly:
mov eax,4 ; place 4 in general 32-bit register eax
mnemonic
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
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!!
22
Mnemonic
• Example line of assembly:
mov eax,4 ; place 4 in general 32-bit register eax
instruction: mnemonic and operand(s)
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
24
Mnemonic
• Machine language instruction gets decoded…• Execution cycle begins…
0x8BEC = 1000 1011 1110 1100
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
26
BREAK TIME!!
• Please stand up, stretch your legs, walk around…
27
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
28
Assembly Process
• How to go from assembly source code to executable
• Two steps:
1. Assemble2. Link3. (Execute)
Assembly Process
.asm
.asm
.asm
assembler
.o
.o
.o
linker
executable
assembly source code
file(s)
object file(s)
executable
program file
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
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
32
Assembly Process
2) Execute• Run the assembly code• Run the machine language instructions…• Do cool stuff…
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!!
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
35
Assembly Process
• Step 1: Assemble the source file eatsyscall.asm
UNIX> nasm –f elf –g –F stabs eatsyscall.asm
invoke the nasm
assembler
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
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
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
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
40
Assembly Process
• Step 2: Link the object file(s) to create executable
UNIX> ld –o eatsyscall eatsyscall.o
41
Assembly Process
• Step 2: Link the object file(s) to create executable
UNIX> ld –o eatsyscall eatsyscall.o
invoke the linker
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)
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
44
Assembly Process
• Step 3: Execute the program
UNIX> ./eatsyscallEat at Joe’s!
./ (dot slash) indicates
current directory
45
Assembly Process
• Step 3: Execute the program
UNIX> ./eatsyscallEat at Joe’s!
eatsyscallexecutable program
name
46
Assembly Process
• Step 3: Execute the program
UNIX> ./eatsyscallEat at Joe’s!
output
47
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
48
Development Process
• General idea for developing assembly code
1. Edit2. Assemble3. Link4. Execute5. Debug6. Repeat..
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
50
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
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
52
Debugging
screenshot of KDbg
53
Debugging
• To begin debugging
UNIX> kdbg eatsyscall
kdbg starts KDbg debugger
GUI
54
Debugging
• To begin debugging
UNIX> kdbg eatsyscall
name of executable program
e.g., eatsyscall
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
56
Outline
• Binary vs. Text Files• Compiler vs. Assembler• Mnemonic• Assembly Process• Development Process• Debugging• Example
57
Example
• VMWare virtual machine• running Linux operating system (lubuntu)
• Assemble, link, execute eatsyscall.asm
• Use KDbg debugger to analyze registers..