# 3.12: Exercises

- Page ID
- 28493

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

- Which of the following operators are pseudo operators? What are the translations of the pseudo operators to real instructions?
- add $t0, $t1, $t2
- add $t0, $t1, 100
- addi $t0, $t1, 100
- subi $t0, $t1, 100
- mult $t0, $t1
- mult $t0, $t1, $t2
- rol $t0, $t1, 3

- 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.
- 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.
- Do the following two problems.
- 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.
- Implement the AND, OR, and NOT operations using only the MIPS
`nor`

operator. Do the same thing using NAND.

- 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. - 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. - 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.
- Can you implement division by 15 using bit shift operations? Why or why not?
- 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? - Assemble a
`mulo`

instruction in MARS. Explain the resultant code in the Basic column of the Execute screen. - Implement the
`rem`

operator using only the`div`

,`mfhi`

, and`mflo`

operators. - 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.
- 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.**- 5x + 3y +z
- ((5x + 3y + z) / 2) * 3
- x
^{3}+2x^{2}+3x+4 - (4x / 3) * y

- 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

- 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.
- 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.
- 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.

- Explain the different between a constant value and an immediate value.
- 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 = "