SlideShare a Scribd company logo
1 of 23
Download to read offline
The Stack
 Frame

An Ivaylo Marinkov presentation
        ivaylo@tsarstva.bg
Call Stack
A stack data structure storing
information of a computer program's
active subroutines.
Whilst important for the software's
proper functioning, its details are
usually hidden and usage is automated
in high-level programming languages.
Call Stack Purpose


Keeping record of the point to which
each active subroutine should return
control when finished.
Active Subroutine
A routine that has been called but
has not finished execution yet.
Afterwords, control should be
returned to the point where the call
has been made.
Routines may be nested to any level
and recursion is possibe – hence the
stack structure.
For example, the DrawSquare
subroutine calls the DrawLine
subroutine from four different
places. DrawLine must know where
to return once completed.
This is accomplished by pushing the
address following the call
instruction – the return address –
onto the stack with each call.
Call Stack Inner Workings
 The caller pushes the return address onto
 the stack (winding).
 The called subroutine, when it finishes,
 pops the return address off the stack and
 transfers control to it (unwinding).
 If a called subroutine calls on yet another
 subroutine, it will push another address
 onto the stack, and so on, with the
 information stacking up and unstacking as
 the program dictates.
Should pushing consume all the
space allocated for the call stack, an
error called stack overflow will
occur.
There is usually a single call stack
associated with each process thread.
However, the program may create
additional call stacks for tasks such as
signal-hadling or cooperative
multitasking.
Additional Call Stack Functions
Local data storage – keeping local-scope
variable values.
Parameter passing – storage for values
passed by calling code.
Evalution stack – in some cases
operands for logical and
arithmetic operations may be
stored in the call stack.
Current instance pointer –
for this pointer in
object-oriented languages.
Structure
       A call stack is
       composed of
       stack frames,
       machine and
       application banary
       interface-
       dependant data
       structures
       containing
       subroutine state
       information.
The Stack Frame

Generally speaking, a
procedure's stack frame contains
all the information necessary to
save and restore the state of the
procedure.
Strictly speaking, it is only necessary
for the calling program and the
called procedure to agree on the
structure of the stack frame for each
procedure call.
However, the specification of a
calling convention facilitates the use
of procedure libraries by defining
the structure of the stack frame
uniformly for all procedure calls.
The frame pointer is
                                 stored in register $30,
                                 also known as $fp. A
                                 stack frame consists of
                                 the memory on the
                                 stack between the
                                 frame pointer and the
                                 stack pointer.

                                 Three steps are
                                 necessary to call a
Calling convention used in the
MIPS architecture stack frame    procedure.
1. Pass the arguments. The first
four arguments are passed in
registers $a0-$a3. The remaining
arguments are pushed onto the
stack.

2. Save the caller-saved registers.
This includes registers $t0-$t9, if
they contain live values at the call
site.

                                       The endless MIPS
3. Execute a jal instruction.          cycle
1. Pass the arguments. The first
four arguments are passed in
registers $a0-$a3. The remaining
arguments are pushed onto the
stack.

2. Save the caller-saved registers.
This includes registers $t0-$t9, if
they contain live values at the call
site.

                                       The endless MIPS
3. Execute a jal instruction.          cycle
Within the called routine, the following steps
are necessary:
1. Establish the stack frame by subtracting
the frame size from the stack pointer.
2. Save the callee-saved registers in the
frame. Register $fp is always saved. Register
$ra and registers $a0-$a3 need to be saved if
they are in use and the routine itself makes
calls. Any of the registers $s0- $s7 that are
used by the callee need to be saved.
3. Establish the frame pointer by adding the
stack frame size to the address in $sp.
To return from a call, a function places the
returned value into $v0 and executes the
following steps:
1. Restore any callee-saved registers that
were saved upon entry.
2. Pop the stack frame by subtracting the
frame size from $sp.
3. Return by jumping to the address in
register $ra.
Debugging
The purpose of a debugger such
as GDB (gnu.org/software/gdb) is
to allow you to see what is going
on “inside” another program
while it executes--or what
another program was doing at
the moment it crashed.
In Practice
        An illustration to
        viewing the call
        stack using GDB.

        1. Compile your
        program with the
        -g option, like
        cc -g -o p1 p1.c
2. Navigate to your program's directory
and run GDB: gdb your_program




