-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial.s
124 lines (100 loc) · 2.11 KB
/
factorial.s
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Recursive implementation of a factorial calculator.
# You will need to complete the factorial function at the bottom of this file.
# Your program should yield n! = 1 for any n < 1.
# YOUR-NAME-HERE, DD/MM/YYYY
########################################################################
# .DATA
.data
prompt_str: .asciiz "Enter n: "
result_str: .asciiz "! = "
########################################################################
# .TEXT <main>
.text
main:
# DO NOT MODIFY THIS FUNCTION.
# Args: void
# Returns: int
#
# Frame: [$ra, $s0]
# Uses: [$v0, $a0, $s0, $t0]
# Clobbers: [$v0, $a0, $t0]
#
# Locals:
# - $s0: int n
# - $t0: int f
#
# Structure:
# - main
# -> [prologue]
# -> [body]
# -> [epilogue]
main__prologue:
begin
push $ra
push $s0
main__body:
li $v0, 4 # syscall 4: print_string
la $a0, prompt_str #
syscall # printf("%s", "Enter n: ");
li $v0, 5 # syscall 5: read_int
syscall #
move $s0, $v0 # scanf("%d", &n);
move $a0, $s0
jal factorial
move $t0, $v0 # int f = factorial(n);
li $v0, 1 # syscall 1: print_int
move $a0, $s0 #
syscall # printf("%d", n);
li $v0, 4 # syscall 4: print_string
la $a0, result_str #
syscall # printf("%s", "! = ")
li $v0, 1 # syscall 1: print_int
move $a0, $t0 #
syscall # printf("%d", f)
li $v0, 11 # syscall 11: print_char
li $a0, '\n' #
syscall # printf("%c", '\n');
main__epilogue:
pop $s0
pop $ra
end
li $v0, 0
jr $ra # return 0;
# DO NOT MODIFY THE ABOVE CODE.
########################################################################
# .TEXT <factorial>
.text
factorial:
# Args:
# - $a0: int n
# Returns: int
#
# Frame: [...]
# Uses: [...]
# Clobbers: [...]
#
# Locals:
# - ...
#
# Structure:
# - factorial
# -> [prologue]
# -> [body]
# -> [epilogue]
factorial__prologue:
begin
push $ra
factorial__body:
ble $a0, 1, factorial_else_if
push $a0
add $a0, $a0, -1
jal factorial__prologue
pop $a0
mul $v0, $a0, $v0
b factorial__epilogue
factorial_else_if:
li $v0, 1
factorial__epilogue:
pop $ra
end
jr $ra