Presentation is loading. Please wait.

Presentation is loading. Please wait.

C structures and Compilation to IA32

Similar presentations


Presentation on theme: "C structures and Compilation to IA32"— Presentation transcript:

1 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…

2 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;

3 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 (%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 (%ebp), %eax # c.x in %eax movl (%ebp), %edx # c.y in %edx movl %eax, -16(%ebp) # q.x = c.x movl %edx, -12(%ebp) # q.y = c.y

4 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 (%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 (%ebp), %eax # c.x in %eax movl (%ebp), %edx # c.y in %edx movl %eax, -16(%ebp) # q.x = c.x movl %edx, -12(%ebp) # q.y = c.y

5 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 (%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 (%ebp), %eax # c.x in %eax movl (%ebp), %edx # c.y in %edx movl %eax, -16(%ebp) # q.x = c.x movl %edx, -12(%ebp) # q.y = c.y

6 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;

7 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;

8 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;

9 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!!

10 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!

11 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??

12 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?? ! Only 16 bytes in this example, but with more complex structures, …

13 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;


Download ppt "C structures and Compilation to IA32"

Similar presentations


Ads by Google