-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEjercicio_19.s
218 lines (190 loc) · 5.29 KB
/
Ejercicio_19.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Lenguajes de Interfaz - Actividad 19
# Suma de elementos en un arreglo
# Alumno: Gómez Aguilar Jared Emmanuel
# Número de Control: 22210309
# Python y Ensamblador
# ---------------------------------------
/*
def suma_arreglo(arreglo):
return sum(arreglo)
*/
# ---------------------------------------
// Gómez Aguilar Jared Emmanuel
// 22210309
.data
prompt_size: .asciz "Enter the number of elements in the array: "
prompt_element: .asciz "Enter element #"
buffer: .skip 20
array: .skip 100 // Space for 25 integers (4 bytes each)
result: .skip 64
colon: .asciz ": "
newline: .asciz "\n"
.text
.global _start
_start:
// Print prompt for array size
mov x0, #1
adr x1, prompt_size
mov x2, #44
mov x8, #64
svc #0
// Read array size
mov x0, #0
adr x1, buffer
mov x2, #20
mov x8, #63
svc #0
// Convert array size to integer
adr x1, buffer
mov x2, #0 // result
mov x3, #10 // multiplier
convert_size_loop:
ldrb w4, [x1]
cmp w4, #'\n'
beq convert_size_done
sub w4, w4, #'0'
mul x2, x2, x3
add x2, x2, x4
add x1, x1, #1
b convert_size_loop
convert_size_done:
mov x9, x2 // Save array size
mov x10, #0 // Array index
adr x11, array // Array base address
// Input array elements
input_loop:
cmp x10, x9
bge input_done
// Print prompt for current element
mov x0, #1
adr x1, prompt_element
mov x2, #19
mov x8, #64
svc #0
// Print element number
adr x1, buffer
mov x2, x10
add x2, x2, #1 // 1-based indexing
bl int_to_ascii
mov x0, #1
adr x1, buffer
bl print_string
// Print colon
mov x0, #1
adr x1, colon
mov x2, #2
mov x8, #64
svc #0
// Read element
mov x0, #0
adr x1, buffer
mov x2, #20
mov x8, #63
svc #0
// Convert element to integer
adr x1, buffer
mov x2, #0 // result
mov x3, #10 // multiplier
convert_element_loop:
ldrb w4, [x1]
cmp w4, #'\n'
beq convert_element_done
sub w4, w4, #'0'
mul x2, x2, x3
add x2, x2, x4
add x1, x1, #1
b convert_element_loop
convert_element_done:
// Store element in array
str x2, [x11, x10, lsl #3] // 8-byte integers
add x10, x10, #1
b input_loop
input_done:
// Calculate sum of array
mov x2, #0 // Sum
mov x10, #0 // Index
sum_loop:
cmp x10, x9
bge sum_done
ldr x3, [x11, x10, lsl #3]
add x2, x2, x3
add x10, x10, #1
b sum_loop
sum_done:
// Convert sum to ASCII
adr x1, result
mov x3, #10 // Divisor
mov x4, #0 // Digit counter
convert_to_ascii:
udiv x5, x2, x3 // Divide by 10
msub x6, x5, x3, x2 // Get remainder
add x6, x6, #'0' // Convert to ASCII
strb w6, [x1, x4] // Store digit
add x4, x4, #1 // Increment counter
mov x2, x5 // Update number
cbnz x2, convert_to_ascii
// Reverse the string
mov x5, #0 // Start index
sub x6, x4, #1 // End index
reverse_loop:
cmp x5, x6
bhs reverse_done
ldrb w7, [x1, x5]
ldrb w8, [x1, x6]
strb w8, [x1, x5]
strb w7, [x1, x6]
add x5, x5, #1
sub x6, x6, #1
b reverse_loop
reverse_done:
// Add newline
strb w3, [x1, x4]
mov w3, #'\n'
strb w3, [x1, x4]
// Print result
mov x0, #1 // stdout
adr x1, result
add x2, x4, #1 // Length including newline
mov x8, #64 // sys_write
svc #0
exit:
mov x0, #0 // return 0
mov x8, #93 // sys_exit
svc #0
// Helper function to convert integer to ASCII
int_to_ascii:
adr x1, buffer
mov x3, #10 // Divisor
mov x4, #0 // Digit counter
int_to_ascii_loop:
udiv x5, x2, x3 // Divide by 10
msub x6, x5, x3, x2 // Get remainder
add x6, x6, #'0' // Convert to ASCII
strb w6, [x1, x4] // Store digit
add x4, x4, #1 // Increment counter
mov x2, x5 // Update number
cbnz x2, int_to_ascii_loop
// Reverse the string
mov x5, #0 // Start index
sub x6, x4, #1 // End index
int_to_ascii_reverse:
cmp x5, x6
bhs int_to_ascii_reverse_done
ldrb w7, [x1, x5]
ldrb w8, [x1, x6]
strb w8, [x1, x5]
strb w7, [x1, x6]
add x5, x5, #1
sub x6, x6, #1
b int_to_ascii_reverse
int_to_ascii_reverse_done:
mov x2, #0
strb w2, [x1, x4]
ret
// Helper function to print string
print_string:
mov x8, #64 // sys_write
svc #0
ret
ASCIINEMA
https://asciinema.org/a/690924