Embedded之Stack之二
1 Function
Programming languages make functions easy to maintain and write by giving each function its own section of memory to operate in.
For example, suppose you have the following function.
int pickMin( int x, int y, int z ) {
int min = x ;
if ( y < min )
min = y ;
if ( z < min )
min = z ;
return min ;
}
You declare parameters x, y, and z. You also declare local variables, min. You know that these variables won't interfere with other variables in other functions, even if those functions use the same variable names.
In fact, you also know that these variables won't interfere with separate invocations of itself.
2 Stacks and functions
Imagine we're starting in main() in a C program. The stack looks something like this

Suppose, inside of body of main() there's a call to foo(). Suppose foo() takes two arguments. One way to pass the arguments to foo() is through the stack. Thus, there needs to be assembly language code in main() to "push" arguments for foo() onto the the stack.

Once we get into code for foo(), the function foo() may need local variables, so foo() needs to push some space on the stack.
We've added a new pointer called FP which stands for frame pointer. The frame pointer points to the location where the stack pointer was, just before foo() moved the stack pointer for foo()'s own local variables.

When exit foo() the stack looks just as it did before we pushed on foo()'s stack frame, except this time the return value has been filled in.

Once main() has the return value, it can pop that and the arguments to foo() off the stack.

Embedded之Stack之二的更多相关文章
- Embedded之Stack之三
Stack Overflow While stacks are generally large, they don't occupy all of memory. It is possible to ...
- Embedded之Stack之一
1 Intro When a program starts executing, a certain contiguous section of memory is set aside for the ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- Spark性能优化【Stack Overflow】
一.异常情况 Stack Overflow 二.异常分析 之所以会产生Stack Overflow,原因是在Stack方法栈中方法的调用链条太长的原因导致的,一般情况有两种: 1.过于深度的递归[常见 ...
- C++ STL——stack和queue
目录 一 stack容器 二 queue容器 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 栈和队列作为经典的数据结构,我们再熟悉不过了.C++ ST ...
- SpringBoot笔记(7)
一.单元测试 1.JUnit5简介 Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库 作为最新版本的JUnit框架,JUnit5与之前版本的Junit框架有很大的不 ...
- 痞子衡嵌入式:IAR内部C-SPY调试组件配套宏文件(.mac)用法介绍
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IAR内部C-SPY调试组件配套宏文件(.mac)用法. 痞子衡之前写过一篇 <JLink Script文件基础及其在IAR下调用 ...
- MATLAB绘图
matlab绘制散点图 clc,clear x=[11.9,11.5,14.5,15.2,15.9,16.3,14.6,12.9,15.8,14.1]; y=[196.84,196.84,197.14 ...
- 剑指offer系列34----按之字形顺序打印二叉树
[题目]请实现一个函数按照之字形打印二叉树, * 即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印, * 其他行以此类推. 未优化,不是最优解,博主用的是队列 ...
随机推荐
- C库的制作
1.库的概念:库是一种可执行的二进制形式: 2.分类: 1>静态库 ①.在程序编译时会被连接到目标代码中: ②.程序运行时不再需要该静态库: ③.体积较大: 2>动态库/共享库 ①.在程序 ...
- 20170704-WNDR4300串口连接信息
find_hif: bootstrap = 0xaf055aWASP BootROM Ver. 1.1Nand Flash initONFI: Control setting = 0xb44find_ ...
- Ac自动机基础题集合
Ac_automaton的板子打熟以后发现碰到题不会做,而且还是比较纯的板子,只要改几处地方就可以,Ac_automation有许多优秀而fantasy的性质,下面粘几个题,来记录一下做题的心得. 1 ...
- BUPT2017 springtraining(16) #3 ——搜索与动态规划
题目在这里啊 A.最长上升子序列,范围很小所以写了简单的O(n^2)算法 #include <iostream> #define rep(i, j, k) for(int i = j;i ...
- jenkins简单持续构建
一.安装jenkins 二.将需要持续构建的java project打包成jar文件 1.选择导出需要运行的main方法所在java类
- [HDU1403]Longest Common Substring(后缀数组)
传送门 求两个串的公共子串(注意,这个公共子串是连续的一段) 把两个串连在一起,中间再加上一个原字符串中不存在的字符,避免过度匹配. 求一遍height,再从height中找满足条件的最大值即可. 为 ...
- 用Grails写单元测试
新的领域,多练练,这样写出的程序,确实坚固些. 也要理解集成测试与数据库相关,单元测试与类方法有关. 如果测试文件没有建立,按如下操作: Unit tests are generated automa ...
- [bzoj2648/2716]SJY摆棋子_KD-Tree
SJY摆旗子 bzoj-2648 题目大意:平面上有n个黑子.有m个操作,可以下一颗白子,查询与曼哈顿距离下最近黑子之间的曼哈顿距离,或者下一颗黑子. 注释:$1\le n,m\le 5\cdot 1 ...
- Spring MVC 入门(二)
前言 上一篇讲到了 Spring MVC 的基本请求处理流程,本篇主要说一说如何使用 Spring MVC 来构建一个简单例子. 大致步骤如下 1.新建项目 2.导入相应 jar 包 (Spring ...
- 2015 编程之美初赛第一场 AC题
题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...