C structures and Compilation to IA32

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
A very simple kernel Anders P. Ravn April A kernel specification /* kernel.h Interface to a lightweight kernel that implements concurrent processes.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Architecture Chapter 2 - Supplement Additional Features In Chapter 2.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
1 Homework / Exam Turn in mp2 at start of class today Reading –PAL, pp 3-6, Exam #1 next class –Open Book / Open Notes –NO calculators or other.
Dynamic Memory Management CAS CS210 Ying Ye Boston University.
Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
1 Homework / Exam Reading –PAL, pp Homework –mp2 due before class number 12 Exam #1 –Class 13 (three sessions from today) –Open book / Open notes.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: April 19, 2006 CSCE 531 Compiler Construction.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Y86 Processor State Program Registers
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
University of Washington x86 Programming III The Hardware/Software Interface CSE351 Winter 2013.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
CS642: Computer Security X86 Review Process Layout, ISA, etc. Drew Davidson
CS 3214 Computer Systems Godmar Back Lecture 8. Announcements Stay tuned for Project 2 & Exercise 4 Project 1 due Sep 16 Auto-fail rule 1: –Need at least.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
A job ad at a game programming company
CS 177 Computer Security Lecture 9
Assembly function call convention
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Instruction Set Architecture
Assembly Language Programming IV: shift, struct, recursion
Credits and Disclaimers
C function call conventions and the stack
Conditional Branch Example
143A: Principles of Operating Systems Lecture 4: Calling conventions
Exploiting & Defense Day 2 Recap
Homework In-line Assembly Code Machine Language
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
Chapter 3 Machine-Level Representation of Programs
Heterogeneous Data Structures & Alignment
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs (x86-64)
02/02/10 20:53 Assembly Questions תרגול 12 1.
Credits and Disclaimers
Compiler Construction CS 606 Sohail Aslam Lecture 1.
Presentation transcript:

C structures and Compilation to IA32 If you’re not careful, data in structures can be copied multiple times Doesn’t matter for small structures, but…

Simple example typedef struct p { int x, y; } point; void make() { point p, q; p.x = 1; p.y = 2; q = move(p, 3, 4); } point move(point p, int dx, int dy) { p.x += dx; p.y += dy; return p;

Original C code IA32 code void make() { point p, q; p.x = 1; p.y = 2; q = move(p, 3, 4); } IA32 code make: pushl %ebp movl %esp, %ebp subl $44, %esp movl $1, -8(%ebp) # p.x = 1 movl $2, -4(%ebp) # p.y = 2 leal -24(%ebp), %eax # &c in %eax movl $4, 16(%esp) # dy is 5th param (3rd in C) movl $3, 12(%esp) # dx is 4th param (2nd in C) movl -8(%ebp), %edx movl -4(%ebp), %ecx movl %edx, 4(%esp) # xcopy = p.x (2nd param) movl %ecx, 8(%esp) # ycopy = p.y (3rd param) movl %eax, (%esp) # &c is 1st param call move subl $4, %esp movl -24(%ebp), %eax # c.x in %eax movl -20(%ebp), %edx # c.y in %edx movl %eax, -16(%ebp) # q.x = c.x movl %edx, -12(%ebp) # q.y = c.y

IA32 code Original C code void make() { make: point p, q; pushl %ebp p.x = 1; p.y = 2; q = move(p, 3, 4); } More like IA32 code point p, q, c; int xcopy, ycopy; xcopy = p.x; ycopy = p.y; move(&c, xcopy, ycopy, 3, 4); q.x = c.x; q.y = c.y; IA32 code make: pushl %ebp movl %esp, %ebp subl $44, %esp movl $1, -8(%ebp) # p.x = 1 movl $2, -4(%ebp) # p.y = 2 leal -24(%ebp), %eax # &c in %eax movl $4, 16(%esp) # dy is 5th param (3rd in C) movl $3, 12(%esp) # dx is 4th param (2nd in C) movl -8(%ebp), %edx movl -4(%ebp), %ecx movl %edx, 4(%esp) # xcopy = p.x (2nd param) movl %ecx, 8(%esp) # ycopy = p.y (3rd param) movl %eax, (%esp) # &c is 1st param call move subl $4, %esp movl -24(%ebp), %eax # c.x in %eax movl -20(%ebp), %edx # c.y in %edx movl %eax, -16(%ebp) # q.x = c.x movl %edx, -12(%ebp) # q.y = c.y

More like IA32 code void make() { point p, q, c; int xcopy, ycopy; p.x = 1; p.y = 2; xcopy = p.x; ycopy = p.y; move(&c, xcopy, ycopy, 3, 4); q.x = c.x; q.y = c.y; } make: pushl %ebp movl %esp, %ebp subl $44, %esp movl $1, -8(%ebp) # p.x = 1 movl $2, -4(%ebp) # p.y = 2 leal -24(%ebp), %eax # &c in %eax movl $4, 16(%esp) # dy is 5th param (3rd in C) movl $3, 12(%esp) # dx is 4th param (2nd in C) movl -8(%ebp), %edx movl -4(%ebp), %ecx movl %edx, 4(%esp) # xcopy = p.x (2nd param) movl %ecx, 8(%esp) # ycopy = p.y (3rd param) movl %eax, (%esp) # &c is 1st param call move subl $4, %esp movl -24(%ebp), %eax # c.x in %eax movl -20(%ebp), %edx # c.y in %edx movl %eax, -16(%ebp) # q.x = c.x movl %edx, -12(%ebp) # q.y = c.y

int x, int y, int dx, int dy) { x += dx; y += dy; c -> x = x; Original C code point move(point p, int dx, int dy) { p.x += dx; p.y += dy; return p; } move: pushl %ebp movl %esp, %ebp movl 8(%ebp), %ecx # &r in %ecx movl 12(%ebp), %eax # --- addl 20(%ebp), %eax # c.x += dx movl %eax, 12(%ebp) # --- movl 16(%ebp), %eax # --- addl 24(%ebp), %eax # c.y += dy movl %eax, 16(%ebp) # --- movl 12(%ebp), %eax # c.x in %eax movl 16(%ebp), %edx # c.y in %edx movl %eax, (%ecx) # r->x = c.x movl %edx, 4(%ecx) # r->y = c.y movl %ecx, %eax # return r More like IA32 code point *move(point &c, int x, int y, int dx, int dy) { x += dx; y += dy; c -> x = x; c -> y = y; return c;

Original C code void make() { point p, q; p.x = 1; p.y = 2; q = move(p, 3, 4); } point move(point p, int dx, int dy) { p.x += dx; p.y += dy; return p; } More like IA32 code void make() { point p, q, c; p.x = 1; p.y = 2; xcopy = p.x; ycopy = p.y; move(&c, xcopy, ycopy, 3, 4); q.x = c.x; q.y = c.y; } point *move(point &c, int x, int y, int dx, int dy) { x += dx; y += dy; c -> x = x; c -> y = y; return c;

Same program, Using pointers typedef struct p { int x, y; } point; // same code, except p is modified void make() { point p; p.x = 1; p.y = 2; move(&p, 3, 4); } void move(point *ptr, int dx, int dy) { ptr -> x += dx; ptr -> y += dy;

Original C code void make() { point p; p.x = 1; p.y = 2; move(&p, 3, 4); } IA32 code make: pushl %ebp movl %esp, %ebp subl $28, %esp movl $1, -8(%ebp) # p.x = 1 movl $2, -4(%ebp) # p.y = 2 movl $4, 8(%esp) # 4 is 3rd param movl $3, 4(%esp) # 3 is 2nd param leal -8(%ebp), %eax # &p is 1st param movl %eax, (%esp) call move leave ret They’re the same!!

Original C code void move(point * ptr, int dx, int dy) { ptr -> x += dx; ptr ->y += dy; } move: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax # ptr in %eax movl (%eax), %eax # ptr -> x in %eax movl %eax, %edx addl 12(%ebp), %edx # ptr -> x += dx movl 8(%ebp), %eax movl %edx, (%eax) movl 4(%eax), %eax # ptr-> y in %eax addl 16(%ebp), %edx # ptr -> y += dy movl %edx, 4(%eax) They’re the same!

Without explicit pointers void make() { point p, q, c; p.x = 1; p.y = 2; xcopy = p.x; ycopy = p.y; move(&c, xcopy, ycopy, 3, 4); q.x = c.x; q.y = c.y; } point *move(point &c, int x, int y, int dx, int dy) { x += dx; y += dy; c -> x = x; c -> y = y; return c; How many copies of data??

Without explicit pointers void make() { point p, q, c; p.x = 1; p.y = 2; xcopy = p.x; ycopy = p.y; move(&c, xcopy, ycopy, 3, 4); q.x = c.x; q.y = c.y; } point *move(point &c, int x, int y, int dx, int dy) { x += dx; y += dy; c -> x = x; c -> y = y; return c; How many copies of data?? --- 2! Only 16 bytes in this example, but with more complex structures, …

With pointers – Only the pointer is copied (4 bytes) With a more complex structure, still only 4 bytes typedef struct p { int x, y; } point; // same code, except p is modified void make() { point p; p.x = 1; p.y = 2; move(&p, 3, 4); } void move(point *ptr, int dx, int dy) { ptr -> x += dx; ptr -> y += dy;