RPC知识杂项
操作系统
- 新建一个fact函数
- 代码如下:
|
- syscall.h
#define SC_Fact 12
int Fact(int a);
- start.s
加入下面代码
.globl Fact |
在exception.cc中加入下面内容,照猫画虎
计算阶乘
else if ((which == SyscallException) && (type == SC_Fact)) {
int n = machine->ReadRegister(4);
int result = 1;
// 计算阶乘
for (int i = 1; i <= n; ++i) {
result *= i;
}
// 将结果存储在寄存器 2 中
machine->WriteRegister(2, result);
printf("\nsystem call Fact......\n");
printf("n is %d\n", n);
printf("Factorial of %d = %d\n\n", n, result);
/* set previous program counter (debugging only) */
machine->WriteRegister(PrevPCReg, machine->ReadRegister(PrevPCReg));
/* set program counter to next instruction (all instructions are 4 bytes wide) */
machine->WriteRegister(PCReg, machine->ReadRegister(PCReg) + 4);
/* set next program counter for branch execution */
machine->WriteRegister(NextPCReg, machine->ReadRegister(NextPCReg) + 4);
return;
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 代码有点萌!
评论
ValineDisqus