Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.

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

Recitation 4: 09/30/02 Outline The Stack! Essential skill for Lab 3 –Out-of-bound array access –Put your code on the stack Annie Luo
University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Structured Data: Sept. 20, 2001 Topics Arrays Structs Unions class08.ppt “The course that gives CMU its Zip!”
Practical session 7 review. Little – Endian What’s in memory? Section.rodata a: DB ‘hello’, 0x20, ’world’, 10, 0 b: DW ‘hello’, 0x20, ’world’, 10, 0 c:
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
UBC104 Embedded Systems Variables, Structures & Pointers.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
UBC104 Embedded Systems Functions & Pointers.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
– 1 – Referencing Examples Code Does Not Do Any Bounds Checking! ReferenceAddressValueGuaranteed? mit[3]36 + 4* 3 = 483 Yes mit[5]36 + 4* 5 = 566 No mit[-1]36.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Machine-Level Programming 6 Structured Data Topics Structs Unions.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 4: The Stack & Lab3 Andrew Faulring Section A 30 September 2002.
Recitation 6 – 2/26/01 Outline Linking Exam Review –Topics Covered –Your Questions Shaheen Gandhi Office Hours: Wednesday.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Bits and Bytes September 1, F’05 class02.ppt “The Class That Gives CMU Its Zip!”
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
Machine-level Programming IV: Data Structures Topics –Arrays –Structs –Unions.
Lecture 9 Aggregate Data Organization Topics Pointers Aggregate Data Array layout in memory Structures February 14, 2012 CSCE 212 Computer Architecture.
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7.
1 Array. 2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Recitation 3: Procedures and the Stack
A job ad at a game programming company
Reading Condition Codes (Cont.)
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Exploiting & Defense Day 2 Recap
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming IV: Structured Data Sept. 30, 2008
Heterogeneous Data Structures & Alignment
Referencing Examples Code Does Not Do Any Bounds Checking!
asum.ys A Y86 Programming Example
Machine-Level Programming 5 Structured Data
Machine-Level Programming 4 Procedures
Feb 2 Announcements Arrays/Structs in C Lab 2? HW2 released
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Structured Data Sept. 19, 2002
Machine-Level Programming IV: Data
Instructors: Majd Sakr and Khaled Harras
The Runtime Environment
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Programming IV: Data
Instructors: Majd Sakr and Khaled Harras
The Runtime Environment
Machine Level Representation of Programs (III)
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming: Introduction
Machine-Level Programming 5 Structured Data
Machine-Level Programming 5 Structured Data
Machine-Level Programming IV: Structured Data
Structured Data I: Homogenous Data Feb. 10, 2000
Instructor: Fatma CORUT ERGİN
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Presentation transcript:

Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early Minglong Shao Office hours: Thursdays 5-6PM Wean Hall 1315

Recursive procedure example: Fibonacci 0x push %ebp 0x mov %esp,%ebp 0x sub $0x10,%esp 0x push %esi 0x push %ebx 0x mov 0x8(%ebp),%ebx 0x804842b cmp $0x2,%ebx 0x804842e jg 0x x mov $0x1,%eax 0x jmp 0x x add $0xfffffff4,%esp 0x804843a lea 0xfffffffe(%ebx),%eax 0x804843d push %eax 0x804843e call 0x x mov %eax,%esi 0x add $0xfffffff4,%esp 0x lea 0xffffffff(%ebx),%eax 0x804844b push %eax 0x804844c call 0x x add %esi,%eax 0x lea 0xffffffe8(%ebp),%esp 0x pop %ebx 0x pop %esi 0x mov %ebp,%esp 0x804845a pop %ebp 0x804845b ret int fibo (int n) { int result; if (n <= 2) result = 1; else result = fibo(n-2) + fibo(n-1); return result; }

Stack Frame 0x push %ebp 0x mov %esp,%ebp 0x sub $0x10,%esp 0x push %esi 0x push %ebx... 0x lea 0xffffffe8(%ebp),%esp 0x pop %ebx 0x pop %esi 0x mov %ebp,%esp 0x804845a pop %ebp 0x804845b ret old %ebp old %esi old %ebx rtrn addr x %ebp %esp Stack at this point

Write Comments For Body 0x mov 0x8(%ebp),%ebx# ebx = x 0x804842b cmp $0x2,%ebx# if (x>2) 0x804842e jg 0x # goto L1 0x mov $0x1,%eax# eax = 1 0x jmp 0x # goto L2 0x add $0xfffffff4,%esp# L1: 0x804843a lea 0xfffffffe(%ebx),%eax# 0x804843d push %eax# push x-2 0x804843e call 0x # call fibo 0x mov %eax,%esi# esi = eax 0x add $0xfffffff4,%esp# 0x lea 0xffffffff(%ebx),%eax# 0x804844b push %eax# push x-1 0x804844c call 0x # call fibo 0x add %esi,%eax# eax += esi 0x # L2:

Stack Changes of fibo(3) fibo(3) %ebp %esp call fibo(1) fibo(3) fibo(1) %ebp %esp 1 0x fibo(3) %ebp %esp return

Stack Changes of fibo(3) call fibo(2) fibo(3) fibo(2) %ebp %esp fibo(3) %ebp %esp return 1 fibo(3) %ebp %esp 0x