If all went fine, you will land on a
command prompt.
3. Then install some breakpoints using the
break command:
break function_name




4. Now run. The program will proceed
until the first breakpoint.
You can select a frame using the frame n
command and view frame information
using the info frame n command.




Some of the details this command
displays are the addresses of the frame,
the next frame down (called by this
frame) and the next frame up (caller of
this frame).
View the call stack using the backtrace
command. A backtrace is a summary of
how your program got where it is. It
shows one line per frame, for many
frames, starting with the one currently
in execution (frame zero), followed by its
caller (frame one), and on up the stack.
Resources
https://en.wikipedia.org/wiki/Call_stack
http://www.cs.uaf.edu/~cs301/notes/Chapter9/node11.html
http://chortle.ccsu.edu/assemblytutorial/Chapter-26/ass26_4.html
http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/
http://www.freebsd.org/doc/en/books/developers-handbook/debugging.html
https://www.gnu.org/software/gdb/
Background images from various sources.


   This presentation was created using only free and open
    source software including the Ubuntu Linux operating
     system, LibreOffice, Mozilla Firefox, Geany, GDB and
                        KolourPaint.

More Related Content

What's hot

Some questions and answers on lte radio interface
Some questions and answers on lte radio interfaceSome questions and answers on lte radio interface
Some questions and answers on lte radio interfaceThananan numatti
 
Lte protocol-stack-mac-rlc-pdcp
Lte protocol-stack-mac-rlc-pdcpLte protocol-stack-mac-rlc-pdcp
Lte protocol-stack-mac-rlc-pdcpPrashant Sengar
 
Radio Network Design & Roll Out
Radio Network Design & Roll OutRadio Network Design & Roll Out
Radio Network Design & Roll OutRathnaKumar47
 
Day 1 LTE Technology Overview
Day 1 LTE Technology OverviewDay 1 LTE Technology Overview
Day 1 LTE Technology Overviewmahesh savita
 
IP Multimedia Subsystem architecture overview
IP Multimedia Subsystem architecture overviewIP Multimedia Subsystem architecture overview
IP Multimedia Subsystem architecture overviewNarasimham Settipalli
 
Ontology Mapping
Ontology MappingOntology Mapping
Ontology Mappingbutest
 
모바일 SNG 비동기 네트워크 통신 사례
모바일 SNG 비동기 네트워크 통신 사례모바일 SNG 비동기 네트워크 통신 사례
모바일 SNG 비동기 네트워크 통신 사례성수 이
 
VoLTE KPI Performance Explained
VoLTE KPI Performance ExplainedVoLTE KPI Performance Explained
VoLTE KPI Performance ExplainedVikas Shokeen
 
Lte principles overview
Lte principles  overviewLte principles  overview
Lte principles overviewNdukwe Amandi
 
SecureOTP: Total One-Time-Password Solution
SecureOTP: Total One-Time-Password SolutionSecureOTP: Total One-Time-Password Solution
SecureOTP: Total One-Time-Password SolutionRafidah Ariffin
 
Zephyr-Overview-20230124.pdf
Zephyr-Overview-20230124.pdfZephyr-Overview-20230124.pdf
Zephyr-Overview-20230124.pdfibramax
 
5 g core network and the cloud - A standards perspective
5 g core network and the cloud - A standards perspective5 g core network and the cloud - A standards perspective
5 g core network and the cloud - A standards perspectiveSridhar Bhaskaran
 
2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network
2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network
2G / 3G / 4G / IMS / 5G Overview with Focus on Core NetworkHamidreza Bolhasani
 
radio resource_management_v1.1_chema
radio resource_management_v1.1_chemaradio resource_management_v1.1_chema
radio resource_management_v1.1_chemamohammad ali amini
 

What's hot (20)

Some questions and answers on lte radio interface
Some questions and answers on lte radio interfaceSome questions and answers on lte radio interface
Some questions and answers on lte radio interface
 
Lte protocol-stack-mac-rlc-pdcp
Lte protocol-stack-mac-rlc-pdcpLte protocol-stack-mac-rlc-pdcp
Lte protocol-stack-mac-rlc-pdcp
 
Radio Network Design & Roll Out
Radio Network Design & Roll OutRadio Network Design & Roll Out
Radio Network Design & Roll Out
 
