-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
48 lines (42 loc) · 1.25 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "machine.h"
uint32_t factorial_ins[1000] = {
SYSCALL_INS(sys_read_int),
RFMT_INS(move, a0, v0, nil, 0),
JFMT_INS(jal, 7), // call proc.
RFMT_INS(move, a0, v0, nil, 0),
SYSCALL_INS(sys_print_int),
SYSCALL_INS(sys_newline),
SYSCALL_INS(sys_halt), // exit
IFMT_INS(addi, sp, sp, -2), // factorial proc.
IFMT_INS(sw, ra, sp, 0),
IFMT_INS(sw, a0, sp, 1),
IFMT_INS(bnz, a0,nil, 3),
IFMT_INS(li, v0,nil, 1), // when n = 0
IFMT_INS(addi, sp, sp, 2),
RFMT_INS(jr, ra,nil, nil, 0),
IFMT_INS(addi, a0, a0, -1), // when n > 0
JFMT_INS(jal, 7), // recursively call factorial proc.
IFMT_INS(lw, a0, sp, 1),
RFMT_INS(mul, v0, a0, nil, 0),
RFMT_INS(mflo, v0,nil, nil, 0),
RFMT_INS(move, a0, v0, nil, 0),
IFMT_INS(lw, ra, sp, 0),
IFMT_INS(addi, sp, sp, 2),
RFMT_INS(jr, ra,nil, nil, 0)
};
int main(void)
{
init_environment();
MACHINE *mach = make_machine();
for (int i = 0; i < 23; i++)
printf("ins %d: %08x\n", i, factorial_ins[i]);
printf("\n");
init_machine(mach);
load_memory(factorial_ins, 100, mach, 0);
run_machine(mach);
free_machine(mach);
return 0;
}