Download presentation
Presentation is loading. Please wait.
Published byIsaac Sartell Modified over 9 years ago
1
Assembly 01
2
Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 1 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!! 2
4
3 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 … 4
6
5 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 6
8
Text File Contents 7 Bless Hex Editor text equivalent 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 8 this analogy will make sense…
10
Compiler vs. Assembler 9 high-level language assembly language compile assemble machine language (object code)
11
Compiler 10 high-level language assembly language 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 11
13
Assembler 12 high-level language assembly language machine language (object code) compile assemble
14
Mnemonic Example use of mov mnemonic: mov eax,4; place 4 in general 32-bit register eax 13
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) 14
16
Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 15
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 … 16
18
Mnemonic Example line of assembly: mov eax,4; place 4 in general 32-bit register eax 17
19
Mnemonic Example line of assembly: mov eax,4; place 4 in general 32-bit register eax 18 mnemonic
20
Mnemonic Example line of assembly: mov eax,4; place 4 in general 32-bit register eax 19 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 20 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 21 instruction: mnemonic and operand(s)
23
Mnemonic Assembler converts instruction into object code mov ebp,esp; save stack pointer to ebp register 0x8BEC 22 assembly language instruction gets assembled into… machine language instruction
24
Mnemonic Machine language instruction gets decoded… Execution cycle begins… 0x8BEC = 1000 1011 1110 1100 23
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 24
26
BREAK TIME!! Please stand up, stretch your legs, walk around… 25
27
Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 26
28
Assembly Process How to go from assembly source code to executable Two steps: 1.Assemble 2.Link 3.(Execute) 27
29
Assembly Process.asm assembler.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 29.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 30.o executable
32
Assembly Process 2) Execute Run the assembly code Run the machine language instructions… Do cool stuff… 31
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!! 32
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 33
35
Assembly Process Step 1: Assemble the source file eatsyscall.asm UNIX> nasm –f elf –g –F stabs eatsyscall.asm 34 invoke the nasm assembler
36
Assembly Process Step 1: Assemble the source file eatsyscall.asm UNIX> nasm –f elf –g –F stabs eatsyscall.asm 35 -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 36 -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 37 -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 38 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 39
41
Assembly Process Step 2: Link the object file(s) to create executable UNIX> ld –o eatsyscall eatsyscall.o 40 invoke the linker
42
Assembly Process Step 2: Link the object file(s) to create executable UNIX> ld –o eatsyscall eatsyscall.o 41 -o command 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 42 name of object file(s) to be linked together
44
Assembly Process Step 3: Execute the program UNIX>./eatsyscall Eat at Joe’s! 43./ (dot slash) indicates current directory
45
Assembly Process Step 3: Execute the program UNIX>./eatsyscall Eat at Joe’s! 44 eatsyscall executable program name
46
Assembly Process Step 3: Execute the program UNIX>./eatsyscall Eat at Joe’s! 45 output
47
Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 46
48
Development Process General idea for developing assembly code 1.Edit 2.Assemble 3.Link 4.Execute 5.Debug 6.Repeat.. 47
49
48.asm.o executable Assembler Linker Debugger.o start here no errors Assembler errors no errors Linker errors works perfectly!! you’re done!! doesn’t work previously assemble d object files editor
50
Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 49
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 50
52
Debugging 51 screensho t of KDbg
53
Debugging To begin debugging UNIX> kdbg eatsyscall 52 kdbg starts KDbg debugger GUI
54
Debugging To begin debugging UNIX> kdbg eatsyscall 53 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 54
56
Outline Binary vs. Text Files Compiler vs. Assembler Mnemonic Assembly Process Development Process Debugging Example 55
57
Example VMWare virtual machine running Linux operating system (lubuntu) Assemble, link, execute eatsyscall.asm Use KDbg debugger to analyze registers.. 56
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.