CSC 2400: Computer Systems. Using the Stack for Function Calls
|
|
- Aubrey Mills
- 5 years ago
- Views:
Transcription
1 CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing the calling function to continue where it left off Return address and contents of registers Stack: last-in-first-out data structure! Stack frame: args, local vars, return address, registers! Stack pointer: pointing to the current top of the stack Calling functions! CALL and instructions! PUSH into and POP from the stack frame! Using the base pointer EBP as a reference point 1
2 Function Calls main calls add! Push arguments on the stack! Push return address on stack! Jump to add! Allocate local variables on stack, etc. Returning to main! Clear the stack frame for add! Pop return address from stack int add(int a, int b, int c) { int d; } d = a + b + c; return d; int main() { int sum, avg; } sum = add(, 4, 5); avg = sum / ; return avg Stack Frame for add Return Address 4 5 Stack Frames High-Level Picture main begins executing main calls P (Extended Stack Pointer) Always points to the top element of the stack Bottom main s Stack Frame 2
3 High-Level Picture main begins executing main calls P P calls Q Bottom P s Stack Frame main s Stack Frame High-Level Picture main begins executing main calls P P calls Q Q returns Bottom Q s Stack Frame P s Stack Frame main s Stack Frame
4 High-Level Picture main begins executing main calls P P calls Q Q returns P returns Bottom P s Stack Frame main s Stack Frame High-Level Picture main begins executing main calls P P calls Q Q returns P returns main returns Bottom main s Stack Frame 4
5 High-Level Picture main begins executing main calls P P calls Q Q returns P returns main returns Bottom Function Call Details Call and Return instructions! call: push EIP on the stack, and jump to function! ret: pop the stack into the EIP to go back Argument passing between procedures! Calling function pushes arguments on to the stack! Called function reads/writes on the stack Local variables! Called function creates and manipulates on the stack Register saving conventions! Either calling or called function saves all of the registers before use 5
6 Call and Return Instructions Instruction Effective Operations PUSH Src SUB, 4 POP Dest CALL Addr MOV [], Src MOV Dest, [] ADD, 4 PUSH EIP JMP Addr POP EIP before CALL Note: can t really access EIP directly, but this is implicitly what call and ret are doing. Call and Return Instructions Instruction Effective Operations PUSH Src SUB, 4 POP Dest CALL Addr MOV [], Src MOV Dest, [] ADD, 4 PUSH EIP JMP Addr POP EIP after CALL Old EIP 6
7 Call and Return Instructions Instruction Effective Operations PUSH Src SUB, 4 POP Dest CALL Addr MOV [], Src MOV Dest, [] ADD, 4 PUSH EIP JMP Addr POP EIP before Old EIP Return instruction assumes that the return address is at the top of the stack Call and Return Instructions Instruction Effective Operations PUSH Src SUB, 4 POP Dest CALL Addr MOV [], Src MOV Dest, [] ADD, 4 PUSH EIP JMP Addr POP EIP after 7
8 Input Arguments Caller pushes input arguments before executing the CALL Parameters are pushed in the reverse order! Push N th argument first! Push 1 st argument last! So that first argument is at the top of the stack at the time of the Call before pushing arguments Input Arguments Caller pushes input parameters before executing the CALL Parameters are pushed in the reverse order! Push N th argument first! Push 1 st argument last! So that first argument is at top of the stack at the time of the CALL before CALL Arg 1 Arg Arg N 8
9 CALL Called function can address arguments relative to : Arg 1 as [+4] before CALL Arg 1 Arg Arg N after CALL Old EIP Arg 1 Arg Arg N : Returning to Caller before Old EIP Arg 1 Arg Arg N after Arg 1 Arg Arg N 9
10 Base Pointer: EBP As Callee executes, may change! E.g., preparing to call another function Use EBP as fixed reference point! E.g., to access arguments and other local variables Need to save old value of EBP! Before overwriting EBP register Callee begins by executing prolog MOV EBP, after CALL Old EIP Arg 1 Arg Arg N EBP Base Pointer: EBP As Callee executes, may change! E.g., preparing to call another function Use EBP as fixed reference point! E.g., to access arguments and other local variables Need to save old value of EBP! Before overwriting EBP register Callee begins by executing prolog MOV EBP,, EBP Old EBP Old EIP Arg 1 Arg Arg N Regardless of, Callee can address Arg 1 as [EBP+8] 1
11 Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes MOV, EBP EBP Old EBP Old EIP Arg 1 Arg Arg N Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes MOV, EBP, EBP Old EBP Old EIP Arg 1 Arg Arg N 11
12 Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes MOV, EBP Old EIP Arg 1 Arg Arg N EBP Base Pointer: EBP Before returning, Callee must restore EBP to its old value Executes MOV, EBP Arg 1 Arg Arg N EBP 12
13 Allocation for Local Variables Local variables of the Callee are also allocated on the stack Allocation done by moving the stack pointer Example: allocate two integers! SUB, 4! SUB, 4! (or equivalently, SUB, 8) Reference local variables using the base pointer! [EBP-4]! [EBP-8] EBP Var 2 Var 1 Old EBP Old EIP Arg 1 Arg Arg N Use of Registers Problem: Callee may use a register that the caller is also using! When callee returns control to caller, old register contents may be lost! Caller cannot continue where it left off Solution: save the registers on the stack! Callee must push register contents before altering! Callee must pop register contents before returning 1
14 A Simple Example int sum(int x, int y) { return x+y; } int main () { int result; } result = sum(1, 2); printf("%d\n", result); return ; Compile: gcc masm=intel sum.c Debug: gdb./a.out disas main Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 Recall that:! The stack grows from high addresses towards low addresses EBP 14
15 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 Recall that:! The stack grows from high addresses towards low addresses EBP Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 15
16 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD,
17 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 RA(main) 1 2 <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] 17
18 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 RA(main) 1 2 <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 RA(main) 1 2 EBP(sum) <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] 18
19 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 RA(main) 1 2 EBP(sum) <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] EAX 2 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 RA(main) 1 2 EBP(sum) <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] EAX 19
20 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 RA(main) 1 2 <sum>: MOV EBP, MOV EAX, DWORD PTR [EBP+12] ADD EAX, DWORD PTR [EBP+8] EAX Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, EAX 2
21 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, EAX Example.section.data.LC STRING "%d\n".lc MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD,
22 Example.section.data.LC STRING "%d\n".lc MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 Address of % Skipping the details of Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 Address of % 22
23 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 Address of % Prepare to: leave EAX Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 EAX 2
24 Example.LC MOV EBP, SUB, 16 MOV DWORD PTR [+4],2 MOV DWORD PTR [],1 MOV DWORD PTR [+4],EAX MOV DWORD PTR [], OFFSET:.LC MOV EAX, ADD, 16 EAX Summary Invoking a function! CALL: call the function! : return from the instruction Stack Frame for a function call includes! Function arguments! Return address! Local variables! Saved registers Base pointer EBP! Fixed reference point in the Stack Frame! Useful for referencing arguments and local variables 24
CSC 8400: Computer Systems. Using the Stack for Function Calls
CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing
More informationCSC 2400: Computer Systems. Using the Stack for Function Calls
CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing
More informationCSC 2400: Computing Systems. X86 Assembly: Function Calls"
CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing
More informationCSC 2400: Computing Systems. X86 Assembly: Function Calls
CSC 24: Computing Systems X86 Assembly: Function Calls 1 Lecture Goals Challenges of supporting functions Providing information for the called function Function arguments and local variables Allowing the
More informationAssembly Language: Function Calls
Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference
More informationAssembly Language: Function Calls" Goals of this Lecture"
Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"
More informationAssembly Language: Function Calls. Goals of this Lecture. Function Call Problems
Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning
More informationAssembly Language: Function Calls" Goals of this Lecture"
Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"
More informationStack -- Memory which holds register contents. Will keep the EIP of the next address after the call
Call without Parameter Value Transfer What are involved? ESP Stack Pointer Register Grows by 4 for EIP (return address) storage Stack -- Memory which holds register contents Will keep the EIP of the next
More informationFunction Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)
Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register
More informationSystems I. Machine-Level Programming V: Procedures
Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp
More informationAssembly Language: Function Calls
Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent instructions and conventions 2 Function Call Problems (1) Calling and returning
More informationPrinceton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls
Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent
More informationProgram Exploitation Intro
Program Exploitation Intro x86 Assembly 04//2018 Security 1 Univeristà Ca Foscari, Venezia What is Program Exploitation "Making a program do something unexpected and not planned" The right bugs can be
More information143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017
143A: Principles of Operating Systems Lecture 4: Calling conventions Anton Burtsev October, 2017 Recap from last time Stack and procedure calls What is stack? Stack It's just a region of memory Pointed
More informationThe IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta
1 The IA-32 Stack and Function Calls CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta 2 Important Registers used with the Stack EIP: ESP: EBP: 3 Important Registers used with the Stack EIP:
More informationFunction Call Convention
Function Call Convention Compass Security Schweiz AG Werkstrasse 20 Postfach 2038 CH-8645 Jona Tel +41 55 214 41 60 Fax +41 55 214 41 61 team@csnc.ch www.csnc.ch Content Intel Architecture Memory Layout
More information143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017
143A: Principles of Operating Systems Lecture 5: Calling conventions Anton Burtsev January, 2017 Stack and procedure calls Stack Main purpose: Store the return address for the current procedure Caller
More informationEECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution
1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can
More information238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018
238P: Operating Systems Lecture 3: Calling conventions Anton Burtsev October, 2018 What does CPU do internally? (Remember Lecture 01 - Introduction?) CPU execution loop CPU repeatedly reads instructions
More informationProcedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27
Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure
More informationCMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang
Project 3 Questions CMSC 313 Lecture 12 How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP
More informationCSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs
CSC 2400: Computer Systems Towards the Hardware: Machine-Level Representation of Programs Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32)
More informationCSC 8400: Computer Systems. Machine-Level Representation of Programs
CSC 8400: Computer Systems Machine-Level Representation of Programs Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32) 1 Compilation Stages
More informationCS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016
CS 31: Intro to Systems Functions and the Stack Martin Gagne Swarthmore College February 23, 2016 Reminders Late policy: you do not have to send me an email to inform me of a late submission before the
More informationCIT Week13 Lecture
CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there
More informationCNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux
CNIT 127: Exploit Development Ch 2: Stack Overflows in Linux Stack-based Buffer Overflows Most popular and best understood exploitation method Aleph One's "Smashing the Stack for Fun and Profit" (1996)
More informationCS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G
CS 33: Week 3 Discussion x86 Assembly (v1.0) Section 1G Announcements - HW2 due Sunday - MT1 this Thursday! - Lab2 out Info Name: Eric Kim (Section 1G, 2-4 PM, BH 5419) Office Hours (Boelter 2432) - Wed
More informationLecture 4 CIS 341: COMPILERS
Lecture 4 CIS 341: COMPILERS CIS 341 Announcements HW2: X86lite Available on the course web pages. Due: Weds. Feb. 7 th at midnight Pair-programming project Zdancewic CIS 341: Compilers 2 X86 Schematic
More informationProcedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29
Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure
More information16.317: Microprocessor Systems Design I Fall 2013
16.317: Microprocessor Systems Design I Fall 2013 Exam 2 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationx86 assembly CS449 Fall 2017
x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS
More informationX86 Stack Calling Function POV
X86 Stack Calling Function POV Computer Systems Section 3.7 Stack Frame Reg Value ebp xffff FFF0 esp xffff FFE0 eax x0000 000E Memory Address Value xffff FFF8 xffff FFF4 x0000 0004 xffff FFF4 x0000 0003
More informationQuestion 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110
Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question
More informationSubprograms: Local Variables
Subprograms: Local Variables ICS312 Machine-Level and Systems Programming Henri Casanova (henric@hawaii.edu) Local Variables in Subprograms In all the examples we have seen so far, the subprograms were
More informationIslamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures
Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB Lab # 10 Advanced Procedures May, 2014 1 Assembly Language LAB Stack Parameters There are
More informationSubprograms: Arguments
Subprograms: Arguments ICS312 Machine-Level and Systems Programming Henri Casanova (henric@hawaii.edu) Activation Records The stack is useful to store and rieve urn addresses, transparently managed via
More informationCS213. Machine-Level Programming III: Procedures
CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward
More informationAssembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University
Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline
More informationMachine Program: Procedure. Zhaoguo Wang
Machine Program: Procedure Zhaoguo Wang Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control int Q(int i) { int t, z; return z; Requirements of procedure calls? P() { y = Q(x); y++;
More informationASSEMBLY III: PROCEDURES. Jo, Heeseung
ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top
More informationAssembly III: Procedures. Jo, Heeseung
Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top
More informationStacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han
s and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements Homework Set #2 due Friday at 11 am - extension Program Assignment #1 due Tuesday Feb. 15 at 11 am - note extension
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 16, SPRING 2013 TOPICS TODAY Project 6 Perils & Pitfalls of Memory Allocation C Function Call Conventions in Assembly Language PERILS
More information16.317: Microprocessor Systems Design I Fall 2014
16.317: Microprocessor Systems Design I Fall 2014 Exam 2 Solution 1. (16 points, 4 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationadministrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?
administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? exam on Wednesday today s material not on the exam 1 Assembly Assembly is programming
More informationW4118: PC Hardware and x86. Junfeng Yang
W4118: PC Hardware and x86 Junfeng Yang A PC How to make it do something useful? 2 Outline PC organization x86 instruction set gcc calling conventions PC emulation 3 PC board 4 PC organization One or more
More informationCMSC 313 Lecture 12 [draft] How C functions pass parameters
CMSC 313 Lecture 12 [draft] How C functions pass parameters UMBC, CMSC313, Richard Chang Last Time Stack Instructions: PUSH, POP PUSH adds an item to the top of the stack POP removes an
More informationArguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values
1 Arguments and Return Values 2 EE 109 Unit 16 Stack Frames MIPS convention is to use certain registers for this task used to pass up to 4 arguments. If more arguments, use the stack used for return value
More informationChapter 14 Functions
Chapter 14 Functions Function Smaller, simpler, subcomponent of program Provides abstraction hide low-level details give high-level structure to program, easier to understand overall program flow enables
More informationSungkyunkwan University
Switch statements IA 32 Procedures Stack Structure Calling Conventions Illustrations of Recursion & Pointers long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case
More informationSystem Software Assignment 1 Runtime Support for Procedures
System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such
More informationCS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri
CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:
More informationPart 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack
Part 7 Stacks The Stack Piles of Data Stack Stack A stack is an abstract data structure that stores objects Based on the concept of a stack of items like a stack of dishes Data can only be added to or
More informationCompiler construction. x86 architecture. This lecture. Lecture 6: Code generation for x86. x86: assembly for a real machine.
This lecture Compiler construction Lecture 6: Code generation for x86 Magnus Myreen Spring 2018 Chalmers University of Technology Gothenburg University x86 architecture s Some x86 instructions From LLVM
More informationX86 Addressing Modes Chapter 3" Review: Instructions to Recognize"
X86 Addressing Modes Chapter 3" Review: Instructions to Recognize" 1 Arithmetic Instructions (1)! Two Operand Instructions" ADD Dest, Src Dest = Dest + Src SUB Dest, Src Dest = Dest - Src MUL Dest, Src
More informationCSE 351: Week 4. Tom Bergan, TA
CSE 35 Week 4 Tom Bergan, TA Does this code look okay? int binarysearch(int a[], int length, int key) { int low = 0; int high = length - ; while (low
More informationAssembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall
Assembly Language Programming: Procedures EECE416 uc Charles Kim Howard University Fall 2013 www.mwftr.com Before we start Schedule of the next few weeks T Nov 19: Procedure and Calls (continued) R Nov
More informationCPS104 Recitation: Assembly Programming
CPS104 Recitation: Assembly Programming Alexandru Duțu 1 Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in
More informationCS429: Computer Organization and Architecture
CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 CS429 Slideset 9: 1 Mechanisms in Procedures
More informationInstructor: Alvin R. Lebeck
X86 Assembly Programming with GNU assembler Lecture 7 Instructor: Alvin R. Lebeck Some Slides based on those from Randy Bryant and Dave O Hallaron Admin Reading: Chapter 3 Note about pointers: You must
More informationCSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II
1 byte{ 1 byte{ CSCI 334: Principles of Programming Languages Lecture 11: Control Structures II Computer Architecture (a really really fast introduction) Instructor: Dan Barowy Memory Instructions main
More informationProcedure-Calling Conventions October 30
Procedure-Calling Conventions October 30 CSC201 Section 002 Fall, 2000 Saving registers Registers are inevitably used by subroutines; changes their! Registers have global scope; calling procedures also
More informationImplementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
Implementing Threads Operating Systems In Depth II 1 Copyright 2018 Thomas W Doeppner All rights reserved The Unix Address Space stack dynamic bss data text Operating Systems In Depth II 2 Copyright 2018
More informationAssembler Programming. Lecture 10
Assembler Programming Lecture 10 Lecture 10 Mixed language programming. C and Basic to MASM Interface. Mixed language programming Combine Basic, C, Pascal with assembler. Call MASM routines from HLL program.
More informationIA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping
Lecture 5 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows
More informationMachine-level Programming (3)
Machine-level Programming (3) Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 2 Procedure Control
More informationMachine Programming 3: Procedures
Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure
More informationLab 10: Introduction to x86 Assembly
CS342 Computer Security Handout # 8 Prof. Lyn Turbak Wednesday, Nov. 07, 2012 Wellesley College Revised Nov. 09, 2012 Lab 10: Introduction to x86 Assembly Revisions: Nov. 9 The sos O3.s file on p. 10 was
More informationLecture 5. Announcements: Today: Finish up functions in MIPS
Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In
More informationThe course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002
15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt
More informationCS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4
CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 4 LAST TIME Enhanced our processor design in several ways Added branching support Allows programs where work is proportional to the input values
More informationCSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1
CSE P 501 Compilers x86 Lite for Compiler Writers Hal Perkins Autumn 2011 10/25/2011 2002-11 Hal Perkins & UW CSE J-1 Agenda Learn/review x86 architecture Core 32-bit part only for now Ignore crufty, backward-compatible
More informationSubprograms, Subroutines, and Functions
Subprograms, Subroutines, and Functions Subprograms are also called subroutines, functions, procedures and methods. A function is just a subprogram that returns a value; say Y = SIN(X). In general, the
More informationDigital Forensics Lecture 3 - Reverse Engineering
Digital Forensics Lecture 3 - Reverse Engineering Low-Level Software Akbar S. Namin Texas Tech University Spring 2017 Reverse Engineering High-Level Software Low-level aspects of software are often the
More informationFunctions in MIPS. Functions in MIPS 1
Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables
More informationGiving credit where credit is due
CSCE 230J Computer Organization Machine-Level Programming III: Procedures Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created
More information16.317: Microprocessor Systems Design I Fall 2015
16.317: Microprocessor Systems Design I Fall 2015 Exam 2 Solution 1. (16 points, 4 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationMechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing
CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 Mechanisms in Procedures Passing Control
More information6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames
6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of Program Counter (Instruc. Pointer) PC/IP is used to fetch an instruction PC/IP contains the address of the next instruction The value in
More informationIntel assembly language using gcc
QOTD Intel assembly language using gcc Assembly language programming is difficult. Make no mistake about that. It is not for wimps and weaklings. - Tanenbaum s 6th, page 519 These notes are a supplement
More informationBuffer Overflow Attack (AskCypert CLaaS)
Buffer Overflow Attack (AskCypert CLaaS) ---------------------- BufferOverflow.c code 1. int main(int arg c, char** argv) 2. { 3. char name[64]; 4. printf( Addr;%p\n, name); 5. strcpy(name, argv[1]); 6.
More informationPractical Malware Analysis
Practical Malware Analysis Ch 4: A Crash Course in x86 Disassembly Revised 1-16-7 Basic Techniques Basic static analysis Looks at malware from the outside Basic dynamic analysis Only shows you how the
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 21: Generating Pentium Code 10 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Simple Code Generation Three-address code makes it
More informationProcedure Call. Procedure Call CS 217. Involves following actions
Procedure Call CS 217 Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return
More informationRegion of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element
Machine Representa/on of Programs: Procedures Instructors: Sanjeev Se(a 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoGom Increasing Addresses Register
More informationIA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address
IA32 Procedures 1 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Stack BoDom Increasing Addresses Register contains lowest stack address address of top element Stack
More informationUniversity of Washington
Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp
More informationx86 Assembly Tutorial COS 318: Fall 2017
x86 Assembly Tutorial COS 318: Fall 2017 Project 1 Schedule Design Review: Monday 9/25 Sign up for 10-min slot from 3:00pm to 7:00pm Complete set up and answer posted questions (Official) Precept: Monday
More informationSRC Assembly Language Programming - III
Computer Architecture Laboratory SRC Assembly Language Programming - III Goals: a) Learn assembly language subroutine methodology b) Write a subroutine in SRC assembler 1. As a group we will develop a
More informationStack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to
Stack Frames EE 357 Unit 8 Stack Frames Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to Every call to a subroutine will create a data structure
More information%r8 %r8d. %r9 %r9d. %r10 %r10d. %r11 %r11d. %r12 %r12d. %r13 %r13d. %r14 %r14d %rbp. %r15 %r15d. Sean Barker
Procedure Call Registers Return %rax %eax %r8 %r8d Arg 5 %rbx %ebx %r9 %r9d Arg 6 Arg 4 %rcx %ecx %r10 %r10d Arg 3 %rdx %edx %r11 %r11d Arg 2 %rsi %esi %r12 %r12d Arg 1 %rdi %edi %r13 %r13d ptr %esp %r14
More informationCourse Administration
Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701
More informationAssembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction
Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction E I P CPU isters Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Topics Assembly Programmer
More informationTowards the Hardware"
CSC 2400: Computer Systems Towards the Hardware Chapter 2 Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32) 1 High-Level Language Make programming
More informationIA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing
15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region
More informationSegmentation in Assembly Language Programming
1 2 Segmentation in General Segmentation in Assembly Language Programming UNIX programs have 3 segments Text segment Executable machine instructions Data segment Initialized data BSS segment (Block Started
More informationStack Discipline Jan. 19, 2018
15-410 An Experience Like No Other Discipline Jan. 19, 2018 Dave Eckhardt Brian Railing Slides originally stolen from 15-213 1 15-410, S 18 Synchronization Registration The wait list will probably be done
More informationWednesday, October 15, 14. Functions
Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:
More informationSecure Programming Lecture 3: Memory Corruption I (Stack Overflows)
Secure Programming Lecture 3: Memory Corruption I (Stack Overflows) David Aspinall, Informatics @ Edinburgh 24th January 2017 Outline Roadmap Memory corruption vulnerabilities Instant Languages and Runtimes
More information