7.9: Exercises
- Page ID
- 57690
\( \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}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Below are some quiz questions and suggested projects based on this chapter.
Questions
Below are some quiz questions based on this chapter.
1) Which of the following instructions is legal / illegal? As appropriate, provide an explanation.
1. mov rax, 54 2. mov ax, 54 3. mov al, 354 4. mov rax, r11 5. mov rax, r11d 6. mov 54, ecx 7. mov rax, qword [qVar] 8. mov rax, qword [bVar] 9. mov rax, [qVar] 10. mov rax, qVar 11. mov eax, dword [bVar] 12. mov qword [qVar2], qword [qVar1] 13. mov qword [bVar2], qword [qVar1] 14. mov r15, 54 15. mov r16, 54 16. mov r11b, 54
2) Explain what each of the following instructions does.
1. movzx rsi, byte [bVar1] 2. movsx rsi, byte [bVar1]
3) What instruction is used to:
- convert an unsigned byte in al into a word in ax.
- convert a signed byte in al into a word in ax.
4) What instruction is used to:
- convert an unsigned word in ax into a double-word in eax.
- convert a signed word in ax into a double-word in eax.
5) What instruction is used to:
- convert an unsigned word in ax into a double-word in dx:ax.
- convert a signed word in ax into a double-word in dx:ax.
6) Explain the difference between the cwd instruction and the movsx instructions.
7) Explain why the explicit type specification (dword in this example) is required on the first instruction and is not required on the second instruction.
1. add dword [dVar], 1 2. add [dVar], eax
8) Given the following code fragment:
mov rax, 9 mov rbx, 2 add rbx, rax
What would be in the rax and rbx registers after execution? Show answer in hex, full register size.
9) Given the following code fragment:
mov rax, 9 mov rbx, 2 sub rax, rbx
What would be in the rax and rbx registers after execution? Show answer in hex, full register size.
10) Given the following code fragment:
mov rax, 9 mov rbx, 2 sub rbx, rax
What would be in the rax and rbx registers after execution? Show answer in hex, full register size.
11) Given the following code fragment:
mov rax, 4 mov rbx, 3 imu1 rbx
What would be in the rax and rdx registers after execution? Show answer in hex, full register size.
12) Given the following code fragment:
mov rax, 5 cqo mov rbx, 3 idiv rbx
What would be in the rax and rdx registers after execution? Show answer in hex, full register size.
13) Given the following code fragment:
mov rax, 11 cqo mov rbx, 4 idiv rbx
What would be in the rax and rdx registers after execution? Show answer in hex, full register size.
14) Explain why each of the following statements will not work.
1. mov 42, eax 2. div 3 3. mov dword [num1], dword [num1] 4. mov dword [ax], 800
15) Explain why the following code fragment will not work correctly.
mov eax, 500 mov ebx, 10 idiv ebx
16) Explain why the following code fragment will not work correctly.
mov eax, -500 cdq mov ebx, 10 div ebx
17) Explain why the following code fragment will not work correctly.
mov ax, -500 cwd mov bx, 10 idiv bx mov dword [ans], eax
18) Under what circumstances can the three-operand multiple be used?
Suggested Projects
Below are some suggested projects based on this chapter.
1) Create a program to compute the following expressions using unsigned byte variables and unsigned operations. Note, the first letter of the variable name denotes the size (b → byte and w → word).
Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
- bAns1 = bNum1 + bNum2
- bAns2 = bNum1 + bNum3
- bAns3 = bNum3 + bNum4
- bAns6 = bNum1 – bNum2
- bAns7 = bNum1 – bNum3
- bAns8 = bNum2 – bNum4
- wAns11 = bNum1 * bNum3
- wAns12 = bNum2 * bNum2
- wAns13 = bNum2 * bNum4
- bAns16 = bNum1 / bNum2
- bAns17 = bNum3 / bNum4
- bAns18 = wNum1 / bNum4
- bRem18 = wNum1 % bNum4
2) Repeat the previous program using signed values and signed operations. Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
3) Create a program to complete the following expressions using unsigned word sized variables. Note, the first letter of the variable name denotes the size (w → word and d → double-word).
- wAns1 = wNum1 + wNum2
- wAns2 = wNum1 + wNum3
- wAns3 = wNum3 + wNum4
- wAns6 = wNum1 – wNum2
- wAns7 = wNum1 – wNum3
- wAns8 = wNum2 – wNum4
- dAns11 = wNum1 * wNum3
- dAns12 = wNum2 * wNum2
- dAns13 = wNum2 * wNum4
- wAns16 = wNum1 / wNum2
- wAns17 = wNum3 / wNum4
- wAns18 = dNum1 / wNum4
- wRem18 = dNum1 % wNum4
Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
4) Repeat the previous program using signed values and signed operations. Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
5) Create a program to complete the following expressions using unsigned double- word sized variables. Note, the first letter of the variable name denotes the size (d → double-word and q → quadword).
- dAns1 = dNum1 + dNum2
- dAns2 = dNum1 + dNum3
- dAns3 = dNum3 + dNum4
- dAns6 = dNum1 – dNum2
- dAns7 = dNum1 – dNum3
- dAns8 = dNum2 – dNum4
- qAns11 = dNum1 * dNum3
- qAns12 = dNum2 * dNum2
- qAns13 = dNum2 * dNum4
- dAns16 = dNum1 / dNum2
- dAns17 = dNum3 / dNum4
12. dAns18 = qNum1 / dNum4
13. dRem18 = qNum1 % dNum4
Use the debugger to execute the program and display the final results. Create a
debugger input file to show the results in both decimal and hexadecimal.
6) Repeat the previous program using signed values and signed operations. Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
7) Implement the example program to compute the sum of squares from 1 to n. Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
8) Create a program to compute the square of the sum from 1 to n. Specifically, compute the sum of integers from 1 to n and then square the value. Use the debugger to execute the program and display the final results. Create a debugger input file to show the results in both decimal and hexadecimal.
9) Create a program to iteratively find the nth Fibonacci number(For more information, refer to: http://en.Wikipedia.org/wiki/Fibonacci_number). The value for n should be set as a parameter (e.g., a programmer defined constant). The formula for computing Fibonacci is as follows:
\[fibonacci(n) = \begin{cases} n & \text{if } n = 0 \text{ or } n = 1 \\ fibonacci (n - 2) + fibonacci (n - 1) & \text{if } n \ge 2 \end{cases}\nonumber\]
Use the debugger to execute the program and display the final results. Test the program for various values of n. Create a debugger input file to show the results in both decimal and hexadecimal.