Arrays Allocated as contiguous blocks of memory Address Computation Example cmu[0]40+0*sizeof(int) = 40 cmu[3]40+3*sizeof(int) = 52 cmu[i]40+i*sizeof(int) = *I “x = cmu[i]” in assembly code #%edx = cmu, %eax = i movl (%edx, %eax, 4), %eax#%eax stores x int cmu[5] = {…}; /*cmu begins at address 40*/

Nested arrays Declaration – T A[R][C]; – Array of data type T – A[i] is an array of C elements – R rows – C columns – Type T element requires K bytes Array Size – R * C * K bytes Arrangement – Row-major ordering

Nested arrays: in terms of Memory A [i] [0] A [i] [C-1] A[i] A [R-1] [0] A [R-1] [C-1] A[R-1] A A [0] A [0] [C-1] A[0] int A[R][C]; A+i*C*4A+(R-1)*C*4 A[0][0]A[0][1]…A[0][c-1] ………… A[i][0]A[i][1]…A[i][c-1] ………… A[R-1][0]A[R-1][1]…A[R-1][C-1] Row-major ordering  Like a matrix  Starting address of A[i]

Nested arrays: in terms of code Suppose we have “int pgh[4][5]” Compute address of pgh[index][dig]: phg + index*sizeof(int)*5 + sizeof(int)*dig Assembly code: # %ecx = dig # %eax = index leal 0(,%ecx,4),%edx # 4*dig leal (%eax,%eax,4),%eax # 5*index movl pgh(%edx,%eax,4),%eax # *(pgh + 4*dig + 20*index)

Structures Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec { int i; int a[3]; int *p; }; Memory Layout iap 0416

Structures - Code Offset of each structure member determined at compile time struct rec { int i; int a[3]; int *p; }; iap 0416 r *idx r int * find_a (struct rec *r, int idx){ return &r->a[idx]; } # %ecx = idx # %edx = r leal 0(,%ecx,4),%eax# 4*idx leal 4(%eax,%edx),%eax# r+4*idx+4

Alignment Offsets Within Structure –Must satisfy element’s alignment requirement Overall Structure Placement –Each structure has alignment requirement K Largest alignment of any element –Initial address & structure length must be multiples of K Example (under Windows): –K = 8, due to ”double” element struct S1 { char c; int i[2]; double v; } *p; ci[0]i[1]v p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8

Unions Overlay union elements Allocate according to largest element Can only use one field at a time union U1 { char c; int i[2]; double v; } *up; c i[0]i[1] v up+0up+4up+8

Homework problem 3.36 typedef struct { int left; a_struct a[CNT]; int right; } b_struct; void test(int i, b_struct *bp) { int n = bp->left + bp->right; a_struct *ap = &bp->a[i]; ap->x[ap->idx] = n; } Suppose a_struct only has elements: x and idx 1push %ebp 2mov %esp, %ebp 3push %ebx 4mov 0x8(%ebp), %eax 5mov 0xc(%ebp), %ecx 6lea (%eax,%eax,4),%eax 7lea 0x4(%ecx,%eax,4),%eax 8mov (%eax), %edx 9shl $0x2, %edx 10mov 0xb8(%ecx), %ebx 11add (%ecx), %ebx 12mov %ebx,0x4(%ebx,%eax,1) 13pop %ebx 14mov %ebp, %esp 15pop %ebp 16ret Assembly code of test: A piece of C code

Homework problem 3.36 Question: –Find out the value of CNT –Complete declaration of a_struct

Homework problem push %ebp 2mov %esp, %ebp 3push %ebx 4mov 0x8(%ebp), %eax# %eax = i 5mov 0xc(%ebp), %ecx# %ecx = bp 6lea (%eax,%eax,4),%eax# 7lea 0x4(%ecx,%eax,4),%eax# %eax = bp+4+4*(5*i) = ap 8mov (%eax), %edx# %edx = *(%eax) = idx 9shl $0x2, %edx# %edx = %edx * 4 = idx * 4 10mov 0xb8(%ecx), %ebx# 11add (%ecx), %ebx# %ebx = *(bp)+*(bp+0xb8)= n 12mov %ebx,0x4(%edx,%eax,1)# *(ap idx*4) = %ebx 13pop %ebx 14mov %ebp, %esp 15pop %ebp 16ret void test(int i, b_struct *bp){ int n = bp->left + bp->right; a_struct *ap = &bp->a[i]; ap->x[ap->idx] = n; }

Answer: CNT = 9 typedef struct { int idx; int x[4]; } a_struct; leftrightidxx[0]x[1]x[2]x[3] bp+0bp+184 idxx[0]x[1]x[2]x[3] bp->a[0] bp+4 bp->a[8] ……

Function pointer (C code) void (*pfunc) (char *message); /*declaration*/ void myprint(char *message) { printf(“my message: %s\n", message); } void message(void (*pfunc)(char *message), char *str) { pfunc(str); } int main(int argc, char* argv[]) { message(myprint, “hello world”); return 0; }

Function pointer (assembly code) main: push%ebp mov%esp, %ebp sub$0x8, %esp and$0xfffffff0, %esp mov$0x0, %eax sub%eax, %esp sub$0x8, %esp push0x push0x call0x add$0x10, %esp mov$0x0, %eax leave ret message: push%ebp mov%esp, %ebp sub$0x8, %esp sub$0xc, %esp push0xc(%ebp) mov0x8(%ebp), %eax call*%eax add$0x10, %esp leave ret Function pointer is starting address of a function myprint: 0x push%ebp 0x mov %esp, %ebp