Search code examples
assemblyx86ascii

How can I check if a character is a letter in assembly?


So, I have a block of code which sets the bounders to check if a character is a letter (not numbers, not symbols), but I don't think it works for the characters in between upper and lower case. Can you help? Thanks!

mov al, byte ptr[esi + ecx]; move the first character to al
cmp al, 0                  ; compare al with null which is the end of string
je done                    ; if yes, jump to done
cmp al, 0x41               ; compare al with "A" (upper bounder)
jl next_char               ; jump to next character if less
cmp al, 0x7A               ; compare al with "z" (lower bounder)
jg next_char               ; jump to next character if greater
//do something if it's a letter
next_char:
//do something different

Solution

  • You need to have a logic that combines multiple conditions similar to what would be a "C" statement: if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))

    You can do that like this:

    ...
    je done                    ; if yes, jump to done
    cmp al, 0x41               ; compare al with "A"
    jl next_char               ; jump to next character if less
    cmp al, 0x5A               ; compare al with "Z"
    jle found_letter           ; if al is >= "A" && <= "Z" -> found a letter
    cmp al, 0x61               ; compare al with "a"
    jl next_char               ; jump to next character if less (since it's between "Z" & "a")
    cmp al, 0x7A               ; compare al with "z"
    jg next_char               ; above "Z" -> not a character
    found_letter:
    // ...
    next_char:
    // ...