Reputation: 389
I am trying to make a simple password check in nasm, but my program never jumps to correct_func
, but the password is right. Am I doing something wrong?
section .data
msg1: db "Enter your password: ", 0
len1 equ $-msg1
correct: db "Correct!", 0x0a
lenc equ $-correct
wrong: db "Wrong!", 0x0a
lenw equ $-wrong
passwd: db "ASD123"
input
section .text
global _start
correct_func:
mov rax, 0x1
mov rdi, 1
mov rsi, correct
mov rdx, lenc
syscall
mov rax, 60
mov rdi, 0
syscall
ret
_start:
mov rax, 0x1
mov rdi, 1
mov rsi, msg1
mov rdx, len1
syscall
mov rax, 0x0
mov rdi, 0
mov rsi, input
mov rdx, 6
syscall
mov rax, passwd
mov rbx, input
cmp rax, rbx
je correct_func
mov rax, 0x1
mov rdi, 1
mov rsi, wrong
mov rdx, lenw
syscall
mov rax, 60
mov rdi, 0
syscall
Upvotes: 1
Views: 2482
Reputation: 92966
To compare two strings compare each character of one string with the corresponding character of the other. If the two strings have the same length and match in all their characters, they are equal. I'll assume that you checked that the two have the same length.
You can do this with an explicit loop:
mov ecx, 0 ; index and loop counter
.loop: mov al, [passwd+rcx] ; load a character from passwd
cmp al, [input+rcx] ; is it equal to the same character in the input?
jne .incorrect ; if not, the password is incorrect
inc ecx ; advance index
cmp ecx, 6 ; reached the end of the string?
jle .loop ; loop until we do
; if this line is reached, the password was correct
Alternatively, you can use the cmpsb
instruction:
mov rsi, passwd ; load the address of passwd
mov rdi, input ; load the address of input
mov ecx, 6 ; load the string length
repe cmpsb ; compare the strings
je correct_func ; jump to correct_func if they are equal
; if this line is reached, the password was wrong
Refer to the instruction set reference for details.
Upvotes: 3