# 3.12: Exercises

• • Charles W. Kann III
• Adjunct Professor (Computer Science) at Gettysburg College

1. Which of the following operators are pseudo operators? What are the translations of the pseudo operators to real instructions?
1. add $t0,$t1, $t2 2. add$t0, $t1, 100 3. addi$t0, $t1, 100 4. subi$t0, $t1, 100 5. mult$t0, $t1 6. mult$t0, $t1,$t2
7. rol $t0,$t1, 3
2. Implement a program to do a bitwise complement (NOT) of an integer number entered by the user. You should use the XOR operator to implement the NOT, do not use the NOT operator. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
3. Implement a program to calculate the 2's complement of a number entered by the user. The program should only user the XOR and ADD operators. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
4. Do the following two problems.
1. Implement a simple program to do a bitwise NAND in MARS. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
2. Implement the AND, OR, and NOT operations using only the MIPS nor operator. Do the same thing using NAND.
5. Implement a program which multiplies a user input by 10 using only bit shift operations and addition. Check to see if your program is correct by using the mult and mflo operators. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
6. Implement a program which multiplies a user input by 15 using only bit shift operations and addition. Check to see if your program is correct by using the mult and mflo operators. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
7. Implement a program which divides a user input by 8 using only bit shift. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
8. Can you implement division by 15 using bit shift operations? Why or why not?
9. Implement an overflow check for multiplication where the two numbers being multiplied are both always positive. Why is this simpler than the generic check implement in the mulo operator?
10. Assemble a mulo instruction in MARS. Explain the resultant code in the Basic column of the Execute screen.
11. Implement the rem operator using only the div, mfhi, and mflo operators.
12. Implement a program to prompt the user for two numbers, the first being any number and the second a prime number. Return to the user a 0 if the second number is a prime factor for the first, or any number if it is not. For example, if the user enter 60 and 5, the program returns 0. If the user enters 62 and 5, the program returns 2.
13. Write programs to evaluate the following expressions. The user should enter the variables, and the program should print back an answer. Prompt the user for all variables in the expression, and print the results in a meaningful manner. The results should be as accurate as possible.
1. 5x + 3y +z
2. ((5x + 3y + z) / 2) * 3
3. x3+2x2 +3x+4
4. (4x / 3) * y
14. Enter the following two instructions in MARS, and assemble them. What differences do you notice? Are the results the same? Will one run faster than the other? Explain what you observe.
addiu $t0,$zero, 60000
ori   $t0,$zero, 60000

15. Write a program to retrieve two numbers from a user, and swap those number using only the XOR operation. You should not use a temporary variable to store the numbers while swapping them. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
16. Using only sll and srl, implement a program to check if a user input value is even or odd. The result should print out 0 if the number is even or 1 if the number is odd. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.
17. The ideal gas law allows the calculation of volume of a gas given the pressure(P), amount of the gas (n), and the temperature (T). The equation is:

V = nRT / P

Since we only have used integer arithmetic, all numbers will be integer values with no decimal points. The constant R is 8.314 and will be specified as (8314/1000). This gives the same result. Implement the idea gas law program where the user is prompted for and enters values for n, T, and P, and V is calculated and printed out. Be careful to implement an accurate version of this program. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner.

18. Explain the different between a constant value and an immediate value.
19. Correct the following programs.
Program 1

.text
main:
li $v0, 4 la$a0, result1
syscall
li $v0, 1 li$a0, 4
syscall

li $v0, 4 la$a0, result2
syscall
li $v0, 1 li$a0, 8
syscall

addi $v0,$zero, 10 #Exit program
syscall
.data
result1: .ascii "\nfirst value = "
result2: .ascii "\nsecond value = "

Program 2

.text
main:
li $v0, 4 la$a0, result1
syscall
li $v0, 4 li$a0, 4
syscall

li $v0, 4 la$a0, result2
syscall
li $v0, 1 li$a0, 8
syscall

addi $v0,$zero, 10 #Exit program
syscall
.data
result1: .asciiz "\nfirst value = "
result2: .asciiz "\nsecond value = "

Program 3

.text
main:
li $v0, 4 la$a0, result1
syscall
li $v0, 4 li$a0, 4
syscall

li $v0, 4 la$a0, result2
syscall
li $v0, 1 li$a0, 8
syscall

addi $v0,$zero, 10 #Exit program
syscall
result1: .ascii "\nfirst value = "
result2: .ascii "\nsecond value = "