操作系统

  1. 新建一个fact函数
  2. 代码如下:
#include "syscall.h"

int main()
{
int result;
result=Fact(6);

Halt();
}
  1. syscall.h

#define SC_Fact 12

int Fact(int a);

  1. start.s

加入下面代码

	.globl Fact
.ent Fact

Fact:
addiu $2,$0,SC_Fact
syscall
j $31
.end Fact
  1. 在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;
    }