Comparatif de chaînes de caractères
Scénario 1 : Recherche d'une chaîne dans une autre
2 chaînes de caractères sont entrées dans le code du programme.
La première est le mot 'hello', la seconde est une chaîne aléatoire contenant le mot 'hello'
Le programme doit dire si la chaîne de caractères 1 (hello) est présente dans la 2.
Si oui, le programme répond 'FOUND'
Si non (si vous remplacez le mot hello par autre chose), le programme répond 'NOT FOUND'
Je tiens tout spécialement à remercier 'omais' pour avoir trouvé la solution à ce premier scénario.
Voici le programme search.asm
%define SYS_WRITE 4
%define SYS_EXIT 1
;;; file ids
%define STDOUT 1
; cat search.asm
section .data
str1 db 'hello', 0
str2 db 'z vmermbzmev,emfb mzinv zinvlienblginbilnb zaeb enb leinrqfv hello zv elibn lienrli n', 0
found db 'FOUND',10, 0
found_len equ $ - found
notfound db 'NOT FOUND',10, 0
notfound_len equ $ - notfound
section .text
global _start
_start:
mov rsi, str1
mov rdi, str2
mov rax, 0
mov rcx, 0
search:
mov rbx, 0
compare:
mov al, [rsi + rbx]
mov ah, [rdi + rcx]
cmp al, ah
je check_find
cmp ah, 0
je notfound_msg
inc rcx
jmp compare
check_find:
inc rbx
inc rcx
mov al, [rsi + rbx]
mov ah, [rdi + rcx]
cmp al, 0
je found_msg
cmp al, ah
jne search
jmp check_find
found_msg:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, found
mov rdx, found_len
syscall
jmp exit
notfound_msg:
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, notfound
mov rdx, notfound_len
syscall
exit:
mov rax, SYS_EXIT ; exit syscall
mov rdi, 0 ; exit code 0 (= 'ok')
syscall ; kernel interrupt: system cal
Assembler
nasm -f elf64 search.asm -o search.o
Linker
ld search.o -o search
Lancer le programme
./search
Scénario 2 : Mot magique avec saisie utilisateur
Le but est de demander à l'utilisateur d'entrer un mot magique.
- Si le mot entré correspond à ce qui est attendu, le programme répond 'BRAVO' et quitte
- Si le mot entré ne correspond pas, le programme répond 'ERREUR' et quitte
Voici le programme motmagique.asm
section .data
prompt_message db 'Entrez un mot: ', 0
prompt_len equ $-prompt_message
magic_word db 'magique', 0
magic_len equ $-magic_word
bravo_message db 'BRAVO', 10
bravo_len equ $-bravo_message
error_message db 'ERREUR', 10
error_len equ $-error_message
section .bss
input_buffer resb 50
section .text
global _start
_start:
; Affichage du message d'invite
mov rax, 4
mov rdi, 1
mov rsi, prompt_message
mov rdx, prompt_len
syscall
; Lecture de l'entrée utilisateur
mov rax, 3
mov rdi, 0
mov rsi, input_buffer
mov rdx, 50
syscall
; Stocker la longueur réelle lue dans rax, et soustraire 1 pour enlever le newline
sub rax, 1
; Comparer l'entrée avec 'magique'
mov rdi, input_buffer
mov rsi, magic_word
mov rcx, rax ; Utiliser la longueur ajustée
call compare_strings
; Décider sur la base du résultat de la comparaison
test rax, rax
jz magic_matched
jmp error_occurred
magic_matched:
; Affichage de 'BRAVO'
mov rax, 4
mov rdi, 1
mov rsi, bravo_message
mov rdx, bravo_len
syscall
jmp exit
error_occurred:
; Affichage de 'ERREUR'
mov rax, 4
mov rdi, 1
mov rsi, error_message
mov rdx, error_len
syscall
exit:
; Sortie du programme
mov rax, 1
xor rdi, rdi
syscall
compare_strings:
; Comparaison caractère par caractère
repe cmpsb
jecxz equal ; Sauter si tous les caractères ont été comparés et sont égaux
mov rax, 1
ret
equal:
xor rax, rax ; Résultat zéro indique une égalité
ret
Assembler
nasm -f elf64 motmagique.asm -o motmagique.o
Linker
ld motmagique.o -o motmagique
Lancer le programme
./motmagique
↑ Haut de page