The Stack.

Slides:



Advertisements
Similar presentations
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Advertisements

1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Instruction Set Architecture Classification According to the type of internal storage in a processor the basic types are Stack Accumulator General Purpose.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Multiple data transfer instructions ARM also supports multiple loads and stores: ldm/ldmia/ldmfd: load multiple registers starting from [base register],
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Intro to Computer Architecture
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 8 :: Subroutines and Control Abstraction
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
CS41B MACHINE David Kauchak CS 52 – Fall Admin  Midterm Thursday  Review question sessions tonight and Wednesday  Assignment 3?  Assignment.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
The Stack. ARMSim memory space: 0x Unused 0x x11400 Stack 0x x09400 Heap 0x?????-0x01400 Data 0x x????? Text 0x x01000.
Multiple data transfer instructions ARM also supports multiple loads and stores: When the data to be copied to the stack is known to be a multiple of 4.
Writing Functions in Assembly
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
ECE 3430 – Intro to Microcomputer Systems
Functions and the Stack
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
Subroutines and the Stack
Writing Functions in Assembly
April 2006 Saeid Nooshabadi
CS41B recursion David Kauchak CS 52 – Fall 2015.
In this lecture Global variables Local variables System stack
The Stack.
Chapter 10 The Stack.
Chapter 9 :: Subroutines and Control Abstraction
Activation Records and Function Calls
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Procedures.
Topic 3-a Calling Convention 1/10/2019.
10/4: Lecture Topics Overflow and underflow Logical operations
ECE 3430 – Intro to Microcomputer Systems
Program and memory layout
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Program and memory layout
Subroutines and the Stack
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Runtime Stack Activation record for hanoi;
Presentation transcript:

The Stack

The Stack ARMSim memory space: 0x11400 + Unused 0x09400-0x11400 Stack Heap 0x?????-0x01400 Data 0x01000-0x????? Text 0x00000-0x01000 Reserved

Stack possibilites How to use the space in stack? Grow up Keep track of top item in stack (Full Ascending) Keep track of first empty slot (Empty Ascending)

Stack possibilites How to use the space in stack? Grow down Keep track of top item in stack (Full Descending) Keep track of first empty slot (Empty Descending)

Stack possibilites ARM Convention : Full Descending R13 Holds "top" of stack

Push Recipe Subtract 4 from sp … 81818181 0x113F4 0x113F8 0x113FC ????????? sp 0x11400

Push Recipe Subtract 4 from sp … 81818181 0x113F4 0x113F8 0x113FC ????????? sp 0x113FC

Push Recipe Subtract 4 from sp STR data … 81818181 0x113F4 0x113F8 0x113FC AA 0x11400 ????????? sp 0x113FC

Push Recipe Subtract 4 from sp … 81818181 0x113F4 0x113F8 0x113FC AA ????????? sp 0x113F8

Push Recipe Subtract 4 from sp STR data … 81818181 0x113F4 0x113F8 BB 0x113FC AA 0x11400 ????????? sp 0x113F8

Pop Recipe LDR data … 81818181 0x113F4 0x113F8 BB 0x113FC AA 0x11400 ????????? sp 0x113F8

Pop Recipe LDR data Add 4 to sp … 81818181 0x113F4 0x113F8 BB 0x113FC ????????? sp 0x113FC

Pop Recipe LDR data … 81818181 0x113F4 0x113F8 BB 0x113FC AA 0x11400 ????????? sp 0x113FC

Pop Recipe LDR data Add 4 to sp … 81818181 0x113F4 0x113F8 BB 0x113FC ????????? sp 0x11400

Push / Pop Always tear down stack in reverse order Push A Push B Pop B Time

Machine Push/Pop Common need : machine instructions Store Multiple to Full Descending use sp and update it (!) store r1 and r2

Machine Push/Pop Common need : machine instructions Load Multiple from Full Descending use sp and update it (!) load r2 then r1 (reverse order)

Pseudo Instructions Easier pseudinstructions: PUSH {register list} POP {register list}

Function Calls

Assembly Function Assembly Functions are branches

Assembly Function Assembly Functions are branches Need to know where to return to when done!

Assembly Function BL label Branch and Link Branch (change PC) But store Store next instruction address in R14 (lr = link register)

Assembly Function BL label Branch and Link Branch (change PC) But store Store next instruction address in R14 (lr = link register)

Assembly Function Return Copy lr back into pc

Assembly Function Return Copy lr back into pc

UDiv Unsigned division function Parameters in r0, r1 Results in r0, r1

UDiv Calling from main Parameters in r0, r1 Results in r0, r1

Using Functions GetTime program Get system time Do lots of divisions to get Hour, Min, Sec

Calling Conventions

Calling Convetions Calling Convention Agreement for how subroutines work How we pass info What registers we can use … Differs between platforms, compilers, etc…

ARM Conventions ARM Register Conventions r0-r3 r4-r9 r10-r15 Register Range Use r0-r3 -Passing parameters & results -Temporary values r4-r9 -Storing information r10-r15 Special purpose

ARM Conventions ARM Register Conventions r0-r3 r4-r9 r10-r15 Register Range Use Caller Function r0-r3 -Passing parameters & results -Temporary values These may get destroyed. Store/restore values if you care about them!!! Change these all you want… r4-r9 -Storing information No worries… If you want to use these you MUST store/restore data that was there!!! r10-r15 Special purpose Only use for intended purpose

ABS Absolute Value Does what it wants with r0-r3 Makes use of r4 and r5 – must save

ABS Main that calls abs must store any r0-r3 used across function call r4+ are safe

Smarter ABS If function ONLY uses r0-r3 to do work, nothing to save:

Smarter ABS If main only uses r0-r3 between function calls, nothing to save

Nested Calls

Nested Calls Real functions often call other functions:

Nested Calls Multiple "return addresses" needed Proc_A needs address D Proc_B needs address B

Nested Calls Multiple "return addresses" needed Proc_A needs address D Proc_B needs address B Store return addresses on stack

Calling Convention Update If function call other functions: Save/restore your return address (lr) to stack with registers r4+

Process Review Subroutine Call (done by the caller): Push any of  r0-r3 that are in use Put argument values into r0-r3 Call the subroutine using bl

Process Review Subroutine Prolog (done to start subroutine): If we want to use r4+ push them If we will call other functions, push lr also Get parameters from r0-r3

Process Review Subroutine Body  Use r0-r3 and any from r4+ you saved

Process Review Subroutine Epilog (done at end of sub): Put returned values in r0-r3 Pop from the stack anything you saved (r4+, lr) Return by moving lr to pc

Process Review Regaining Control (done by the caller): Get results from r0-r3 Pop any old r0-r3 values you stored before call