Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
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
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
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.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
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.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
6.828: PC hardware and x86 Frans Kaashoek
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
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.
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.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
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.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Recitation 2 – 2/4/02 Outline Floating Point Typecasting
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Assembly Language: Function Calls Jennifer Rexford.
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
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
Assembly function call convention
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
C function call conventions and the stack
Conditional Branch Example
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Aaron Miller David Cohen Spring 2011
Introduction to Compilers Tim Teitelbaum
Recitation 2 – 2/4/01 Outline Machine Model
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming 1 Introduction
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Instructor: David Ferry
Condition Codes Single Bit Registers
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming 2 Control Flow
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 2 Control Flow
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
X86 Assembly Review.
Machine-Level Representation of Programs (x86-64)
Presentation transcript:

Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall 1315

Lab 2 reminder Must be done on FISH machines –Check if you can login –“fish.pl” (by &

Assembly Format Op Src, Dest –add %eax, %ebx# %ebx += %eax –sub %eax, %ebx# %ebx -= %eax Op Arg –jmp 0x # unconditional branch –jge 0x # branch if >= in signed # comparison

Special registers %eaxreturn value %eipinstruction pointer %ebpbase (stack frame) pointer %espstack pointer

Simple sddressing modes $1010 (R)Mem[R] 10(R)Mem[R + 10] 0x10(R)Mem[R + 16]

Indexed addressing modes Generic Form D(Rb, Ri, S)Mem[Reg[Rb]+S*Reg[Ri]+D] Examples (Rb,Ri)Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri)Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S)Mem[Reg[Rb]+S*Reg[Ri]]

Stack frames Abstract partitioning of the stack Each Frame contains the state for a single function instant Growing downwards Stack Pointer ( %esp ) Frame Pointer ( %ebp ) Return Addr Saved Registers Argument Build Old %ebp Local Variables Arguments Caller Frame

Function & stack Function Setup Save Old Base Pointer (pushl %ebp) Set up own base pointer (movl %esp, %ebp) –Note that this saves the old stack pointer Save any registers that could be clobbered –Where? Function Body Operations on data, loops, function calls

Function & stack Function Cleanup Return value placed in %eax –What about returning larger values? (structs, doubles) Restore Caller’s Stack Pointer (movl %ebp, %esp) Restore Old Base Pointer (popl %ebp) “leave” same as above sequence Return –Where does it return to?

Procedure Related Instructions int a_func (int arg1, int arg2, int arg3) Get arguments: –arg1: mov 8(%ebp),%ecx –arg2: mov 12(%ebp),%ecx –arg3? Set return value: –mov 0x1, %eax# return 1; mov 16(%ebp),%ecx

Example 1: Arithmetic int func1(int a, int b) { int x, y; x = a + b; y = 2*x - b; return x*y; }

func1 assembly func1: push %ebp # save frame pointer mov %esp,%ebp # frame ptr = stack ptr mov 0xc(%ebp),%ecx # %ecx = b mov 0x8(%ebp),%eax # %eax = a add %ecx,%eax # %eax = x = a + b lea (%eax,%eax,1),%edx # %edx = 2 * x sub %ecx,%edx # %edx = y = 2 * x - b imul %edx,%eax # %eax = x * y leave # restore stack pointer # restore frame pointer ret

Using gdb with func1 break func1 run disas where print/x $eax print/x $ecx

Example 2: Control int func2(int a, int b) { if(a>b) return a; else return b; }

ASM for func2 func2: push %ebp # save frame pointer mov %esp,%ebp # frame ptr = stack ptr mov 0x8(%ebp),%eax # %eax = a mov 0xc(%ebp),%ecx # %ecx = b mov %eax,%edx # %edx = $eax = a cmp %ecx,%eax # compare (a>b) jg.L1 # a > b mov %ecx,%edx # %edx = %ecx = b.L1: # mov %edx,%eax # %eax = %edx leave # restore stack pointer # restore frame pointer ret

Example 3: loop int get_sum(int * array, int size) { int sum = 0; int i=0; for (i=0; i<size; i++) sum += array[i]; return sum; } get_sum: push %ebp mov %esp,%ebp push %ebx mov 8(%ebp),%ebx mov 12(%ebp),%ecx mov $0,%eax mov %eax,%edx cmp %ecx,%eax jge.L4.L6: add (%ebx,%edx,4),%eax inc %edx cmp %ecx,%edx jl.L6.L4: mov (%esp), %ebx leave ret

Example 3: loop get_sum: push %ebp mov %esp,%ebp push %ebx mov 8(%ebp),%ebx mov 12(%ebp),%ecx mov $0,%eax mov %eax,%edx cmp %ecx,%eax jge.L4.L6: add (%ebx,%edx,4),%eax inc %edx cmp %ecx,%edx jl.L6.L4: mov (%esp,1), %ebx leave ret # ebx = array # ecx = size # eax = 0 //return value # edx = 0 //i # # if (i>size) goto L4 # eax += Mem[ebx+edx*4] # edx ++ //i++ # # if (edx < ecx) goto L6 # # restore ebx

Defuse a simple bomb