简体   繁体   中英

In x8086 Assembly Language, how do I check if a char is an uppercase letter?

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
  • I don't see that you cleared DX before running the loop.
  • You need to retrieve bytes from the string since you clearly raise its pointer by just 1 on each iteration.
  • You must interpret the result of the comparison in an unsigned way or else all the ASCII's from 0 to 64 would be considered uppercase letters!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM