Lec 5: Calling conventions and stack frames RISC-V
- Ref: https://github.com/huihongxiao/MIT6.S081/tree/master/lec05-calling-conventions-and-stack-frames-risc-v
- Preparation: Calling Convention
Outline
- C -> ASM / Processors
- RISC-V & x86
- Registers
- Stack + calling conventions
- Struct Layout in memory
C -> ASM
- C -> Asm -> Bianary (.o/.obj 文件)
RISC-V & x86
- RISC-V(精简指令集)和 x86(复杂指令集)的区别:
- 指令集的数量: RISC-V 指令集更少
- RISC-V 指令的操作比 x86 更简单
- RISC 指令集开源
- RISC-V 指令集组成:
- Base Integer Instruction Set: 常用指令, 如
add
,mult
- Standard Extension Instruction Set: 扩展指令集
- Base Integer Instruction Set: 常用指令, 如
RISC-V Registers
- a0~a7用于函数传参, 参数超过 8 个使用该内存传参
- Saver:
- Caller: Not preserved across function call 该类型寄存器的值在函数调用期间不会保留, 即该寄存器在函数调用中可能被重写
- Callee: preserved across function call
Stack
- fp: frame ponter, 即栈帧, 相当于 x86 的 bp.
- 汇编函数 ASM Function 组成:
- Function prologue: 调整 sp, 保存返回地址 ra
- Body
- Epollgue: 将返回地址加载会 ra 寄存器, 还原 sp