Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Similar documents
Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Assembly Language: Function Calls

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Buffer Overflow Attack (AskCypert CLaaS)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CS429: Computer Organization and Architecture

Machine Program: Procedure. Zhaoguo Wang

MIPS Procedure Calls - Review

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

Machine-level Programs Procedure

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

18-600: Recitation #4 Exploits

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Subprograms, Subroutines, and Functions

LIFO : Last In First Out

CSC 2400: Computer Systems. Using the Stack for Function Calls

CSC 2400: Computer Systems. Using the Stack for Function Calls

Functions in MIPS. Functions in MIPS 1

Machine Programming 3: Procedures

CSC 8400: Computer Systems. Using the Stack for Function Calls

Lecture V Toy Hardware and Operating System

Machine-Level Programming III: Procedures

Instruction Set Architectures (4)

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

EE 3170 Microcontroller Applications

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Computer Organization: A Programmer's Perspective

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

SRC Assembly Language Programming - III

The Stack & Procedures

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Course Administration

Subroutine. Chapter 8

18-600: Recitation #4 Exploits (Attack Lab)

Recitation: Attack Lab

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Computer Architecture and System Programming Laboratory. TA Session 3

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Assembly Language: Function Calls

Run-time Environment

Assembly Language: Function Calls" Goals of this Lecture"

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Lecture 5. Announcements: Today: Finish up functions in MIPS

Machine-Level Programming (2)

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Assembly Language: Function Calls" Goals of this Lecture"

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

Using SRAM in AVR assembler language

EE 361 University of Hawaii Fall

Today. Putting it all together

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

The Stack & Procedures

CSE 351 Midterm - Winter 2015 Solutions

Computer Systems C S Cynthia Lee

Chapter 10 Memory Model for Program Execution. Problem

Lecture 4 CIS 341: COMPILERS

Computer Systems Lecture 9

Compiling Code, Procedures and Stacks

University of Washington

Procedure Call. Procedure Call CS 217. Involves following actions

Computer Architecture and System Software Lecture 06: Assembly Language Programming

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Stacks and Subroutines

Topic 7: Activation Records

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Storage in Programs. largest. address. address

18-600: Recitation #3

CSE 351 Midterm - Winter 2015

Shift and Rotate Instructions

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

System Software Assignment 1 Runtime Support for Procedures

G Programming Languages - Fall 2012

Stacks and Function Calls

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

CSE Lecture In Class Example Handout

real-time kernel documentation

Implementing Subroutines. Outline [1]

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Systems I. Machine-Level Programming V: Procedures

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Procedures and Stacks

CS 3330 Exam 3 Fall 2017 Computing ID:

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

Procedures and the Call Stack

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

Transcription:

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 removed from the top of the stack This gives a First-In-Last-Out logic (aka FILO) Same concept is also called Last-In-First-Out (LIFO) 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 3 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 4 Examples of Stacks Stack Operation: Push Page-visited "back button" history in a web browser Undo sequence in a text editor Deck of cards in Windows Solitaire A value is added to the stack It is placed on the top location Rest of the items are "covered" 5 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 5 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 6 1

Stack Operation: Pop Implementing in Memory Removes an item from the stack Last item added is removed 2 nd item becomes the top On a processor, the stack stores integers size of the integer the bit-size of the system 64-bit system 64-bit integer these are called words A fixed location pointer (S0) defines the bottom of the stack A stack pointer (SP) gives the location of the top of the stack 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 7 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 8 Approaches Size of the Stack Growing upwards Bottom Pointer (S0) points to the lowest address in the stack buffer stack grows towards higher addresses Grow downwards Bottom Pointer (S0) points to the highest address in the stack buffer stack grows towards lower addresses As an abstract data structure stacks are assumed to be infinitely deep so, an arbitrary amount of data can stored However stacks are implemented using memory buffers which are finite in size If the amount of data exceeds the allocated space, a stack overflow error occurs 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 9 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 10 Subroutine Call Basics Subroutine Call Basics Subroutines are important part of virtually all computer languages Essential to designing any assembly program Organizing Your Program 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 12 2

