Cmp byte [eax], 0

Résolu/Fermé
33AMELIA Messages postés 9 Date d'inscription jeudi 24 août 2017 Statut Membre Dernière intervention 29 avril 2021 - 29 avril 2021 à 18:13
Dalfab Messages postés 706 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 2 novembre 2023 - 30 avril 2021 à 15:06
Bonjour,

Je débute avec l'assembleur. Ce code détermine la longueur de la chaine "Hello, brave new world!". Voici le code en entier.


SECTION .data
msg db 'Hello, brave new world!', 0Ah
SECTION .text
global _start

_start:

mov ebx, msg ; move the address of our message string into EBX
mov eax, ebx ; move the address in EBX into EAX as well (Both now point to the same segment in memory)

nextchar:
cmp byte [eax], 0 ; compare the byte pointed to by EAX at this address against zero (Zero is an end of string delimiter)
jz finished ; jump (if the zero flagged has been set) to the point in the code labeled 'finished'
inc eax ; increment the address in EAX by one byte (if the zero flagged has NOT been set)
jmp nextchar ; jump to the point in the code labeled 'nextchar'

finished:
sub eax, ebx ; subtract the address in EBX from the address in EAX
; remember both registers started pointing to the same address (see line 15)
; but EAX has been incremented one byte for each character in the message string
; when you subtract one memory address from another of the same type
; the result is number of segments between them - in this case the number of bytes

mov edx, eax ; EAX now equals the number of bytes in our string
mov ecx, msg ; the rest of the code should be familiar now
mov ebx, 1
mov eax, 4
int 80h

mov ebx, 0
mov eax, 1
int 80h


Je n'arrive pas à comprendre ce que fait
cmp byte [eax], 0 
. Je sais que EAX devrait tout comme EBX pointer sur notre chaine donc contenir son adresse et que 0 symbolise la fin de chaine. Que signifie byte [EAX] et pourquoi le compare le comparer à 0 ?

1 réponse

Dalfab Messages postés 706 Date d'inscription dimanche 7 février 2016 Statut Membre Dernière intervention 2 novembre 2023 101
30 avril 2021 à 15:06
Bonjour,

Tu as toutes les infos pour comprendre. Peut-être est-ce la syntaxe qui te perturbe?
byte [eax]
désigne la valeur l'octet (
byte
) qui est pointé (
[ ]
) par la valeur de
eax
. On veut vérifier si à ce moment la valeur de
eax
correspond à la fin de la chaîne de caractères (ici caractère<=>byte<=>octet).
Et si c'est le cas, on termine le traitement (c'est l'instruction suivante.)
1