Day 1 LTE Technology Overview
Day 1 LTE Technology OverviewDay 1 LTE Technology Overview
Day 1 LTE Technology Overview
 
Lte Tutorial
Lte TutorialLte Tutorial
Lte Tutorial
 
Ims Services
Ims ServicesIms Services
Ims Services
 
IP Multimedia Subsystem architecture overview
IP Multimedia Subsystem architecture overviewIP Multimedia Subsystem architecture overview
IP Multimedia Subsystem architecture overview
 
Ontology Mapping
Ontology MappingOntology Mapping
Ontology Mapping
 
모바일 SNG 비동기 네트워크 통신 사례
모바일 SNG 비동기 네트워크 통신 사례모바일 SNG 비동기 네트워크 통신 사례
모바일 SNG 비동기 네트워크 통신 사례
 
VoLTE KPI Performance Explained
VoLTE KPI Performance ExplainedVoLTE KPI Performance Explained
VoLTE KPI Performance Explained
 
Lte principles overview
Lte principles  overviewLte principles  overview
Lte principles overview
 
SecureOTP: Total One-Time-Password Solution
SecureOTP: Total One-Time-Password SolutionSecureOTP: Total One-Time-Password Solution
SecureOTP: Total One-Time-Password Solution
 
Zephyr-Overview-20230124.pdf
Zephyr-Overview-20230124.pdfZephyr-Overview-20230124.pdf
Zephyr-Overview-20230124.pdf
 
Ip ran v1.1
Ip ran v1.1Ip ran v1.1
Ip ran v1.1
 
E nodeb
E nodebE nodeb
E nodeb
 
TEMS PARAMETER (3G)
TEMS PARAMETER (3G)TEMS PARAMETER (3G)
TEMS PARAMETER (3G)
 
Quick 3GPP RRC ASN.1
Quick 3GPP RRC ASN.1Quick 3GPP RRC ASN.1
Quick 3GPP RRC ASN.1
 
5 g core network and the cloud - A standards perspective
5 g core network and the cloud - A standards perspective5 g core network and the cloud - A standards perspective
5 g core network and the cloud - A standards perspective
 
2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network
2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network
2G / 3G / 4G / IMS / 5G Overview with Focus on Core Network
 
radio resource_management_v1.1_chema
radio resource_management_v1.1_chemaradio resource_management_v1.1_chema
radio resource_management_v1.1_chema
 

Similar to The Stack Frame

Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfHimanshu883663
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorialhughpearse
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementRahul Jamwal
 
002 hbase clientapi
002 hbase clientapi002 hbase clientapi
002 hbase clientapiScott Miao
 
Introduction to c
Introduction to cIntroduction to c
Introduction to camol_chavan
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
CD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docxCD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docxKANDE ARCHANA
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docxssuser1c8ca21
 
Chap6 procedures & macros
Chap6 procedures & macrosChap6 procedures & macros
Chap6 procedures & macrosHarshitParkar6677
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Javier Tallón
 

Similar to The Stack Frame (20)

Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdf
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorial
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Chapter 5 notes
Chapter 5 notesChapter 5 notes
Chapter 5 notes
 
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
Co&al lecture-07
Co&al lecture-07Co&al lecture-07
Co&al lecture-07
 
002 hbase clientapi
002 hbase clientapi002 hbase clientapi
002 hbase clientapi
 
Chapter 6 notes
Chapter 6 notesChapter 6 notes
Chapter 6 notes
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Chapter 5 notes new
Chapter 5 notes newChapter 5 notes new
Chapter 5 notes new
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
CD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docxCD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docx
 
Run time storage
Run time storageRun time storage
Run time storage
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
Chap6 procedures & macros
Chap6 procedures & macrosChap6 procedures & macros
Chap6 procedures & macros
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Compiler 2011-8-re1
Compiler 2011-8-re1Compiler 2011-8-re1
Compiler 2011-8-re1
 
Compiler 2011-8-re1
Compiler 2011-8-re1Compiler 2011-8-re1
Compiler 2011-8-re1
 
Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?Mitigating overflows using defense in-depth. What can your compiler do for you?
Mitigating overflows using defense in-depth. What can your compiler do for you?
 

Recently uploaded

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 

Recently uploaded (20)

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 

