AppendixDSolutions to Selected Exercises¶ permalink
Exercises2.1.1Exercises
Exercise2
Answer\(\binary{1000} \; \binary{0011} \; \binary{1010} \; \binary{1111}\)
\(\binary{1001} \; \binary{0000} \; \binary{0000} \; \binary{0001}\)
\(\binary{1010} \; \binary{1010} \; \binary{1010} \; \binary{1010}\)
\(\binary{0101} \; \binary{0101} \; \binary{0101} \; \binary{0101}\)
Exercise3
Answer\(32\)
\(48\)
\(4\)
\(16\)
Exercise4
Answer\(2\)
\(8\)
\(16\)
\(3\)
\(5\)
\(2\)
Exercises2.2.1Exercises
Exercise1
AnswerDecimal number: \(r = 10, n = 8, d_{7} = 2, d_{6} = 9, d_{5} = 4, d_{4} = 5, d_{3} = 8, d_{2} = 2, d_{1} = 5, d_{0} = 4\)
Hexadecimal number: \(r = 16, n = 8, d_{7} = 2, d_{6} = 9, d_{5} = 4, d_{4} = 5, d_{3} = 8, d_{2} = 2, d_{1} = 5, d_{0} = 4\)
Exercise2
Answer\begin{align*} \binary{1010} \; \binary{0101}_{2} &= 1 \times 2^{7} + 0 \times 2^{6} + 1 \times 2^{5} + 0 \times 2^{4} + 0 \times 2^{3} + 1 \times 2^{2} + 0 \times 2^{1} + 1 \times 2^{0}\\ &= 128 + 0 + 32 + 0 + 0 + 4 + 0 + 1 \\ &= 165_{10} \end{align*}
Exercise3
Answer\(170\)
\(85\)
\(240\)
\(15\)
\(128\)
\(99\)
\(123\)
\(255\)
Exercise4
Answer\(43981\)
\(4660\)
\(65244\)
\(2015\)
\(32768\)
\(1024\)
\(32170\)
\(12345\)
Exercise5
AnswerSet \(Result = 0\)

