-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage.asm
More file actions
154 lines (117 loc) · 2.89 KB
/
average.asm
File metadata and controls
154 lines (117 loc) · 2.89 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
150
151
152
153
154
.data
# Messages for user interaction
msg1: .asciiz "\nEnter the count of numbers you want to find average of (0 to exit): "
msg2: .asciiz "Enter number "
msg3: .asciiz ": "
msg4: .asciiz "\nThe average is: "
msg5: .asciiz "\nError: Count must be between 1 and 100\n"
msg6: .asciiz "\nProgram terminated.\n"
colon: .asciiz ": "
# Ensure 4-byte alignment for the array
.align 2 # 2^2 = 4 byte alignment
# Allocate more space for the array (400 bytes = space for 100 integers)
array: .space 400
.text
main:
# Main program loop
jal get_count
# Check if user wants to exit (entered 0)
beqz $v0, exit_program
# Store count in $s0 for future use
move $s0, $v0
# Get input numbers
jal input_numbers
# Calculate and display average
jal calculate_average
# Loop back to beginning
j main
# Get count from user and validate
get_count:
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
# Check if user wants to exit
beqz $v0, get_count_return
# Validate count (must be between 1 and 100)
blez $v0, invalid_count
bgt $v0, 100, invalid_count
j get_count_return
invalid_count:
li $v0, 4
la $a0, msg5
syscall
# Try again
j get_count
get_count_return:
jr $ra
# Input numbers into array
input_numbers:
# Initialize counters
li $t0, 0 # Loop counter
li $t1, 0 # Array index offset
input_loop:
# Display prompt with number index
li $v0, 4
la $a0, msg2
syscall
# Display current number (1-based)
addi $a0, $t0, 1
li $v0, 1
syscall
# Display colon
li $v0, 4
la $a0, colon
syscall
# Read integer
li $v0, 5
syscall
# Store in array
sw $v0, array($t1)
# Increment counters
addi $t0, $t0, 1
addi $t1, $t1, 4
# Check if we've reached the count
blt $t0, $s0, input_loop
jr $ra
# Calculate and display average
calculate_average:
# Initialize sum
li $t0, 0 # Sum
li $t1, 0 # Array index offset
li $t2, 0 # Loop counter
sum_loop:
# Load number from array
lw $t3, array($t1)
# Add to sum
add $t0, $t0, $t3
# Increment counters
addi $t2, $t2, 1
addi $t1, $t1, 4
# Check if we've processed all numbers
blt $t2, $s0, sum_loop
# Calculate average as floating point
# Convert sum to float
mtc1 $t0, $f0
cvt.s.w $f0, $f0
# Convert count to float
mtc1 $s0, $f1
cvt.s.w $f1, $f1
# Perform floating point division
div.s $f12, $f0, $f1
# Display result
li $v0, 4
la $a0, msg4
syscall
# Print floating point result (value already in $f12)
li $v0, 2
syscall
jr $ra
# Exit program
exit_program:
li $v0, 4
la $a0, msg6
syscall
li $v0, 10
syscall