X86 NASM Assembly converting lower to upper and upper to lowercase characters

后端 未结 5 1473
失恋的感觉
失恋的感觉 2020-12-20 06:02

As i am pretty new to assembly, i have a few questions in regards to how i should convert from a lowercase to an uppercase if the user enters an uppercase letter or vice ver

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 06:29

    If you only support ASCII, then you can force lowercase using an OR 0x20

      or   eax, 0x20
    

    Similarly, you can transform a letter to uppercase by clearing that bit:

      and  eax, 0xBF   ; or use ~0x20
    

    And as nneonneo mentioned, the character case can be swapped using the XOR instruction:

      xor  eax, 0x20
    

    That only works if eax is between 'a' and 'z' or 'A' and 'Z', so you'd have to compare and make sure you are in the range:

      cmp  eax, 'a'
      jl   .not-lower
      cmp  eax, 'z'
      jg   .not-lower
      or   eax, 0x20
    .not-lower:
    

    I used nasm syntax. You may want to make sure the jl and jg are correct too...

    If you need to transform any international character, then that's a lot more complicated unless you can call a libc tolower() or toupper() function that accept Unicode characters.


    As a fair question: why would it work? (asked by kuhaku)

    ASCII characters (also ISO-8859-1) have the basic uppercase characters defined between 0x41 and 0x5A and the lowercase characters between 0x61 and 0x7A.

    To force 4 into 6 and 5 into 7, you force bit 5 (0x20) to be set.

    To go to uppercase, you do the opposite, you remove bit 5 so it becomes zero.

提交回复
热议问题