For \(i = 0, \cdots, (n1)\)
\(Result = Result + d_{i} * 16^{i}\)
\(40960\)
\(65535\)
\(1024\)
\(4369\)
\(34952\)
\(400\)
\(43981\)
\(21845\)
Exercises2.3.1Exercises
Exercise1
Answer\(\binary{1111011} = \hex{7d}\)
Exercise2
Answer\(\hex{64}\)
\(\hex{7d}\)
\(\hex{0a}\)
\(\hex{58}\)
\(\hex{ff}\)
\(\hex{10}\)
\(\hex{20}\)
\(\hex{80}\)
Exercise3
Answer\(\hex{0400}\)
\(\hex{03e8}\)
\(\hex{8000}\)
\(\hex{7fff}\)
\(\hex{0100}\)
\(\hex{ffff}\)
\(\hex{1234}\)
\(\hex{abcd}\)
Exercise4
AnswerSince there are 12 values, we need 4 bits. Any 4bit code would work. Here is one example:
Grade  Code  (in hex) 
A  \(\binary{0000}\)  \(\hex{0}\) 
A  \(\binary{0001}\)  \(\hex{1}\) 
B+  \(\binary{0010}\)  \(\hex{2}\) 
B  \(\binary{0011}\)  \(\hex{3}\) 
B  \(\binary{0100}\)  \(\hex{4}\) 
C+  \(\binary{0101}\)  \(\hex{5}\) 
C  \(\binary{0110}\)  \(\hex{6}\) 
C  \(\binary{0111}\)  \(\hex{7}\) 
D+  \(\binary{1000}\)  \(\hex{8}\) 
D  \(\binary{1001}\)  \(\hex{9}\) 
D  \(\binary{1010}\)  \(\hex{a}\) 
F  \(\binary{1011}\)  \(\hex{b}\) 
Exercises2.4.1Exercises
Exercise1
AnswerFour bytes are sufficient to store an unsigned integer up to \(4,294,967,295\) so we would need to allocate the memory bytes at \(\hex{0x2fffeb96}\)— \(\hex{0x2fffeb99}\text{.}\)
Exercise2
Answer\begin{gather*} \hex{00100e: 00}\\ \hex{00100f: 01}\\ \hex{001010: 02}\\ \hex{001011: 03}\\ \cdots \cdots\\ \hex{00102a: 1c}\\ \hex{00102b: 1d} \end{gather*}
Exercise3
Answer\(10_{16}\text{,}\) which is 0x10 in C/C++ syntax.
Exercises2.5.1Exercises
Exercise2
AnswerYou can type as many 'f's as you like, but the program only accepts eight. Therefore, the largest unsigned integer is \(4 \times 8 = 32\) bits.
Exercise3
SolutionAny integer greater than \(\hex{7fffffff}\) gives a negative output. This will be explained in Section 3.3, but for now realize that this change is caused by printf formatting, not the storage format, which is still unsigned int. Also note that we did not change scanf's reading format.
Exercise4
SolutionNotice the use of an “infinite” loop and a break statement to end a program.
Exercises2.7.1Exercises
Exercise1
SolutionExercises2.8.1Exercises
Exercise1
SolutionExercise2
SolutionExercise3
SolutionThere are five separate files for this solution. Four of the files, writeStr.h, writeStr.c, readLn.h, and readLn.c, will be used with other programs in the book. If you do not know how to compile multiplefile programs in C, refer to Appendix C.
Exercises3.1.1Exercises
Exercise1
AnswerFour bits. Note that six of the bit patterns are unused.
Exercise2
SolutionUsing our fourbit code, we would store one decimal digit in every four bits. That is, the lowestorder digit would be stored in bits 3–0, the next lowerorder digit in bits 7–4, etc. For example, let's consider \(48 + 27\text{:}\)
\(48_{10}\)  \(\rightarrow\)  \(\hex{00000048}_{16}\)  
+  \(27_{10}\)  \(\rightarrow\)  \(\hex{00000027}_{16}\) 
\(75_{10}\)  \(\ne\)  \(\hex{0000007f}_{16}\) 
Exercise3
Solution\(carry_{0} = 0\)

For \(i = 0, \cdots, (N1)\)
sum\(_{i} = (x_{i} + y_{i} + carry_{i})\) % \(2\)
carry\(_{i+1} = (x_{i} + y_{i} + carry_{i}) / 2\)
Exercise4
Solution\(carry_{0} = 0\)

For \(i = 0, \cdots, (N1)\)
sum\(_{i} = (x_{i} + y_{i} + carry_{i})\) % \(16\)
carry\(_{i+1} = (x_{i} + y_{i} + carry_{i}) / 16\)
Exercise5
SolutionSubtracting \(y\) from \(x\text{.}\)
\(borrow = 0\)

For \(i = 0, \cdots, (N1)\)

If \(y_{i} \le x_{i}\)
difference\(_{i} = x_{i}  y_{i}\)

Else
\(j = i + 1\)

While \((x_{j} = 0)\) and \((j \lt N)\)
\(j = j + 1\)

If \(j = N\)
\(borrow = 1\)
\(j = j  1\)
\(x_{j} = x_{j} + 2\)

While \(j \gt i\)
\(x_{j} = x_{j}  1 \)
\(j = j  1\)
\(x_{j} = x_{j} + 2\)
difference\(_{i} = x_{i}  y_{i}\)

Exercise6
SolutionSubtracting \(y\) from \(x\text{.}\)
\(borrow = 0\)

For \(i = 0, \cdots, (N1)\)

If \(y_{i} \le x_{i}\)
difference\(_{i} = x_{i}  y_{i}\)

Else
\(j = i + 1\)

While \((x_{j} = 0)\) and \((j \lt N)\)
\(j = j + 1\)