Why Use Subroutines Subroutine Call 1. Allow commonly used functions to be written once and used whenever needed 2. Provide abstraction, making it easier for multiple programmers to collaborate on a program Processors are designed to support subroutines and, as a result, contain special instructions and hardware The stack used to save the current state of the system also used to pass data between subroutines but, we will cover these later 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 13 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 14 When you call a subroutine Nesting Calls 1. Processor pushes the program counter (PC) an address on the stack 2. PC is changed to the address of the subroutine 3. Subroutine executes and ends with a "return" instruction 4. Processor then pops and restores the original PC 5. Execution continues after the initial call Subroutines can call other subroutines For example: Main program calls Subroutine A Subroutine A calls Subroutine B Each time another subroutine is called a new PC is put on the stack additional calls will put their PCs on the stack top of the stack is always to the calling routine 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 15 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 16 Passing Passing Through Registers Useful subroutines need you to be able to pass data into them and be able to read data from it One of the easiest ways to accomplish this is by using the processors registers Incredibly efficient! However processor might not have enough registers for each parameter Other forms of passing data have to be used 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 17 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 18 3

Instruction: Call x64 Subroutines The Call Instruction is used to transfer control to a subroutine Other processors call it different names such as JSR (Jump Subroutine) The stack is used to save the current PC Organizing Your Programs with Intel 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 20 Instruction: Call Instruction: Return Usually a label (which is an address) CALL address The Return Instruction is used mark the end of subroutine When the instruction is executed the old program counter is read from the system stack the current program counter is updated restoring execution after the initial call 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 21 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 22 Instruction: Return Instruction: Return Do not forget this! If you do execution will simply continue, in memory, until a return instruction is encountered often is can run past the end of your program and run data! RET No arguments! 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 23 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 24 4

Subroutine Example _start: mov $4, %rax mov $12, %rbx call AddIt add $1, %rbx... AddIt: add %rax, %rbx ret Saving Registers & Lost Data Avoiding horrible side-effects 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 25 Saving Registers & Lost Data Two Solutions Each subroutine will use the registers as it needs So, when a sub is called, it may modify the caller's registers Some processors have few registers so its very likely This can lead to hard-to-fix bugs if caution is not used e.g. loop counter gets changed 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 27 saves values caller saves all their registers to memory before making the subroutine call after, it restores the values before continuing not recursion friendly it pushes all of them! Subroutine saves the values push registers (it will change) onto the stack before it returns, it pops (and restores) the old values off the stack 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 28 Saving Registers How nice! :-) DoSomething: push %rax push %rbx push %rcx... pop %rcx pop %rbx pop %rax ret Save registers Your code Restore them. Note the reverse order The Jenga of data! Stack Frames 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 29 5

Subroutine Challenges Subroutine Challenges Need to pass data as input from the calling program as outputted results must be done for any number of inputs even if it exceeds the available number of registers Need local variables space needs to be allocated cannot overwrite caller data Subroutines may be called from anywhere used in different locations within a program often compiled separately outside the caller generally impossible to determine which registers may be safely used by a subroutine 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 31 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 32 Stack Frames Stack Frames So, many compilers use Stack Frames Rather than pass all parameters through registers, the system stack is used The stack is also used to store local variables This approach is used by most compilers given most processors are very limited in their registers Easy concept, but very difficult to program (at first) 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 33 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 34 Stack Frame Contents Nesting is Possible Contains all the information needed by subroutine Includes: calling program's return address input parameters to the subroutine the subroutine s local variables space to backup the caller's register file Because the stack is LIFO (last in first out), subroutines can call subroutines This approach allows recursion and all the features found in high-level programming languages Stack 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 35 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 36 6

Nesting is Possible How it Starts Up For example, subroutinef() calls g() which then calls h() Since the stack is used, the stack frames follow the LIFO behavior Stack f() stack frame g() stack frame h() stack frame pushes the subroutine's arguments onto the stack caller calls the subroutine Subroutine then uses the stack to backup registers and "carve" out local variables 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 37 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 38 How it Finishes Calling Convention Subroutine restores the original register values removes the local variables from the stack calls the processor "return" instruction, then removes its arguments from the stack handles the result which can be passed either in a register or on the stack Different programming systems may arrange the stack frame differently A calling convention is used by a programming system (e.g. a language) to define how data will be passed In particular, it defines the structure of the stack frame and how data is returned 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 39 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 40 Calling convention Stack Frame Size Varies For example: Is the first argument pushed first? Or last? Is the result in a register? Or the stack? If all subroutines follow the same format caller can use the same format for each subroutines can also be created separately and linked together The number of input arguments and local variables varies from subroutine to subroutine The arrangement of data within the stack frame also varies from compiler to compiler Stack frames is a concept and it is used with various differences 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 41 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 42 7

