assembly - toUpper/toLower in mips -
i need write program using mips take string , switch uppercase letters lower case , lowercase letters uppercase. instinct write if statements using letter numerical values wondering if there better way go this.
yes, there better way of doing this! ascii values of corresponding upper , lower case alphabetical characters differ 0x20. instance, 'a' = 0x41 , 'a' = 0x61. basically, sixth bit set lower case characters, , cleared upper case.
the simplest implementation uses bit-bashing trick - if can identify character letter, can upcase using:
ch &= ~0x20; or downcase with:
ch |= 0x20; remember, won't work other alphabetic ascii characters. can check whether character matches (ch >= 'a' && ch <= 'z') || (ch >= 'a' && ch <= 'z').
another approach avoids if entirely build 256-entry table consisting of intended output each character, , indexing each character. may slightly more efficient, depending on processor, @ expense of being little more annoying write out (or generate).
Comments
Post a Comment