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
|
.386
.model flat, stdcall
option casemap : none
include \masm32\include\masm32rt.inc
.data
array db 2, 5, 6, 3, 7, 1, 2, 8 ;tableau à trier
array_ok db 1
.code
start:
mov ebx, 0
routine:
mov array_ok, 1 ;on suppose le tableau trié
;on vérifie si on se trouve à la fin du tableau, si oui on se place au début (end_of_array)
mov eax, lengthof array
sub eax, 1
cmp ebx, eax
je end_of_array
;si la case actuelle a une valeur supérieur à celle de la case suivante...
movzx eax, array[ebx]
movzx edx, array[ebx + 1]
cmp eax, edx
jg changer ;on interverti les valeurs des deux cases
;sinon si on est à la fin du tableau, on vérifi si il est trié (array_ok = 1)
mov eax, lengthof array
sub eax, 1
cmp ebx, eax
je verif_array
inc ebx ;si on est pas à la fin, on incrémente ebx et on retourne au début de la routine
jmp routine
changer:
;on sauvegarde les valeurs des 2 cases à modifier dans ecx et edx
movzx ecx, array[ebx]
movzx edx, array[ebx + 1]
;on enresgistre les adresses des 2 cases à mettre à jour
movzx eax, BYTE PTR [array[ebx]]
movzx edi, BYTE PTR [array[ebx + 1]]
;on met à jour les 2 cases
mov [eax], edx
mov [edi], ecx
;on a du faire un changement, le tableau n'est donc toujours pas trié -> array_ok = 0
mov array_ok, 0
inc ebx ;on avance d'une case dans le tableau
jmp routine
verif_array:
;si le tableau est trié (array_ok = 1) on affiche
cmp array_ok, 1
je affichage
jmp routine
end_of_array:
mov ebx, 0
jmp routine
;--- AFFICHAGE ---------------------------------
affichage:
mov ebx, 0
tab:
movzx edx, array[ebx]
print str$ (edx), 13, 10
mov eax, lengthof array
sub eax, 1 ;on soustrait 1 de la taille du tableau
cmp ebx, eax
je fin ;si on arrive à la fin du tableau on quitte la boucle
inc ebx ;sinon on incrémente ebx
jmp tab
fin :
inkey "continuer..."
exit ;rend la main à l'OS
end start |
Partager