Recitation 2 – 2/4/01 Outline Machine Model

Slides:



Advertisements
Similar presentations
Compiladores I- 1 - COMPARACION PROGRAMA FUENTE Y ENSAMBLADOR GENERADO.
Advertisements

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.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
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.
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.
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.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
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
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.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Recitation 3: Procedures and the Stack
A job ad at a game programming company
X86 Assembly - Data.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Machine-Level Programming I:
X86 Assembly, and C-to-assembly
C function call conventions and the stack
IA32 Processors Evolutionary Design
Conditional Branch Example
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Aaron Miller David Cohen Spring 2011
Homework In-line Assembly Code Machine Language
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming II: Arithmetic & Control
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Ch. 2 Two’s Complement Boolean vs. Logical Floating Point
Machine-Level Programming 1 Introduction
Computer Architecture adapted by Jason Fritts then by David Ferry
asum.ys A Y86 Programming Example
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
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming 2 Control Flow
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
Machine-Level Programming I: Introduction Feb. 1, 2000
Machine-Level Programming I: Introduction
X86 Assembly - Data.
Machine-Level Programming: Introduction
Machine-Level Representation of Programs II
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
CS-447– Computer Architecture M,W 10-11:20am Lecture 5 Instruction Set Architecture Sep 12th, 2007 Majd F. Sakr
Machine-Level Programming I: Introduction Sept. 10, 2002
Presentation transcript:

15-213 Recitation 2 – 2/4/01 Outline Machine Model Assembly Programming Structure Addressing Modes L2 Practice Stuff James Wilson e-mail: wilson2@andrew.cmu.edu Office Hours: Friday 1:30 – 3:00 Wean 52XX Cluster

Machine Model CPU Memory Addresses Registers E I P Object Code Program Data Data Condition Codes Instructions Stack

Special Registers %eax Return Value %eip Instruction Pointer %ebp Base (Stack Frame) Pointer %esp Stack Pointer

Assembly Programming: Structure 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

Assembly Programming: Structure Function Cleanup Return value placed in %eax What about returning larger values? (structs, doubles, etc.) Restore Caller’s Stack Pointer (movl %ebp, %esp) Restore Old Base Pointer (popl %ebp) Return Where does it return to?

Assembly Programming: Simple Addressing Modes Examples (R) Mem[R] $10(R) Mem[R + 10] $0x10(R) Mem[R + 16]

Assembly Programming: 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]]

