I am currently writing a program that takes in a password and checks for strength based on certain criteria, I am having trouble determining if my logic is correct for checking for an uppercase letter.
mov cx, count
mov bx, OFFSET pw
upper_loop:
mov ax, [bx]
cmp dx, count
je upper_msg ; prompt user that no uppercase letter was entered
sub ax, 'A'
cmp ax, 'Z' - 'A'
jle lower_check
inc bx
inc dx
jmp upper_loop
Your program is not correct, it may give false positives when the password contains for instance '@' (0x40) instead of capital letter. Subtracting 'A' from '@' yields -1 (0xFF), which is lower than 'Z'-'A' (0x19), so you jump to lower_check although no uppercase letter has been actually encountered. My proposition is to avoid comparing characters as signed numbers:
mov cx, count
mov si, OFFSET pw
cld
upper_loop:
lodsb
cmp al,'A'
jb not_u
cmp al,'Z'
jbe lower_check ; Break if at least one uppercase letter is present.
not_u:loop upper_loop
upper_msg ; prompt user that no uppercase letter was entered
Sticking to your example, this is the result:
xor dx, dx
mov cx, count
mov bx, OFFSET pw
upper_loop:
mov al, [bx]
cmp dx, count
je upper_msg ; prompt user that no uppercase letter was entered
sub al, 'A'
cmp al, 'Z' - 'A'
jbe lower_check
inc bx
inc dx
jmp upper_loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.