# 5.11: Exercises

1. There are many algorithms presented in this text that would lend themselves to be included as subprograms in the utils.asm file. Implement some or all of the following into the utils.asm file, properly documenting them, and include programs to test them.
1. NOR subprogram - take two input parameters, and return the NOR operation on those two parameter.
2. NAND- take two input parameters, and return the NAND operation on those two parameter.
3. NOT- take one input parameters, and return the NOT operation on that parameter.
4. Mult4 - take an input parameter, and return that parameter multiplied by 4 using only shift and add operations.
5. Mult10 - take an input parameter, and return that parameter multiplied by 10 using only shift and add operations.
6. Swap- take two input parameters, swap them using only the XOR operation.
7. RightCircularShift - take an input parameter, and return two values. The first is the value that has been shifted 1 bit using a right circular shift, and the second is the value of the bit which has been shifted.
8. LeftCircularShift - take an input parameter, and return two values. The first is the value that has been shifted 1 bit using a left circular shift, and the second is the value of the bit which has been shifted.
9. ToUpper - take a 32 bit input which is 3 characters and a null, or a 3 character string. Convert the 3 characters to upper case if they are lower case, or do nothing if they are already upper case.
10. ToLower - take a 32 bit input which is 3 characters and a null, or a 3 character string. Convert the 3 characters to lower case if they are upper case, or do nothing if they are already lower case.
2. A colleague decided that the PrintInt subprogram should print a new line after the integer has been printed, and has modified the PrintInt to print a new line character as follows.
# subprogram:    PrintInt
# author:        Charles W. Kann
# purpose:       To print a string to the console
# input:         $a0 - The address of the string to print. #$a1 - The value of the int to print
# returns: None
# side effects: The String is printed followed by the integer value.
.text
PrintInt:
# Print string. The string address is already in $a0 li$v0, 4
syscall

# Print integer. The integer value is in $a1, and must # be first moved to$a0.
move $a0,$a1
li $v0, 1 syscall # Print a new line character jal PrintNewLine #return jr$ra

When the program is run, it never ends and acts like it is stuck in an infinite loop. Help this colleague figure out what is wrong with the program.

1. Explain what is happening in the program
2. Come up with a mechanism which shows that this program is indeed in an infinite loop.
3. Come up with a solution which fixes this problem.
3. Someone has modified the utils.asm file to insert a PrintTab subprogram immediately after the PrintNewLine subprogram as shown below (changes are highlighted in yellow). The programmer complains that the PrintTab command cannot be called using the "jal PrintTab" instruction. What is wrong, and how can this be fixed? Explain all the problems in the code this programmer has written.
# subprogram:    PrintNewLine
# author:        Charles Kann
# purpose:       to output a new line to the user console
# input:         None
# output:        None
# side effects:  A new line character is printed to the user's console
.text
PrintNewLine:
li $v0, 4 la$a0, __PNL_newline
syscall
jr $ra .data __PNL_newline: .asciiz "\n" PrintTab: li$v0, 4
la $a0, tab syscall jr$ra
.data
tab: .asciiz "\t"