Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prompt the user and read in a string. Make your string large enough to hold 100

ID: 3771925 • Letter: P

Question

Prompt the user and read in a string. Make your string large enough to hold 100 characters. Assuming there is just one blank between each word, count the number of words in the string. Create a new string which is a copy of the original string, except that the new string is converted to uppercase. All non-letter characters should be unchanged. Convert the letters using difference between the ASCII codes for capital letters and the ASCII codes for small letters. Print the original string, the new string, and the word count. Make sure you print messages to label your output.

When we call syscall to read in a string, we give a length n in $a1. syscall reads in at most n-1 characters and it adds a null byte (which has the value 0x00) to the end of the string. If the string is less than n-1 characters, then syscall adds a newline (which has the value 0xa) after the null byte. To find the end of the string, test for the null byte. You can ignore the newline when counting words and converting to uppercase.

Explanation / Answer

n order to make case-insensitive comparisons, both items being compared need to be in the same case. Only the letters should be converted, of course. By looking at the ASCII code chart, we see the only difference between the upper case and lower case letters is bit 5. To make a letter:

    lower case, force this bit to be 1, by ORing with 00100000 = 0x20
    upper case, force this bit to be 0, by ANDing with 11011111 = 0xDF
    the opposite case, change this bit, by XORing with 00100000 = 0x20

Binary digit to ASCII character


Suppose we believe that register $t0 contains a binary number that can be represented by a single digit, 0..9. (This would be true for the remainder of division by 10.) From the ASCII table, we see that the numeric characters '0' .. '9' are the hex values 0x30 .. 0x39. All we have to do is OR the register with 0x30, and we have the required character, suitable for storing in a string.

                or $t0, 0x30      #binary number to ASCII character

Binary 4 bits to ASCII character for Hexadecimal - '0' .. '9' 'A' .. 'F'


If we want to represent 4 bits as a hex digit in a string, this isn't quite good enough, because the ASCII code 'A" doesnt follow immediately after '9', instead, there is a gap of 7 other characters. The solution is to test and add 7 if necessary. It is also a good idea to make sure only the lowest 4 bits are non-zero. In this coding example, we show what happens to the value 13, in binary 1101, in $t0:

     and   $t0, 0x000f      #00001101 only allow lowest 4 bits non-zero
     or    $t0, 0x30        #00111101
     ble   $t0,'9', numeric #00000111   to be added, since > '9'
        add $t0, 7         #01000100   ASCII 'D'
numeric:

# function to convert word value to a hex string. Returns pointer to the string
#    arguments:
#        input: a0, word to be converted
#        output: v0, pointer to the string
#    registers used:
#        t2 - working copy of word
#        t1 - point to string characters, right to left
#        t0 - single 4-bit value, converting to character, as above

word2hex:
    la     $v0, str8            #string workspace defined in data segment
    add    $t1, $v0, 8          #point to last character position + 1
    move   $t2, $a0             #copy input, will be shifting $t2
hexloop:
      sub    $t1, 1             #point 1 character left
      and    $t0, $t2, 0x000f   #only allow lowest 4 bits non-zero, converting in $t0
      srl    $t2,$t2, 4         #shift copy of word so next 4 bits are on right
      or    $t0, 0x30           #make it numeric ASCII character, but ...
      ble    $t0,'9', numeric   #IF > '9',
        add $t0, 7             #must add 7 to get 'A' .. 'F'
numeric:
      sb     $t0, ($t1)         #store hex character in string
      bne    $t1,$v0, hexloop   #UNTIL all 8 characters stored
    jr     $ra                  #RETURN from function call, $v0 points to string

A fully running version using this code is hex1.asm