Search code examples
assemblyx86-16case-insensitiveemu8086

Compare for equal to uppercase and lowercase versions of the same letter


Hello currently I'm doing a project in asm language and I came up with this code:

mov ah, 07h 
int 21h 
mov bl,al     

cmp bl, 'w'
je  up
cmp bl, 'W'
je  up 

The code is about entering a letter and jumping to another function. The thing I want to do is to compare it even if it is in either uppercase or lowercase. Is there a way to do it?


Solution

  • Because the uppercase letters [A,Z] (ASCII codes [65,90]) differ from the lowercase letters [a,z] (ASCII codes [97,122]) by 32, and because of how the ASCII table is organised, all the lowercase letters have their bit5 set while none of the uppercase letters have their bit5 set.

    Make it case-insensitive

    Before comparing you can or the character code with 32, and then you'll need just one comparison.

    mov ah, 07h 
    int 21h 
    mov bl, al     
    
    or  al, 32     ; Make case-insensitive
    cmp al, 'w'    ; Only comparing lowercase, but accepting both cases
    je  up
    

    What this or al, 32 instruction does is:

    • if AL is [A,Z] it becomes [a,z]
    • if AL is [a,z] it remains [a,z]