Example 1: Simple Stuff int foo(int x, int y) { int t, u; t = 3*x-6*y; Convert to Assembly int foo(int x, int y) { int t, u; t = 3*x-6*y; t = t - 1; u = 16*t; return -u*t; }

Example 1: Answer Method 1 foo: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax movl $3, %ecx imull %ecx,%edx movl $6, %ecx imull %ecx,%eax subl %eax,%edx decl %edx movl %edx,%eax sall $4,%eax imull %edx,%eax negl %eax movl %ebp,%esp popl %ebp ret Method 2 foo: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax leal (%edx,%edx,2),%edx leal (%eax,%eax,2),%eax addl %eax,%eax subl %eax,%edx decl %edx movl %edx,%eax sall $4,%eax imull %edx,%eax negl %eax movl %ebp,%esp popl %ebp ret

Example 2: More Simple Stuff Convert to Assembly int absdiff(int x, int y) { if (x>=y) return x-y; else return y-x; }

Example 2: Answer absdiff: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx # edx = x movl 12(%ebp),%eax # eax = y cmpl %eax,%edx # jl .L3 # if (x<y) goto L3 subl %eax,%edx # edx = x-y movl %edx,%eax # eax = x-y jmp .L6 # goto L6 .L3: subl %edx,%eax # eax = y-x .L6: movl %ebp,%esp popl %ebp ret

Example 3: Backwards Convert to C get_sum: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx # ebx = 1st arg movl 12(%ebp),%ecx # ecx = 2nd arg xorl %eax,%eax # eax = 0 movl %eax,%edx # edx = 0 cmpl %ecx,%eax # jge .L4 # if (ecx >= 0) goto L4 .L6: addl (%ebx,%edx,4),%eax # eax += Mem[ebx+edx*4] incl %edx # edx ++ cmpl %ecx,%edx # jl .L6 # if (ecx < edx) goto L6 .L4: popl %ebx movl %ebp,%esp popl %ebp ret

Example 3: Answer int get_sum(int * array, int size) { int sum = 0; int i=0; for (i=0; i<size; i++) sum += array[i]; return sum; }

Example 4: Jump Tables Convert to Assembly int calc(int operation, int x, int y) { int result; switch(operation) { case 0: result = x + y; break; case 1: result = x – y; case 2: result = x * y; case 3: result = x / y; case 4: result = x % y; default: result = 0; break; } return result;

Example 4: Answer (sort of) .rodata .L10: .long .L4 .long .L5 .long .L6 .long .L7 .long .L8 calc: pushl %ebp movl %esp,%ebp pushl %esi pushl %ebx movl 8(%ebp),%ecx movl 12(%ebp),%ebx movl 16(%ebp),%esi cmpl $4,%ecx ja .L9 jmp *.L10(,%ecx,4) .L4: addl %esi,%ebx jmp .L3 .L5: subl %esi,%ebx .L6: imull %esi,%ebx jmp .L3 .L7: movl %ebx,%eax cltd idivl %esi movl %eax,%ebx .L8: movl %edx,%ebx .L9: xorl %ebx,%ebx .L3: popl %ebx popl %esi movl %ebp,%esp popl %ebp ret

Example 4: Answer (sort of) The real assembler output has alignment directives

Example 5 A mystery function int mystery(int x, int y) is compiled into the assembly on the next page. What is the function?

Example 5 A0: mystery: A1: movl 8(%ebp), %edx A2: movl $0x0, %eax A3: cmpl $0x0, 12(%ebp) A4: je .L11 A5: .L8 A6: cmpl $0x0, 12(%ebp) A7: jle .L9 A8: add %edx, %eax A9: decl 12(%ebp) A10: jmp .L10 A11:.L9 A12: sub %edx, %eax A13: incl 12(%ebp) A14:.L10 A15: compl $0x0, 12(%ebp) A16: jne .L8 A17:.L11 A18: mov %ebp, %esp A19: pop %ebp A20: ret

Example 5 A0: mystery: A1: movl 8(%ebp), %edx A2: movl $0x0, %eax A3: cmpl $0x0, 12(%ebp) A4: je .L11 A5: .L8 A6: cmpl $0x0, 12(%ebp) A7: jle .L9 A8: add %edx, %eax A9: decl 12(%ebp) A10: jmp .L10 A11:.L9 A12: sub %edx, %eax A13: incl 12(%ebp) A14:.L10 A15: compl $0x0, 12(%ebp) A16: jne .L8 A17:.L11 A18: mov %ebp, %esp A19: pop %ebp A20: ret # Get x # Set result = 0 # Compare y:0 # If(y == 0) goto Done # Loop: # If(y <= 0) goto Negative # result += x # y— # goto Check # Negative: # result -= x # y++ # Check: # If(y != 0) goto Loop # Done: # # Cleanup

Example 5 So what is the function computing? /* x times y, where x and y may be pos. or neg. */ int multiply(int x, int y) { int result = 0; while( y != 0 ) { if (y > 0) { result += x; y--; } else { result -= x; y++; return result;

Challenge Problem A function with prototype int mystery2(int *A, int x, int N) is compiled into the assembly on the next page. Hint: A is an array of integers, and N is the length of the array. What is this mystery function computing?

Challenge Problem A0: mystery2: A1: push %ebp A2: movl %esp, %ebp A3: movl 8(%ebp), %ebx A4: movl 16(%ebp), %edx A5: movl $0xffffffff, %eax A6: movl $0x0, 0xfffffff4(%ebp) A7: decl %edx A8: compl %edx, %ecx A9: jg .L13

Challenge Problem (continued) A11: add %edx, %ecx A12: sarl $0x1, %ecx A13: cmpl 12(%ebp), (%ebx, %ecx, 4) A14: jge .L10 A15: incl %ecx A16: movl %ecx, 0xfffffff4(%ebp) A17: jmp .L12 A18: .L10 A19: cmpl (%ebx, %ecx, 4), 12(%ebp) A20: jle .L11 A21: movl %ecx, %edx A22: decl %edx A23: jmp .L12

Challenge Problem (more code) A25: movl %ecx, %eax A26: jmp .L13 A27: .L12 A28: cmpl %edx, 0xfffffff4 A29: jle .L9 A30: .L13 A31: movl %ebp, %esp A32: pop %ebp A33: ret

Challenge Problem Answer: Binary Search # Set up A0: mystery2: # # ebx = Array A # edx = High = N # result = -1 # Low = 0 # ecx = Mid = Low # High— # Compare Low:High # If Low > High goto Done A0: mystery2: A1: push %ebp A2: movl %esp, %ebp A3: movl 8(%ebp), %ebx A4: movl 16(%ebp), %edx A5: movl $0xffffffff, %eax A6: movl $0x0, 0xfffffff4(%ebp) A7: decl %edx A8: compl %edx, %ecx A9: jg .L13

Challenge Problem (continued) A11: add %edx, %ecx A12: sarl $0x1, %ecx A13: cmpl 12(%ebp), (%ebx, %ecx, 4) A14: jge .L10 A15: incl %ecx A16: movl %ecx, 0xfffffff4(%ebp) A17: jmp .L12 A18: .L10 A19: cmpl (%ebx, %ecx, 4), 12(%ebp) A20: jle .L11 A21: movl %ecx, %edx A22: decl %edx A23: jmp .L12 # Loop: # Mid += High # Mid /= 2 # Compare A[Mid]:X # If >=, goto High # Mid++ # Low = Mid # goto Check # High: # Compare X:A[Mid] # If <=, goto Found # High = Mid # High—

Challenge Problem (more code) # Found: # Result = Mid # goto Done # Check: # Compare Low:High # If <=, goto Loop # Done: # # Cleanup A24: .L11 A25: movl %ecx, %eax A26: jmp .L13 A27: .L12 A28: cmpl %edx, 0xfffffff4 A29: jle .L9 A30: .L13 A31: movl %ebp, %esp A32: pop %ebp A33: ret

Challenge Problem int binsearch(int *A, int X, int N) { int Low, Mid, High; Low = 0; High = N - 1; while( Low <= High ) { Mid = ( Low + High ) / 2; if( A[ Mid ] < X ) Low = Mid + 1; else if( A[ Mid ] > X ) High = Mid - 1; return Mid; /* Found */ } return -1;