注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路

To find the final symmetry & beauty

 
 
 

日志

 
 
 
 

caller saved register and callee saved register[原创]  

2010-11-15 02:16:53|  分类: 计算之美 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

           It is often confusing that how registers are controlled during function calls. Actually, registers are core resources in computer and they have very severe control conventions most for performance and compatibility reasons. Register control is often art of compilers not CPU or OS. If all compilers follows the same set of control strategies, modules written compiled by different compilers or modules form different programming language can be combined (linked) to form an executable file.

         Here I want to explain the somewhat academic phrase “caller saved registers” and “called saved registers”. Take the following call hierarchy as an example. The following is certainly not an formal C syntax, But you should know the call sequence. G call N and N in turn calls C. from the perspective from N, G is the caller and N is callee. When code runs in C,N would be caller and C this time the callee. Take the MIPS as an instance. MIPS have caller saved registers from $t0 to $t7 and callee saved registers $s0 to $s9.

         G->N->C

G()

{

         N()

         {

                   C();

         }

}

Caller saved registers mean that the registers’ content may be damaged by callee of this caller. From the above function call hierarchy, within the scope of N, caller saved registers’ content may be damaged by C. So, when N want to use the the caller saved registers, It must make the assumption that the content of the registers may be changed by the callee. If the caller N want to use the register $t0 for example, It must save the register’s content to memory for using after function call. All in all, the caller should responsible for the caller saved registers’ content,  the callee has no responsibility to care about them. Now, let’s take a look at the callee saved registers. From the name we can guess that the callee should be responsible for this set of registers’ content. If the callee wants to the this kind of register, It must save its content before using it and restore the content before return. The whole result is that the callee saved registers’ content change after the caller call a function. The callee is responsible for it. So, these kind of registers are called callee saved registers.

         The above is more about convention but principle. A function certainly can changed all registers(except for hardware forbids this), So this convention is controlled by human(those who write an compiler). If you write your own compiler you should know these different conventions for compatible with other modules compiled by other compiler, or you won’t make your compiler popular. Note that a function may both save “caller saved registers” and “callee saved registers” to memory for later restore. Save “caller saved registers” for itself later use and “called saved registers” for not destroying content for its caller.

  评论这张
 
阅读(2148)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017