Search code examples
assemblymipsmips32

Assembly Language - Display Integer based on cases of characters


so quick question. I have a program that accepts two characters (letters) from a user - all I have to do is output an integer representing a comparison of the case of these two letters. 0-3 are the possible output, and below are the examples.

  • If the user enters two uppercase (ie 'A' 'B') - the output should be 0.
  • If the user enters first uppercase and next lowercase (ie 'A' 'b') - the output should be 1.
  • If the user enters first lowercase and next uppercase (ie 'a' 'B') - the output should be 2.
  • If the user enters two lowercase (ie 'a' 'b') - the output should be 3.

I somehow have to do this using only bit manipulation (so I guess and, or, nor, etc., and in less than 8 lines of code. I'm guessing I have to do some kind of comparison between the 6th bit of the characters (since that is the one that determines the case of the character) - however I have no idea how to relay that logic to output the correct integer.


Solution

  • This question has all the signs of being a class assignment, so based on that, I'm not going to provide a complete answer, but provide snippets of code to enable the original questioner to develop their understanding and solve the problem themselves.

    # Assuming $0 contains character 1, and $1 contains character 2
    # Then this will get the case of the two letters into $2 and $3
    # doing a logical AND on bit 5
    andi $2,$0,0x20
    andi $3,$1,0x20
    

    and

    # To move the bits to the right place for the answer
    # The key here is to realise that the first letter needs to be at
    # bit0 and the second letter needs to be at bit1
    srl $4,$2,0x05
    srl $5,$3,0x04
    

    and

    # Combine the results into register 6
    # with bit 1 from 1st letter and bit 2 from the second letter.
    and $6,$4,$5
    

    Now, all you need to do it to decide if both bits are the same, if so, then you need to NOT both bits. and you have your answer.

    Now I have shown some examples in assembler you should be able to do the comparison and conditional jump bits yourself.