If \(j = N\)
\(borrow = 1\)
\(j = j  1\)
\(x_{j} = x_{j} + 16\)

While \(j \gt i\)
\(x_{j} = x_{j}  1 \)
\(j = j  1\)
\(x_{j} = x_{j} + 16\)
difference\(_{i} = x_{i}  y_{i}\)

Exercises3.3.1Exercises
Exercise1
Solution
If \(x \ge 0\)
Convert \(x\) to binary.

Else
Negate \(x\)
Convert the result to binary.
Compute the 2's complement of the result in the binary domain.
Exercise2
Solution
If highoder bit is \(0\)
Convert \(x\) to decimal.

Else
Compute the 2's complement of \(x\text{.}\)
Compute the decimal equivalent of the result.
Place a minus sign in front of the decimal equivalent.
Exercise3
Solution\(+85\)
\(85\)
\(16\)
\(+15\)
\(128\)
\(+99\)
\(+123\)
Exercise4
Solution\(+4660\)
\(30875\)
\(+22136\)
\(4660\)
\(292\)
\(+2016\)
\(32768\)
\(+32767\)
\(32767\)
Exercise5
Solution\(\hex{64}\)
\(\hex{ff}\)
\(\hex{f6}\)
\(\hex{58}\)
\(\hex{81}\)
\(\hex{f0}\)
\(\hex{e0}\)
\(\hex{80}\)
Exercise6
Solution\(\hex{7bcd}\)
\(\hex{ff04}\)
\(\hex{0400}\)
\(\hex{fc00}\)
\(\hex{ffff}\)
\(\hex{8000}\)
\(\hex{7fff}\)
\(\hex{ff00}\)
\(\hex{8001}\)
\(\hex{ff80}\)
Exercises3.5.1Exercises
Exercise1
SolutionStart at the tic mark for \(1\text{,}\) move \(3\) tic marks CW, giving \(4 = \binary{100}_{2}\text{.}\) We did not pass the tic mark at the top, so C = \(\binary{0}\text{,}\) and the result is correct.
Start at the tic mark for \(3\text{,}\) move \(4\) tic marks CW, giving \(7 = \binary{111}_{2}\text{.}\) We did not pass the tic mark at the top, so C = \(\binary{0}\text{,}\) and the result is correct.
Start at the tic mark for \(5\text{,}\) move \(6\) tic marks CW, giving \(3 = \binary{011}_{2}\text{.}\) We passed the tic mark at the top, so C = \(\binary{1}\text{,}\) and the result is wrong.
Start at the tic mark for \(+1\text{,}\) move \(3\) tic marks CW, giving \(4 = \binary{100}_{2}\text{.}\) We passed the tic mark at the bottom, so V = \(\binary{1}\text{,}\) and the result is wrong.
Start at the tic mark for \(3\text{,}\) move \(3\) tic marks CCW, giving \(+2 = \binary{010}_{2}\text{.}\) We passed the tic mark at the bottom, so C = \(\binary{0}\text{,}\) and the result is wrong.
Start at the tic mark for \(+3\text{,}\) move \(4\) tic marks CCW, giving \(1 = \binary{111}_{2}\text{.}\) We did not pass the tic mark at the bottom, so C = \(\binary{0}\text{,}\) and the result is correct.
Exercise2
Solutionsum  signed  unsigned  
a.  \(\hex{ff}\)  right  right 
b.  \(\hex{45}\)  right  wrong 
c.  \(\hex{fb}\)  right  right 
d.  \(\hex{de}\)  wrong  right 
e.  \(\hex{0e}\)  right  wrong 
f.  \(\hex{00}\)  wrong  wrong 
Exercise3
Solutionsum  signed  unsigned  
a.  \(\hex{0000}\)  right  wrong 
b.  \(\hex{1110}\)  right  wrong 
c.  \(\hex{0000}\)  wrong  wrong 
d.  \(\hex{03ff}\)  right  wrong 
e.  \(\hex{7fff}\)  right  right 
f.  \(\hex{7fff}\)  wrong  wrong 
Exercises4.2.1Exercises
Exercise1
SolutionShifting \(17\) to the left \(14\) times gives 278728. Shifting \(17\) to the right \(3\) times gives 2.
Exercises4.4.1Exercises
Exercise1
SolutionThe fflush(stdout); statement is required because output to stdout is line buffered.
Exercises5.1.1Exercises
Exercise1
SolutionFor Equation (5.1.3):
\(x\)  \(x \cdot 1\) 
\(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{1}\) 
And for Equation (5.1.4):
\(x\)  \(x + 0\) 
\(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{1}\) 
Exercise2
SolutionFor Equation (5.1.5):
\(x\)  \(y\)  \(x \cdot y\)  \(y \cdot x\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
And for Equation (5.1.6):
\(x\)  \(y\)  \(x + y\)  \(y + x\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
Exercise3
SolutionFor Equation (5.1.7):
\(x\)  \(x \cdot 0\) 
\(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{0}\) 
And for Equation (5.1.8):
\(x\)  \(x + 1\) 
\(\binary{0}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{1}\) 
Exercise4
SolutionFor Equation (5.1.9):
\(x\)  \(x'\)  \(x \cdot x'\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\) 
And for Equation (5.1.10):
\(x\)  \(x'\)  \(x + x'\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\) 
Exercise5
SolutionFor Equation (5.1.11):
\(x\)  \(x\)  \(x \cdot x\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
And for Equation (5.1.12):
\(x\)  \(x\)  \(x + x\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
Exercise6
SolutionFor Equation (5.1.13):
\(x\)  \(y\)  \(z\)  \(y + z\)  \(x \cdot (y + z)\)  \(x \cdot y\)  \(x \cdot z\)  \(x \cdot y + x \cdot z\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
And for Equation (5.1.14):
\(x\)  \(y\)  \(z\)  \(y \cdot z\)  \(x + y \cdot z\)  \(x + y\)  \(x + z\)  \((x + y) \cdot (x + z)\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\) 
\(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\) 
Exercises5.2.1Exercises
Exercise1
SolutionThe \(\sum\) column shows where the sum of minterms evaluates to \(1\text{,}\) and the \(\prod\) column shows where the product of Maxterms evaluates to \(0\text{.}\) Of course, in the blank cells the functions evaluate to the complementary value.
\(x\)  \(y\)  \(z\)  \(F(x,y,z) = \sum(0,1,5,6)\)  \(F(x,y,z) = \prod(2,3,4,7)\) 
\(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  
\(\binary{0}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  
\(\binary{0}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  
\(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  
\(\binary{1}\)  \(\binary{0}\)  \(\binary{0}\)  \(\binary{0}\)  
\(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  \(\binary{1}\)  
\(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\)  \(\binary{1}\)  
\(\binary{1}\)  \(\binary{1}\)  \(\binary{1}\)  \(\binary{0}\) 
Exercises5.3.1.1Exercises
Exercise1
SolutionLet a 4bit integer be \(wxyz\) where each literal represents one bit. The even 4bit integers are given by the function: \begin{alignat*}{1} F(w,x,y,z) \amp {}={} w' \cdot x' \cdot y' \cdot z' + w' \cdot x' \cdot y \cdot z' + w' \cdot x \cdot y' \cdot z' + w' \cdot x \cdot y \cdot z' \\ \amp \quad + w \cdot x' \cdot y' \cdot z' + w \cdot x' \cdot y \cdot z' + w \cdot x \cdot y' \cdot z' + w \cdot x \cdot y \cdot z' \end{alignat*} Using the distributive property repeatedly we get: \begin{alignat*}{1} F(w,x,y,z) \amp {}={} z' \cdot (w' \cdot x' \cdot y' + w' \cdot x' \cdot y + w' \cdot x \cdot y' + w' \cdot x \cdot y \\ \amp \quad + w \cdot x' \cdot y' + w \cdot x' \cdot y + w \cdot x \cdot y' + w \cdot x \cdot y) \\ \amp {}={} z' \cdot (w' \cdot (x' \cdot y' + x' \cdot y + x \cdot y' + x \cdot y) \\ \amp \quad + w \cdot (x' \cdot y' + x' \cdot y + x \cdot y' + x \cdot y)) \\ \amp {}={} z' \cdot (w' + w) \cdot (x' \cdot y' + x' \cdot y + x \cdot y' + x \cdot y) \\ \amp {}={} z' \cdot (w'+ w) \cdot (x' \cdot (y' + y) + x \cdot (y' + y)) \\ \amp {}={} z' \cdot (w' + w) \cdot (x' + x) \cdot (y' + y) \end{alignat*} And from the complement property we arrive at a minimal sum of products: \begin{gather*} F(w,x,y,z) = z' \end{gather*} which you recognize as Figure 5.1.3.
Exercises5.3.2.1Exercises
Exercise1
SolutionFirst we draw the Karnaugh map:
Several groupings are possible. Keep in mind that groupings can wrap around. We will work with:
which yields a minimal sum of products: \begin{equation*} F(x,y,z) = z' + x' \cdot y' + x \cdot y \end{equation*}
Exercise2
SolutionThis expression includes Maxterms 0, 1, 3, 4, and 7. These appear in a Karnaugh map:
Next we encircle the largest adjacent blocks, where the number of cells in each block is a power of two. Notice that Maxterm \(M_{0}\) appears in two groups.
From this Karnaugh map it is very easy to write the function as a minimal product of sums: \begin{equation*} F(x,y,z) = (x + y) \cdot (y + z) \cdot (y' + z') \end{equation*}
Exercise3
SolutionUsing the Karnaugh map zeros:
we obtain the complement of our desired function: \begin{equation*} F'(x,y,z) = x' \cdot y \cdot z + x \cdot y' \cdot z \end{equation*} and from DeMorgan's Law: \begin{equation*} F(x,y,z) = (x + y' + z') \cdot (x' + y + z') \end{equation*}
Exercise4
SolutionExercise5
SolutionExercise6
SolutionThe prime numbers correspond to the minterms \(m_{2}\text{,}\) \(m_{3}\text{,}\) \(m_{5}\text{,}\) and \(m_{7}\text{.}\) The minterms \(m_{10}\text{,}\) \(m_{11}\text{,}\) \(m_{12}\text{,}\) \(m_{13}\text{,}\) \(m_{14}\text{,}\) \(m_{15}\) cannot occur so are marked “don't care” on the Karnaugh map.
which gives: \begin{equation*} F(w,x,y,z) = x \cdot z + x' \cdot y \end{equation*}
Exercises6.5.1Exercises
Exercise1
Solution\(x_1\)  \(x_0\)  \(y_1\)  \(y_0\)  \(F(x,y)\) 
\(0\)  \(0\)  \(0\)  \(0\)  \(0\) 
\(0\)  \(0\)  \(0\)  \(1\)  \(1\) 
\(0\)  \(0\)  \(1\)  \(0\)  \(1\) 
\(0\)  \(0\)  \(1\)  \(1\)  \(1\) 
\(0\)  \(1\)  \(0\)  \(0\)  \(0\) 
\(0\)  \(1\)  \(0\)  \(1\)  \(0\) 
\(0\)  \(1\)  \(1\)  \(0\)  \(1\) 
\(0\)  \(1\)  \(1\)  \(1\)  \(1\) 
\(1\)  \(0\)  \(0\)  \(0\)  \(0\) 
\(1\)  \(0\)  \(0\)  \(1\)  \(0\) 
\(1\)  \(0\)  \(1\)  \(0\)  \(0\) 
\(1\)  \(0\)  \(1\)  \(1\)  \(1\) 
\(1\)  \(1\)  \(0\)  \(0\)  \(0\) 
\(1\)  \(1\)  \(0\)  \(1\)  \(0\) 
\(1\)  \(1\)  \(1\)  \(0\)  \(0\) 
\(1\)  \(1\)  \(1\)  \(1\)  \(0\) 
Exercises8.5.1Exercises
Exercise1
AnswerThis can vary, depending on different versions of the compiler, etc. This is an exercise on using gdb so you can become comfortable with it for when you will actually need it to find your errors. It is also an excellent learning tool.
Exercise2
AnswerThe contents of the register should be one less than the integer you entered when the program prompted you. The C statement uses 12 bytes of program memory.
Exercise3
AnswerThe program uses the address of ex, &ex, so the variable must be located in memory.
Exercise5
SolutionExercise6
SolutionExercises9.3.1Exercises
Exercise1
SolutionThe exit code is displayed in octal.
Exercise2
SolutionExercise3
SolutionExercise4
SolutionExercise5
SolutionExercises10.3.1Exercises
Exercise1
SolutionExercises10.4.3Exercises
Exercise1
SolutionExperiment with entering different numbers of characters, and you will see that the read function reads the newline character (enter key) as one character. Try entering “abcdls” (without the quotes). Notice that any characters beyond the four requested remain in the input buffer and are read by the shell when the program exits.
Exercises12.1.4Exercises
Exercise1
SolutionExercise2
SolutionExercises12.2.3Exercises
Exercise1
SolutionExercises13.2.1Exercises
Exercise1
SolutionI suggest discussing this with your instructor and colleagues. Opinions will differ. Listen and learn.
Exercise2
SolutionExercise3
SolutionExercise4
SolutionExercise5
SolutionThere are multiple ways to solve this problem. I chose to write a separate function, newLine to give good flexibility in how I display text for users. It is a given that users will want you to change your program's display.
Exercises14.1.1Exercises
Exercise1
SolutionExercises14.2.1Exercises
Exercise1
SolutionYou will need to copy the assembly language code for hexToInt into a file and then assemble it with:
as gstabs o hexToInt2.o hexToInt2.s
Then use gcc to link the object files with the file containing the main function:
gcc g o hexConvert1 hexToInt2.o writeStr.o readLn.o hexConvert1.c
Exercise2
AnswerIt works for both cases because all alphabetic characters are numerically higher than the numeral characters in the ASCII code.
Exercise3
SolutionExercise4
SolutionExercises14.4.1Exercises
Exercise1
SolutionThe writeStr, readLn, newLine, uDecToInt, and uIntToDec functions have been provided earlier in the book. Here is the main function to solve this problem.
@ incrementDec.s @ Prompts user for unsigned decimal number and adds 1 to it. @ Bob Plantz  6 August 2016 @ Define my Raspberry Pi .cpu cortexa53 .fpu neonfparmv8 .syntax unified @ modern syntax @ Constants for assembler .equ maxChars,11 @ max input chars .equ inputString,16 @ for input string .equ outputString,28 @ for output string .equ locals,24 @ space for local vars @ Constant program data .section .rodata .align 2 prompt: .asciz "Enter an unsigned number up to 4294967294: " @ The program .text .align 2 .global main .type main, %function main: stmfd sp!, {fp, lr} @ save caller's info add fp, sp, 4 @ our frame pointer sub sp, sp, locals @ for local vars ldr r0, promptAddr @ prompt user bl writeStr add r0, fp, inputString @ place for user input mov r1, maxChars @ limit input size bl readLn add r0, fp, inputString @ user input bl uDecToInt @ convert it add r1, r0, 1 @ increment user's number add r0, fp, outputString bl uIntToDec add r0, fp, outputString bl writeStr bl newLine mov r0, 0 @ return 0; add sp, sp, locals @ deallocate local var ldmfd sp!, {fp, lr} @ restore caller's info bx lr @ return promptAddr: .word prompt
Exercise2
Solution@ uIntToDecMls.s @ Converts an int to the corresponding unsigned @ decimal text string. @ Calling sequence: @ r0 < address of place to store string @ r1 < int to convert @ bl uIntToDec @ Bob Plantz  6 August 2016 @ Bob Plantz  9 October 2016: use mls for remainder computation @ Define my Raspberry Pi .cpu cortexa53 .fpu neonfparmv8 .syntax unified @ modern syntax @ Constant for assembler .equ tempString,32 @ for temp string .equ locals,16 @ space for local vars .equ zero,0x30 @ ascii 0 .equ NUL,0 @ The program .text .align 2 .global uIntToDec .type uIntToDec, %function uIntToDec: stmfd sp!, {r4, r5, r6, r7, fp, lr} @ save caller's info add fp, sp, 20 @ our frame pointer sub sp, sp, locals @ for local vars mov r4, r0 @ caller's string pointer add r5, fp, tempString @ temp string mov r7, 10 @ decimal constant mov r0, NUL @ end of C string strb r0, [r5] add r5, r5, 1 @ move to char storage mov r0, zero @ assume the int is 0 strb r0, [r5] movs r6, r1 @ int to convert beq copyLoop @ zero is special case convertLoop: cmp r6, 0 @ end of int? beq copy @ yes, copy for caller udiv r0, r6, r7 @ no, div to get quotient mls r2, r0, r7, r6 @ the mod (remainder) mov r6, r0 @ the quotient orr r2, r2, zero @ convert to numeral strb r2, [r5] add r5, r5, 1 @ next char position b convertLoop copy: sub r5, r5, 1 @ last char stored locally copyLoop: ldrb r0, [r5] @ get local char strb r0, [r4] @ store the char for caller cmp r0, NUL @ end of local string? beq allDone @ yes, we're done add r4, r4, 1 @ no, next caller location sub r5, r5, 1 @ next local char b copyLoop allDone: strb r0, [r4] @ end C string add sp, sp, locals @ deallocate local var ldmfd sp!, {r4, r5, r6, r7, fp, lr} @ restore caller's info bx lr @ return
Exercise3
Solution@ incrementSigned.s @ Prompts user for signed decimal number and adds 1 to it. @ Bob Plantz  29 July 2016 @ Define my Raspberry Pi .cpu cortexa53 .fpu neonfparmv8 .syntax unified @ modern syntax @ Constants for assembler .equ maxChars,12 @ max input chars .equ inputString,16 @ for input string .equ outputString,28 @ for output string .equ locals,24 @ space for local vars @ Constant program data .section .rodata .align 2 prompt: .asciz "Enter a signed integer between 2147483648 and +2147483647: " @ The program .text .align 2 .global main .type main, %function main: stmfd sp!, {fp, lr} @ save caller's info add fp, sp, 4 @ our frame pointer sub sp, sp, locals @ for local vars ldr r0, promptAddr @ prompt user bl writeStr bl getDecInt @ convert it add r0, r0, 1 @ increment user's number bl putDecInt @ print result bl newLine mov r0, 0 @ return 0; add sp, sp, locals @ deallocate local var ldmfd sp!, {fp, lr} @ restore caller's info bx lr @ return promptAddr: .word prompt
@ getDecInt.s @ Gets signed decimal integer from keyboard. @ Calling sequence: @ bl getDecInt @ returns equivalent int @ Bob Plantz  29 July 2016 @ Define my Raspberry Pi .cpu cortexa53 .fpu neonfparmv8 .syntax unified @ modern syntax @ Constants for assembler .equ maxChars,12 @ max input chars .equ inputString,20 @ for input string .equ locals,16 @ space for local vars @ Useful source code constants .equ POS,0 .equ NEG,1 @ The program .text .align 2 .global getDecInt .type getDecInt, %function getDecInt: stmfd sp!, {r4, fp, lr} @ save caller's info add fp, sp, 8 @ our frame pointer sub sp, sp, locals @ for the string add r0, fp, inputString @ place to store input mov r1, maxChars @ limit input length bl readLn add r0, fp, inputString @ input string mov r4, POS @ assume postive int ldrb r1, [r0] @ get char cmp r1, ' @ minus sign? bne checkPlus @ no, check for plus sign mov r4, NEG @ yes, flag as neg add r0, r0, 1 @ go to the number b convert @ and convert it checkPlus: cmp r1, '+ @ plus sign? bne convert @ no, we're at the number add r0, r0, 1 @ go to the number convert: bl uDecToInt cmp r4, POS @ positive int? beq allDone @ yes, we're done mvn r0, r0 @ no, complement it add r0, r0, 1 @ and finish negate allDone: add sp, sp, locals @ deallocate local var ldmfd sp!, {r4, fp, lr} @ restore caller's info bx lr @ return
@ putDecInt.s @ Converts an int to the corresponding signed @ decimal text string. @ Calling sequence: @ r0 < int to print @ bl putDecInt @ Bob Plantz  29 July 2016 @ Define my Raspberry Pi .cpu cortexa53 .fpu neonfparmv8 .syntax unified @ modern syntax @ Constants for assembler .equ decString,20 @ for string .equ locals,20 @ space for local vars @ Useful source code constants .equ POS,0 .equ NEGBIT,0x80000000 @ The program .text .align 2 .global putDecInt .type putDecInt, %function putDecInt: stmfd sp!, {r4, fp, lr} @ save caller's info add fp, sp, 8 @ our frame pointer sub sp, sp, locals @ space for the string add r4, fp, decString @ place to store string mov r1, '+ @ assume positive strb r1, [r4] tst r0, NEGBIT @ negative int? beq positive @ no, go on mov r1, ' @ yes, need to negate strb r1, [r4] mvn r0, r0 @ complement add r0, r0, 1 @ two's complement positive: mov r1, r0 @ int to convert add r0, r4, 1 @ skip over sign char bl uIntToDec add r0, fp, decString @ place to store string bl writeStr add sp, sp, locals @ deallocate local var ldmfd sp!, {r4, fp, lr} @ restore caller's info bx lr @ return
Exercises15.1.1Exercises
Exercise1
SolutionExercises15.3.1Exercises
Exercise1
SolutionExercises15.4.1Exercises
Exercise1
SolutionExercises16.1.1Exercises
Exercise1
SolutionRefer to Equation (16.1.1).
\(i = 1\)

While (\(F \ne 0\)) AND (wantmorebits)
\(F = 2 \times F\)
\(d_{i} = int(F)\)
\(F = F  d_{i}\)
\(i = i  1\)
Exercises16.4.1Exercises
Exercise1
SolutionCompute \(s\text{,}\) \(e+127\text{,}\) and \(f\text{.}\) \begin{align*} s &= \binary{0}\\ e + 127 &= \binary{00000000}_{2}\\ e &= 127_{10}\\ f &= \binary{00000000000000000000000} \end{align*}
Finally, plug these values into Equation (16.4.1). (Remember to add the hidden bit.) \begin{align*} (1)^0 \times 1.00\dots 00 \times 2^{127} &= \mbox{a very small number}\\ &\ne 0.0 \end{align*} so we do need the special case.
Exercise2
Answer\(\hex{3f80 0000}\)
\(\hex{bdcc cccd}\)
\(\hex{44fc 0000}\)
\(\hex{3b80 0000}\)
\(\hex{c543 5500}\)
\(\hex{3ea8 f5c3}\)
\(\hex{3f2b 851f}\)
\(\hex{4048 f5c3}\)
Exercise3
Answer\(+2.0\)
\(1.0\)
\(+0.0625\)
\(16.03125\)
\(100.03125\)
\(1.2\)
\(123.449997\)
\(54.320999\)
Exercises18.5.1Exercises
Exercise1
SolutionExercises19.2.1Exercises
Exercise1
SolutionSee Listing 19.2.6 for gpioPinFSelect and Listing 19.2.8 for gpioPinSet.