C Prog. To Object Code text text binary binary Code in files p1.c p2.c

Slides:



Advertisements
Similar presentations
Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
Advertisements

Machine Programming I: Basics
Machine-Level Programming I: Basics
University of Washington Instruction Set Architectures ISAs Brief history of processors and architectures C, assembly, machine code Assembly basics: registers,
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-243: Introduction to Computer Systems 4 th Lecture, Sep. 2, 2010 Instructors: Randy Bryant.
Instruction Set Architectures
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Tue. May 27, 2015 Instructors: Nathaniel.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS.
Machine-Level Programming I: Introduction January 29, 2002 Topics Assembly Programmer’s Execution Model Accessing Information –Registers –Memory Arithmetic.
Machine-Level Programming I: Introduction Sept. 10, 2002 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction
– 1 – ISAs and Microarchitectures Instruction Set Architecture The interface between hardware and software “Language” + programmer visible state + I/O.
Machine-Level Programming I: Introduction Jan 27, 2004 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Introduction Apr. 10, 2006 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction Apr. 14, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
1 Assemblers and Linkers. 2 Goals for this Lecture Help you to learn about: The assembly process IA-32 machine language Why? Machine language is the last.
Machine-Level Programming I: Introduction Sept. 14, 2004 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction Sept. 10, 2007 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction Sept. 10, 2002 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Sep. 11, 2012 Instructors: Dave O’Hallaron,
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 14, 2013 These slides are from website which accompanies the.
Carnegie Mellon 1 Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5 th Lecture, Jan 28, 2014 Instructors: Seth Copen.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
1 Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2015 Instructor: John Barr * Modified slides from.
Lee CSCE 312 TAMU 1 Machine-Level Programming I: Basics Instructor: Dr. Hyunyoung Lee Based on slides provided by: Randy Bryant and Dave O’Hallaron.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics Sep. 15, 2015.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Recitation 6 – 2/26/01 Outline Linking Exam Review –Topics Covered –Your Questions Shaheen Gandhi Office Hours: Wednesday.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Carnegie Mellon 1 Machine-Level Programming I: Basics Slides based on from those of Bryant and O’Hallaron.
Lec 4Systems Architecture1 Systems Architecture Lecture 4: Compilers, Assemblers, Linkers & Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
Machine-Level Programming I: Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS 105 “Tour of.
University of Washington Basics of Machine Programming The Hardware/Software Interface CSE351 Winter 2013.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University Ref.: Carnegie Mellon, CSPP, Intel, AMD.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Lecture 4 Assembly Language Topics Finish IEEE Floating Point multiplication, addition Lab 1 comments Assembly Language Intro January 25, 2011 CSCE 212.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
University of Washington Today Lab1, how is it going?  Now due on Sunday HW 1 out! (Integer, FP, …) Luis out of town Monday (no class)  Spend the time.
1 Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2015 Instructor: John Barr * Modified slides from.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
OUTLINE 2 Pre-requisite Bomb! Pre-requisite Bomb! 3.
Machine-Level Programming I: Introduction Jan. 30, 2001 Topics Assembly Programmer’s Execution Model Accessing Information –Registers –Memory Arithmetic.
Spring 2016Assembly Review Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Spring 2016Machine Code & C Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
1 Binghamton University Machine-Level Programming I: Basics CS220: Computer Systems II.
X86 Assembly - Data.
ISAs and Microarchitectures
Instruction Set Architecture
Machine-Level Programming I:
Machine-Level Programming I:
Machine-Level Programming 1 Introduction
Y86 Processor State Program Registers
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming I: Introduction Feb. 1, 2000
Machine-Level Programming I: Introduction
The Runtime Environment
X86 Assembly - Data.
Machine-Level Programming: Introduction
Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan
Machine-Level Programming I: Basics
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Machine-Level Programming I:
Machine-Level Programming I:
Machine-Level Programming I: Introduction Sept. 10, 2002
Presentation transcript:

C Prog. To Object Code text text binary binary Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc –S xxx.c) Asm program (p1.s p2.s) text Assembler (gcc –c xxx.c or as) Object program (p1.o p2.o) Static libraries (.a) binary Linker (gcc or ld) binary Executable program (p)

Compiling into Assembly C Code Generated IS32 Assembly int sum(int x, int y) { int t = x+y; return t; } sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax movl %ebp,%esp popl %ebp ret Log to mercury.cs.uml.edu only Obtain with command gcc -O -S code.c Produces file code.s

Object Code Code for sum Assembler Translates .s into .o Binary encoding of each instruction Nearly-complete image of executable code Missing linkages between code in different files Linker Resolves references between files Combines with static run-time libraries E.g., code for malloc, printf Some libraries are dynamically linked Linking occurs when program begins execution 0x401040 <sum>: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x08 0xec 0x5d 0xc3 Total of 13 bytes Each instruction 1, 2, or 3 bytes Starts at address 0x401040

Machine Instruction Example Assembly Add 2 4-byte integers Operands: x: Register %eax y: Memory M[%ebp+8] t: Register %eax Return function value in %eax Object Code 3-byte instruction Stored at address 0x401046 addl 8(%ebp),%eax Similar to expression: x += y More precisely: int eax; int *ebp; eax += ebp[2] 0x401046: 03 45 08

Disassembling Object Code Disassembled 00401040 <_sum>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 0c mov 0xc(%ebp),%eax 6: 03 45 08 add 0x8(%ebp),%eax 9: 89 ec mov %ebp,%esp b: 5d pop %ebp c: c3 ret d: 8d 76 00 lea 0x0(%esi),%esi Disassembler objdump -d p Useful tool for examining object code Analyzes bit pattern of series of instructions Produces approximate rendition of assembly code Can be run on either a.out (complete executable) or .o file

Alternate Disassembly Disassembled Object 0x401040 <sum>: push %ebp 0x401041 <sum+1>: mov %esp,%ebp 0x401043 <sum+3>: mov 0xc(%ebp),%eax 0x401046 <sum+6>: add 0x8(%ebp),%eax 0x401049 <sum+9>: mov %ebp,%esp 0x40104b <sum+11>: pop %ebp 0x40104c <sum+12>: ret 0x40104d <sum+13>: lea 0x0(%esi),%esi 0x401040: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x08 0xec 0x5d 0xc3 Within gdb Debugger gdb p disassemble sum Disassemble procedure x/13b sum Examine the 13 bytes starting at sum

Binary Bomb project Worth twice the points of MIPS programming assignments Download the bomb from http://mercury.cs.uml.edu:15213 File transfer it to mercury.cs.uml.edu Untar it Has three files binary bomb, bomb.c, README