Compare a string in Assembly x86

Asked

Viewed 329 times

1

Every time I type the letter "F" or "f" on the console, it will always say that I did not type the letter "f". Why?

section .data:
  msg: db "Digite a letra F: "
  msgLen: equ $- msg

  msgEqual: db "Voce digitou a letra F"
  msgEqualLen: equ $- msgEqual

  msgNotEqual: db "Voce nao digitou a letra F"
  msgNotEqualLen: equ $- msgNotEqual

section .bss
  letra resb 2

section .text:
  global _start

  _start:
    ; Escrever menssagem principal na tela
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, msgLen
    int 0x80
    ; Entrada do teclado
    mov eax, 3
    mov ebx, 1
    mov ecx, letra
    mov edx, 2
    int 0x80

    cmp ecx, "f"
    je .equal
    jne .notEqual

    .equal:
      ; printar a resposta na tela(se for igual)
      mov eax, 4
      mov ebx, 1
      mov ecx, msgEqual
      mov edx, msgEqualLen
      int 0x80
      ; finalizar a aplicação(como se fosse um return)
      mov eax, 1
      mov ebx, 0
      int 0x80

    .notEqual:
      ; printar a resposta na tela(se não for igual)
      mov eax, 4
      mov ebx, 1
      mov ecx, msgNotEqual
      mov edx, msgNotEqualLen
      int 0x80
      ; return
      mov eax, 1
      mov ebx, 0
      int 0x80

1 answer

0


On the line:

cmp ecx, "f"

The program is comparing the value stored in the register ecx with the letter f, however, after the system call sys_read, this register (still) contains the address of the variable "letter" and not the character typed in the entry by the user.

The entered value is stored in the memory address pointed by "letter", therefore it is necessary to indicate that the comparison takes place between the letter "f" and the value stored in that memory position ("letter"), and that this value has the size of 1 byte.

To fix the program, just change the line above by:

cmp byte [ecx], "f"

Another important point is that the program compares the code of the typed character with the code of the letter "f" lowercase (0x66) and, if the entry is "F" uppercase (0x46), the message "You did not type the letter F".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.