Stack Frames on the x64 Stack Frames on the x64 Pretty much how its done on all processors Stack frames on the x64 are accomplished pretty much the same way as other processors How it is done in real life is not simple and is one of the hardest concepts to understand 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 44 Stack Frames on the x64 Stack Frames on the x64 On the x64, we will use the Base Pointer () to access elements in the stack frame This is a pointer register We will use it as an "anchor" in our stack frame As we build the stack frame, we will set to fixed address in the stack frame Our parameters and local variables will be accessed by looking at memory relative to the So, we will look x many bytes above and below the "anchor" 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 45 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 46 Stack on the x64 Stack on the x64 The stack base on the x64 is stored in high memory and grows downwards towards 0 So, as the size of the stack increases, the stack pointer () will decrease in value On a 64-bit system, it will decrease by increments of 8 bytes So each of our values (local variables and parameters) will be offsets of 8 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 47 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 48 8

Structure of a Stack Frame 1. Push In this example, the stack is growing downward The stack pointer () is always on the bottom of the stack frame starts by pushing each of the parameters onto the stack The order in which the parameters are pushed is defined in the calling convention 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 49 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 50 2. Call the subroutine 3. Save the Old Base Pointer The caller then uses the Call Instruction to pass control to the subroutine The processor pushes the PC (program counter) on the stack Subroutine now runs We need to set the Base Pointer () So, the old version needs to be saved (so it can be restored) Old value is pushed 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 51 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 52 4. Set the Base Pointer 5. Add Local Variables Then, it sets the Base Pointer () to the current stack pointer () address is an "anchor" that we will use Subroutine now creates local variables on the stack Their initial values can be simply pushed Note: many compilers use a trick here that we will discuss later Local variables 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 53 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 54 9

6. Backup Registers The Completed Stack Frame Finally, the subroutine saves all the registers (that will change) on the stack It will restore them at the end Local variables Saved Registers The Stack Frame contains all the information a subroutine needs We just need to be able to access the data programmatically Local variables Saved Registers 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 55 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 56 & Local Variables Size of Stack Values 64 bit Now, is set to an address between the parameters and the local variables We can use offsets from to access each Parameter 3 Parameter 2 Parameter 1 Local variable 1 Local variable 2 Local variable 3 Local variable 4 x64 stack grows downward, so Variables will have a negative offset will have a positive offset Parameter 3 Parameter 2 Parameter 1 Local variable 1 Local variable 2 Local variable 3 Local variable 4 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 57 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 58 Size of Stack Values 32 bit Size of Stack Values 64 bit +16 Parameter 3 +32 Parameter 3 On a 32-bit system, each word is 4 bytes So, each value on the stack is 4 bytes Offsets increase and decrease by 4 +12 +8-4 -8-12 -16 Parameter 2 Parameter 1 Local variable 1 Local variable 2 Local variable 3 Local variable 4 On a 64-bit system, each word is 8 bytes So, each value on the stack is 8 bytes Offsets increase and decrease by 8 +24 +16-8 -16-24 -32 Parameter 2 Parameter 1 Local variable 1 Local variable 2 Local variable 3 Local variable 4 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 59 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 60 10

Example Subroutine: Setup Example push $42 push %rax call Subroutine Call subroutine Push parameters push %rbp mov %rsp, %rbp push $1 push $2 push %rax push %rbx Local variables Backup Backup registers Set new 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 61 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 62 Subroutine Data Example Subroutine: Ending Example Offset from pointer mov 16(%rbp), %rax add 24(%rbp), %rax mov %rax, -8(%rbp) Parameter 1 Parameter 2 Local variable 1 pop %rbx pop %rax mov %rbp, %rsp pop %rbp ret Restore registers Restore Set to Effectively deletes all local variables Only return when the stack is restored 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 63 11/20/2018 Sacramento State - Cook - CSc 35 - Fall 2018 64 11