-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog37
186 lines (143 loc) · 3.79 KB
/
prog37
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
--37
.data
// Definición de la pila
pila: .skip 800 // Espacio para 100 elementos (8 bytes cada uno)
tam_pila: .quad 100 // Tamaño máximo de la pila
tope: .quad 0 // Índice del tope de la pila
// Mensajes de formato para printf
msg_push: .string "Elemento insertado: %d\n"
msg_pop: .string "Elemento extraído: %d\n"
msg_peek: .string "Elemento en el tope: %d\n"
msg_llena: .string "Error: Pila llena\n"
msg_vacia: .string "Error: Pila vacía\n"
.text
.global main
// Función para verificar si la pila está vacía
is_empty:
adr x0, tope
ldr x0, [x0]
cmp x0, #0
cset x0, eq
ret
// Función para verificar si la pila está llena
is_full:
adr x0, tope
ldr x0, [x0]
adr x1, tam_pila
ldr x1, [x1]
cmp x0, x1
cset x0, eq
ret
// Función push: inserta un elemento en la pila
push:
stp x29, x30, [sp, #-16]!
stp x19, x20, [sp, #-16]!
mov x19, x0 // Guardar valor a insertar
bl is_full
cbnz x0, push_error
// Obtener posición actual
adr x0, tope
ldr x1, [x0] // x1 = tope actual
// Calcular dirección donde insertar
adr x2, pila
lsl x3, x1, #3 // x3 = tope * 8
add x2, x2, x3 // x2 = dirección donde insertar
// Insertar valor
str x19, [x2]
// Incrementar tope
add x1, x1, #1
str x1, [x0]
// Imprimir mensaje de éxito
adr x0, msg_push
mov x1, x19
bl printf
mov x0, #0 // Éxito
b push_fin
push_error:
adr x0, msg_llena
bl printf
mov x0, #-1 // Error
push_fin:
ldp x19, x20, [sp], #16
ldp x29, x30, [sp], #16
ret
// Función pop: extrae el elemento del tope
pop:
stp x29, x30, [sp, #-16]!
stp x19, x20, [sp, #-16]!
bl is_empty
cbnz x0, pop_error
// Obtener posición actual
adr x0, tope
ldr x1, [x0] // x1 = tope actual
sub x1, x1, #1 // Decrementar tope
// Calcular dirección del elemento
adr x2, pila
lsl x3, x1, #3 // x3 = (tope-1) * 8
add x2, x2, x3
// Extraer valor
ldr x19, [x2] // x19 = valor a retornar
// Actualizar tope
str x1, [x0]
// Imprimir mensaje
adr x0, msg_pop
mov x1, x19
bl printf
mov x0, x19 // Retornar valor
b pop_fin
pop_error:
adr x0, msg_vacia
bl printf
mov x0, #-1
pop_fin:
ldp x19, x20, [sp], #16
ldp x29, x30, [sp], #16
ret
// Función peek: mira el elemento del tope sin extraerlo
peek:
stp x29, x30, [sp, #-16]!
stp x19, x20, [sp, #-16]!
bl is_empty
cbnz x0, peek_error
// Obtener posición actual
adr x0, tope
ldr x1, [x0] // x1 = tope actual
sub x1, x1, #1 // Apuntar al último elemento
// Calcular dirección del elemento
adr x2, pila
lsl x3, x1, #3 // x3 = (tope-1) * 8
add x2, x2, x3
// Obtener valor
ldr x19, [x2] // Guardar valor
// Imprimir mensaje
adr x0, msg_peek
mov x1, x19
bl printf
mov x0, x19 // Retornar valor
b peek_fin
peek_error:
adr x0, msg_vacia
bl printf
mov x0, #-1
peek_fin:
ldp x19, x20, [sp], #16
ldp x29, x30, [sp], #16
ret
main:
stp x29, x30, [sp, #-16]!
// Probar push
mov x0, #10
bl push
mov x0, #20
bl push
mov x0, #30
bl push
// Probar peek
bl peek
// Probar pop
bl pop
// Probar pop de nuevo
bl pop
ldp x29, x30, [sp], #16
ret
.global printf