-
Notifications
You must be signed in to change notification settings - Fork 0
/
TP1_INF4170_P1.asm
108 lines (92 loc) · 2.77 KB
/
TP1_INF4170_P1.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
# Auteurs : Patrick Nolin NOLP18059508
# Emanuel Gonthier GONE27069202
.data
msg_t: .asciiz "Veuillez entrer le taille de votre tableau (entre 1 et 20): "
msg_t_err: .asciiz "\nTaille invalide.\n"
msg_e: .asciiz "Veuillez entrer les éléments du tableau : "
.text
main: # initialisation du tableau
li $v0,4
la $a0, msg_t
syscall
addi $v0,$0,5 # recupérer saisi utilisateur
syscall
add $s1,$0,$v0 # nb element
sle $s2, $s1, $0 # Est-ce que la saisie est 0 ou moins?
bne $s2, 1, trop
li $v0, 4
la $a0, msg_t_err # Affichage d'un message d'erreur pour une taille invalide.
syscall
j main # On redemande la taille du tableau.
trop: sgt $s2, $s1, 20 # Est-ce que la saisie est plus de 20?
bne $s2, 1, ok
li $v0, 4
la $a0, msg_t_err # Affichage d'un message d'erreur pour une taille invalide.
syscall
j main # On redemande la taille du tableau.
ok: lui $s0,0x1004 # adresse de base du tableau NE PAS MODIFIER
ori $s0,$s0,0x0000 # charger adresse tableau
addi $s2,$0,0 # compteur
li $v0,4
la $a0, msg_e
syscall
add $t0, $t0, $s0 # charger adresse tableau dans $t0
loop1: beq $s1,$s2,done1 # nb elem == compteur => done
addi $v0,$0,5 # recupérer saisi utilisateur
syscall
sw $v0,0($t0) #mettre valeur dans la case
addi $t0,$t0,4 #déplacement d'une case
addi $s2,$s2,1 #incrementer compteur
j loop1
done1: #test de swap
addi $a0,$0, 1 # i = 1
addi $a1,$0, 2 # j = 2
jal swap
#test de getLeftChildIndex
addi $a0,$0, 1 # index = 1
jal gLci
add $t0,$0,$v0
li $v0, 1
add $a0,$t0,$zero
syscall
#test de getRightChildIndex
addi $a0,$0, 1 # index = 1
jal gRci
add $t0,$0,$v0
li $v0, 1
add $a0,$t0,$zero
syscall
#terminer le programme
addi $v0, $0, 10
syscall
# void swap(int i, int j) :échange le contenu de deux éléments du tableau avec les indices i et j
swap: # enregistrer sur la pile les registre utilisé dans la fonction
addi $sp, $sp, -4
sw $s0, 0($sp)
#nb déplacement
sll $t0,$a0,2 # déplacement i
add $t0,$t0,$s0 # adresse de a[i] = deplacement i + adresse de base du tableau
sll $t1,$a1,2 # déplacement j
add $t1,$t1,$s0 # adresse de a[j] = deplacement j + adresse de base du tableau
#récuperer valeurs
lw $t2,0($t0) #int temp = a[i]
lw $t3,0($t1) #int temp2 = a[j]
# swap
sw $t3,0($t0) #a[i] = temp2
sw $t2,0($t1) #a[j] = temp
# restorer les registres
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
# int getLeftChildIndex(int index) : retourne l'indice de l'enfant de gauche
gLci: addi $t0,$0,0 # $t0 = 0
sll $t0,$a0,1 # index * 2
addi $t0,$t0,1 # index + 1
add $v0,$0,$t0 # return
jr $ra
# int getRightChildIndex(int index) : retourne l'indice de l'enfant de droite
gRci: addi $t0,$0,0 # $t0 = 0
sll $t0,$a0,1 # index * 2
addi $t0,$t0,2 # index + 2
add $v0,$0,$t0 # return
jr $ra