-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination_calculator.asm
More file actions
149 lines (115 loc) · 3.1 KB
/
combination_calculator.asm
File metadata and controls
149 lines (115 loc) · 3.1 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
.data
program_title: .asciiz "\n===== Combination Calculator (nCr) =====\n"
input1: .asciiz "Please Enter the value of 'n': "
input2: .asciiz "Please Enter the value of 'r': "
result: .asciiz "\nResult (nCr): "
error_negative: .asciiz "\nError: Input values must be positive integers."
error_r_gt_n: .asciiz "\nError: 'r' cannot be greater than 'n'."
continue_prompt: .asciiz "\n\nCalculate another combination? (1 for yes, 0 for no): "
goodbye_msg: .asciiz "\nThank you for using the Combination Calculator!\n"
.text
main:
# Display program title
li $v0, 4
la $a0, program_title
syscall
input_loop:
# Get value of n
li $v0, 4
la $a0, input1
syscall
li $v0, 5
syscall
move $s0, $v0 # $s0 = n value
# Get value of r
li $v0, 4
la $a0, input2
syscall
li $v0, 5
syscall
move $s1, $v0 # $s1 = r value
# Validate inputs
# Check if n > 0
blez $s0, input_error_negative
# Check if r > 0
blez $s1, input_error_negative
# Check if n >= r
blt $s0, $s1, input_error_r_gt_n
# Calculate nCr
jal calculate_combination
# Display result
li $v0, 4
la $a0, result
syscall
li $v0, 1
move $a0, $s2 # $s2 = result of combination
syscall
j prompt_continue
input_error_negative:
li $v0, 4
la $a0, error_negative
syscall
j prompt_continue
input_error_r_gt_n:
li $v0, 4
la $a0, error_r_gt_n
syscall
j prompt_continue
prompt_continue:
# Ask if user wants to continue
li $v0, 4
la $a0, continue_prompt
syscall
li $v0, 5
syscall
# If input is 1, continue with another calculation
beq $v0, 1, main
# Otherwise exit with goodbye message
li $v0, 4
la $a0, goodbye_msg
syscall
# Terminate program
li $v0, 10
syscall
# Function to calculate combination using (n!) / (r! * (n-r)!)
calculate_combination:
# Special case: if r = 0 or r = n, result is 1
beq $s1, $zero, return_one
beq $s0, $s1, return_one
# Special case: if r = 1, result is n
li $t0, 1
beq $s1, $t0, return_n
# Calculate n!
move $t0, $s0 # $t0 = n
li $t3, 1 # $t3 = factorial result
factorial_n:
mul $t3, $t3, $t0
subi $t0, $t0, 1
bgtz $t0, factorial_n
move $s3, $t3 # $s3 = n!
# Calculate r!
move $t0, $s1 # $t0 = r
li $t3, 1 # $t3 = factorial result
factorial_r:
mul $t3, $t3, $t0
subi $t0, $t0, 1
bgtz $t0, factorial_r
move $s4, $t3 # $s4 = r!
# Calculate (n-r)!
sub $t0, $s0, $s1 # $t0 = n-r
li $t3, 1 # $t3 = factorial result
factorial_n_minus_r:
mul $t3, $t3, $t0
subi $t0, $t0, 1
bgtz $t0, factorial_n_minus_r
move $s5, $t3 # $s5 = (n-r)!
# Calculate nCr = n! / (r! * (n-r)!)
mul $t0, $s4, $s5
div $s2, $s3, $t0
jr $ra
return_one:
li $s2, 1
jr $ra
return_n:
move $s2, $s0
jr $ra