Credits and Disclaimers

Slides:



Advertisements
Similar presentations
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Advertisements

Copyright 2014 – Noah Mendelsohn UM Macro Assembler Functions Noah Mendelsohn Tufts University Web:
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
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.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
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.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
1 Assembly Language: Function Calls Jennifer Rexford.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
Spring 2016Assembly Review Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Reading Condition Codes (Cont.)
Instruction Set Architecture
Credits and Disclaimers
Credits and Disclaimers
C function call conventions and the stack
CSCE 212 Computer Architecture
Credits and Disclaimers
143A: Principles of Operating Systems Lecture 4: Calling conventions
The Stack & Procedures CSE 351 Spring 2017
Machine-Level Programming III: Procedures
Recitation: Attack Lab
Procedures & The Stack Announcements Lab 1 graded HW 2 out
Machine-Level Programming 4 Procedures
Assembly Programming IV CSE 351 Spring 2017
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Procedures & The Stack I CSE 351 Autumn 2016
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Procedures & The Stack II CSE 351 Winter 2017
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Machine-Level Programming III: Procedures Sept 18, 2001
The Stack & Procedures CSE 351 Winter 2018
Machine Level Representation of Programs (IV)
The Stack & Procedures CSE 410 Winter 2017
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Get To Know Your Compiler
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
CS201- Lecture 8 IA32 Flow Control
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Carnegie Mellon Ithaca College
Credits and Disclaimers
Credits and Disclaimers
Credits and Disclaimers
Credits and Disclaimers
Visual Studio x64 C Compiler function entrance code
Computer Architecture and System Programming Laboratory
Presentation transcript:

Credits and Disclaimers The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3nd Edition by Bryant and O'Hallaron x86 Assembly/GAS Syntax on WikiBooks (http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax) Using Assembly Language in Linux by Phillip ?? (http://asm.sourceforge.net/articles/linasm.html) The C code was compiled to assembly with gcc version 4.8.3 on CentOS 7. Unless noted otherwise, the assembly code was generated using the following command line: gcc –S –m64 -fno-asynchronous-unwind-tables –mno-red-zone –O0 file.c AT&T assembly syntax is used, rather than Intel syntax, since that is what the gcc tools use.

Stack Operations: push The push instruction: - decrements the stack pointer rsp by 8, making room on the stack - copies the value of its operand to the top of the stack . . . main: pushq %rbp ?? rsp ?? rsp value in rbp current stack not in the stack before after

Stack Operations: pop The pop instruction: - copies the item at the top of the stack into its operand - increments the stack pointer rsp by 8, removing the old top item . . . main: popq %rbp ?? rsp value of rbp ?? rsp before after