The Stack Frame

  • 1. The Stack Frame An Ivaylo Marinkov presentation ivaylo@tsarstva.bg
  • 2. Call Stack A stack data structure storing information of a computer program's active subroutines. Whilst important for the software's proper functioning, its details are usually hidden and usage is automated in high-level programming languages.
  • 3. Call Stack Purpose Keeping record of the point to which each active subroutine should return control when finished.
  • 4. Active Subroutine A routine that has been called but has not finished execution yet. Afterwords, control should be returned to the point where the call has been made. Routines may be nested to any level and recursion is possibe – hence the stack structure.
  • 5. For example, the DrawSquare subroutine calls the DrawLine subroutine from four different places. DrawLine must know where to return once completed. This is accomplished by pushing the address following the call instruction – the return address – onto the stack with each call.
  • 6. Call Stack Inner Workings The caller pushes the return address onto the stack (winding). The called subroutine, when it finishes, pops the return address off the stack and transfers control to it (unwinding). If a called subroutine calls on yet another subroutine, it will push another address onto the stack, and so on, with the information stacking up and unstacking as the program dictates.
  • 7. Should pushing consume all the space allocated for the call stack, an error called stack overflow will occur. There is usually a single call stack associated with each process thread. However, the program may create additional call stacks for tasks such as signal-hadling or cooperative multitasking.
  • 8. Additional Call Stack Functions Local data storage – keeping local-scope variable values. Parameter passing – storage for values passed by calling code. Evalution stack – in some cases operands for logical and arithmetic operations may be stored in the call stack. Current instance pointer – for this pointer in object-oriented languages.
  • 9. Structure A call stack is composed of stack frames, machine and application banary interface- dependant data structures containing subroutine state information.
  • 10. The Stack Frame Generally speaking, a procedure's stack frame contains all the information necessary to save and restore the state of the procedure.
  • 11. Strictly speaking, it is only necessary for the calling program and the called procedure to agree on the structure of the stack frame for each procedure call. However, the specification of a calling convention facilitates the use of procedure libraries by defining the structure of the stack frame uniformly for all procedure calls.
  • 12. The frame pointer is stored in register $30, also known as $fp. A stack frame consists of the memory on the stack between the frame pointer and the stack pointer. Three steps are necessary to call a Calling convention used in the MIPS architecture stack frame procedure.
  • 13. 1. Pass the arguments. The first four arguments are passed in registers $a0-$a3. The remaining arguments are pushed onto the stack. 2. Save the caller-saved registers. This includes registers $t0-$t9, if they contain live values at the call site. The endless MIPS 3. Execute a jal instruction. cycle
  • 14. 1. Pass the arguments. The first four arguments are passed in registers $a0-$a3. The remaining arguments are pushed onto the stack. 2. Save the caller-saved registers. This includes registers $t0-$t9, if they contain live values at the call site. The endless MIPS 3. Execute a jal instruction. cycle
  • 15. Within the called routine, the following steps are necessary: 1. Establish the stack frame by subtracting the frame size from the stack pointer. 2. Save the callee-saved registers in the frame. Register $fp is always saved. Register $ra and registers $a0-$a3 need to be saved if they are in use and the routine itself makes calls. Any of the registers $s0- $s7 that are used by the callee need to be saved. 3. Establish the frame pointer by adding the stack frame size to the address in $sp.
  • 16. To return from a call, a function places the returned value into $v0 and executes the following steps: 1. Restore any callee-saved registers that were saved upon entry. 2. Pop the stack frame by subtracting the frame size from $sp. 3. Return by jumping to the address in register $ra.
  • 17. Debugging The purpose of a debugger such as GDB (gnu.org/software/gdb) is to allow you to see what is going on “inside” another program while it executes--or what another program was doing at the moment it crashed.
  • 18. In Practice An illustration to viewing the call stack using GDB. 1. Compile your program with the -g option, like cc -g -o p1 p1.c
  • 19. 2. Navigate to your program's directory and run GDB: gdb your_program If all went fine, you will land on a command prompt.
  • 20. 3. Then install some breakpoints using the break command: break function_name 4. Now run. The program will proceed until the first breakpoint.
  • 21. You can select a frame using the frame n command and view frame information using the info frame n command. Some of the details this command displays are the addresses of the frame, the next frame down (called by this frame) and the next frame up (caller of this frame).
  • 22. View the call stack using the backtrace command. A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the one currently in execution (frame zero), followed by its caller (frame one), and on up the stack.