CMU Computer Systems: Machine-Level Programming (Procedures)

Mechanisms in Procedures

  • Passing control
    • To beggining of procedure code
    • Back to return point
  • Passing data
    • Procedure arguments
    • Return value
  • Memory management
    • Allocate during procedure execution
    • Deallocate upon return
  • Mechanisms all implemented with machine instructions
  • x86-64 implemention of a procedure uses only those mechanisms required

Procedures

  • Stack Structure
  • Calling Conventions
    • Passing control
    • Passing data
    • Managing local data
  • Illustration of Recursion
x86-64 Stack
  • Region of memory managed with stack discipline
  • Grows toward lower address
  • Register %rsp contains lowest stack address
    • address of “top” element
  • Operation: Push and Pop
Procedure Control Flow
  • Use stack to support procedure call and return
  • Procedure call: call label
    • Push return address on stack
    • Jump to label
  • Return address:
    • Address of the next instruction right after call
    • Example from disassembly
  • Procedure return: ret
    • Pop address from stack
    • Jump to address
Procedure Data Flow (ABI rule)
  • Registers
    • First 6 arguments: %rdi %rsi %rdx %rcx %r8 %r9
  • Return value: %rax
  • Only allocate stack space when needed
Stack-Based Languages
  • Languages that support recursion
    • C, Pascal, Java
    • Code must be “Reentrant”
      • Multiple simultaneous instantiations of single procedure
    • Need some place to store state of each instantiation
      • Arguments
      • Local variables
      • Return pointer
  • Stack discipline
    • State for given procedure needed for limited time
      • From when called to when return
    • Callee returns before caller does
  • Stack allocated in Frames
    • state for single procedure instantiation
Stack Frames
  • Contents
    • Return information
    • Local storage (if needed)
    • Temporary space (if needed)
  • Management
    • Space allocated when enter procedure
      • “Set-up” code
      • Includes push by call instruction
    • Deallocated when return
      • “Finish” code
      • Includes pop by ret instruction
Register Saving Conventions
  • “Caller Saved”
    • Caller saves temporary values in its frame before the call
  • “Callee Saved”
    • Callee saves temporary values in its frame before using
    • Callee restores them before returning to caller
x86-64 Linux Register Usage
  • %rax
    • Return value
    • Also caller-saved
    • Can be modified by procedure
  • %rdi, …, %r9
    • Arguments
    • Also caller-saved
    • Can be modified by procedure
  • %r10, %r11
    • Caller-saved
    • Can be modified by procedure
  • %rbx, %r12, %r13, %r14
    • Callee-saved
    • Callee must save & restore
  • %rbp
    • Callee-saved
    • Callee must save & restore
    • May be used as frame pointer
    • Can mix & match
  • %rsp
    • Special form of callee save
    • Restored to original value upon exit from procedure
Observations About Recursion
  • Handled Without Special Consideration
    • Stack frames mean that each function call has private storage
      • Saved registers & local variables
      • Saved return pointer
    • Register saving conventions prevent one function call from corrupting another’s call
      • Unless the C code explicitly does so
    • Stack discipline follows call/ return pattern
  • Also works for mutual recursion
    • P calls Q; Q calls P

x86-64 Procedure Summary

  • Important Points
    • Stack is the right data structure for procedure call/ return
      • If P calls Q, then Q returns before P
  • Recursion (& mutual recursion) handled by normal calling conventions
    • Can safely store values in local stack frame and in callee-saved registers
    • Put function arguments at top of stack
    • Result return in %rax
  • Pointers are addresses of values
    • On stack or global
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值