-
Notifications
You must be signed in to change notification settings - Fork 0
/
pila_reverse.asm
138 lines (111 loc) · 1.99 KB
/
pila_reverse.asm
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
#EMPEZADO A LAS 18:25 -> CREATE e INSERT acabados correctamente a las 19:35 (1h10m) PRINT acabado a las 19:42(1h 17m)
.data
msg: .asciiz "Introduzca un numero: "
sep: .asciiz " | "
.text
main:
la $a0, msg
li $v0, 4
syscall
li $v0, 5
syscall
move $a0,$v0
move $a1, $zero
jal create
move $s0, $v0
main_loop:
la $a0, msg
li $v0, 4
syscall
li $v0, 5
syscall
beqz $v0, exit
move $a1,$v0
move $a0, $s0
jal insert_in_order_reverse #From smallest to biggest
beqz $v0, main_loop
move $s0, $v0
b main_loop
exit:
move $a0, $s0
jal print_all
li $v0, 10
syscall
create: #IN (a0 -> Val; a1 -> P_Next) | OUT (v0 -> P_Me)
#PROTOCOL START
subu $sp, $sp, 32
sw $ra, 16($sp)
sw $fp, 20($sp)
addiu $fp, $sp, 24
sw $a0, 0($fp)
sw $a1, 4($fp)
li $a0,8
li $v0,9
syscall
lw $a0, 0($fp)
sw $a0, 0($v0)
sw $a1, 4($v0)
#PROTOCOL END
lw $ra, 16($sp)
lw $fp, 20($sp)
addiu $sp, $sp, 32
jr $ra
insert_in_order_reverse: #IN (a0 -> P_First; a1 -> Val) | OUT (v0 -> P_First_b [0 if not changes])
#PROTOCOL START
subu $sp, $sp, 32
sw $ra, 16($sp)
sw $fp, 20($sp)
addiu $fp, $sp, 24
sw $a0, 0($fp)
sw $a1, 4($fp)
lw $t0, 0($a0)
ble $a1, $t0, insert_first
insert_loop:
lw $t2, 4($a0)
beqz $t2, insert_post
lw $t0, 0($t2)
ble $a1, $t0, insert_post
lw $a0, 4($a0)
sw $a0, 0($fp) #Stack changes
b insert_loop
insert_post: #a0(stack) -> P_n; #t2 -> P_next
lw $a0, 4($fp)
move $a1, $t2
jal create
lw $a0, 0($fp)
sw $v0, 4($a0)
li $v0, 0
b insert_exit
insert_first:
lw $a0, 4($fp)
lw $a1, 0($fp)
jal create
insert_exit:
#PROTOCOL END
lw $ra, 16($sp)
lw $fp, 20($sp)
addiu $sp, $sp, 32
jr $ra
print_all: #a0 -> P_first
#PROTOCOL START
subu $sp, $sp, 32
sw $ra, 16($sp)
sw $fp, 20($sp)
addiu $fp, $sp, 24
sw $a0, 0($fp)
lw $a0, 4($a0)
beqz $a0, print_exit
jal print_all
print_exit:
lw $a0, 0($fp)
lw $a0, 0($a0)
li $v0, 1
syscall
la $a0, sep
li $v0, 4
syscall
#PROTOCOL END
lw $ra, 16($sp)
lw $fp, 20($sp)
addiu $sp, $sp, 32
jr $ra