C Code static int max(int A, int B); int main() { // caller int x = 7; int y = 12; int retval = max(x, y); return 0; } int max(int A, int B) { //called procedure int Bigger = A; if ( A < B ) Bigger = B; return Bigger;

Logical Steps in a Procedure Call Calling a procedure (function) in C would seem to involve four steps: 1 set up the parameters that will be passed into the called procedure 2 cause execution to jump to the first instruction within the procedure 3 when the procedure is done, cause execution to jump back to the next instruction in the caller 4 access the value returned by the called procedure, if any // caller int x, y; . . . int retval = max(x, y); 1 // called int max(int A, int B) { int Bigger; . . . return Bigger; } 3 4 2

caller's frame before parameters are set Preparing Parameters The caller's stack frame (prior to the call) will logically reflect the current state of the caller's execution. // caller int x, y; . . . int retval = max(x, y); rbp old frame ptr rbp - 4 x rbp - 8 y rbp - 12 . . . rsp end caller frame caller's frame before parameters are set

caller's frame after parameters are set Preparing Parameters The first 6 parameters are passed in registers, parameters 7 and up are passed on the stack. While there’s are only 2 parameters in this example, note order of (unneeded) parameters on the stack: rbp old frame ptr rbp - 4 x: 7 rbp - 8 y: 12 rbp - 12 passed value: argument n . . . rsp passed value: argument 7 // caller int x, y; . . . int retval = max(x, y); Registers rdi x rsi y rdx ?? rcx r8 r9 caller's frame after parameters are set

Assembly View Registers . . . main: # usual rbp/rsp contortions make room on # stack for locals and passed values subq $16, %rsp movl $7, -4(%rbp) movl $12, -8(%rbp) movl -8(%rbp), %edx movl -4(%rbp), %eax movl %edx, %esi movl %eax, %edi rdi x rsi y Registers

Jumping In We jump into the called procedure by using the assembly instruction call: main: . . . # this follows the code # shown previously movl %edx, %esi movl %eax, %edi call max movl %eax, -12(%rbp)) . . . rbp local rbp - 4 stuff rbp - 8 for rbp - 12 caller . . . ?? rsp + 8 rsp return-to address The call instruction has two effects: 1 push the address of the next instruction in the caller onto the stack 2 put the address represented by the symbol max into the PC 1 the Stack

Called Procedure Overview max: pushq %rbp # stack setup movq %rsp, %rbp subq $24, %rsp movl %edi, -20(%rbp) # function body movl %esi, -24(%rbp) movl -20(%rbp), %eax movl %eax, -4(%rbp) cmpl -24(%rbp), %eax jge .L4 movl -24(%rbp), %eax .L4: movl -4(%rbp), %eax # set return value leave # jump back to caller ret

Local Stack Setup Registers the Stack max: pushq %rbp # save old frame ptr movq %rsp, %rbp # set rbp to this frame subq $24, %rsp # make room for locals movl %edi, -20(%rbp) # move parameters movl %esi, -24(%rbp) . . . fame for main() local stuff for caller rbp + 8 return-to address rbp old frame ptr rbp - 4 local: Bigger ... rbp - 20 x rbp - 24 y Registers fame for max() rdi x rsi y the Stack

The Computations the Stack max: . . . movl -20(%rbp), %eax movl %eax, -4(%rbp) cmpl -24(%rbp), %eax jge .L4 movl -24(%rbp), %eax .L4: rbp + 8 return-to address rbp old frame ptr rbp - 4 local: Bigger rbp - 20 x rbp - 24 y the Stack

Returning a Value We use the eax (or %rax) register to hold the return value: max: . . . movl -4(%rbp), %eax # set return value to Bigger

Preparing to Leave The leave instruction resets the stack and frame pointers prior to returning: max: . . . leave rbp must be reset point to the beginning of the stack frame of the caller rbp local rbp - 4 stuff rbp - 8 for rbp - 12 caller . . . rsp return-to address rsp must be reset point to where the top of the stack was when the call instruction was made. the Stack

Preparing to Leave after before older frame ptr x y . . . ?? rbp + 8 return-to address rbp old frame ptr rbp - 4 local: Bigger ... rsp rbp older frame ptr rbp - 4 x rbp - 8 y . . . ?? rsp return-to address old frame ptr 2 after 1 movl %rbp, %rsp # 1 popl %rbp # 2 before

Jumping Back We jump back by using the assembly instruction ret: max: . . . ret popq %rip rbp older frame ptr rbp - 4 x rbp - 8 y . . . ?? rsp return-to address rbp older frame ptr rbp - 4 x rbp - 8 y . . . ?? rsp return-to address before after

Stack before execution of the call instruction Stack Summary . . . rbp old frame ptr rbp - 4 x rbp - 8 y rbp - 12 ?? rbp – 16 rbp – 20 ... rsp Stack before execution of the call instruction

Stack Summary stack after execution of the call instruction . . . rbp old frame ptr rbp - 4 x rbp - 8 y rbp - 12 ?? rbp – 16 rbp – 20 rbp – 24 rbp – 28 rsp return-to address . . . older frame ptr x y ?? return-to address rbp old frame pointer rbp - 4 local: Bigger ... rbp - 20 rbp - 24 stack after execution of the call instruction stack after execution of stack setup code in max()

stack after execution of leave instruction in max() Stack Summary . . . rbp older frame ptr x y ?? rsp return-to address stack after execution of leave instruction in max()

Stack Summary stack after execution of ret instruction in max(), . . . rbp older frame ptr x y ?? rsp stack after execution of ret instruction in max(), execution now proceeds in main()