Basics
Prev: Introduction
Next: Power-of-2 Boundaries
Sections
2-1Manipulating Rightmost Bits2-2Addition Combined with Logical Operations2-3Inequalities among Logical and Arithmetic Expressions2-4Absolute Value Function2-5Average of Two Integers2-6Sign Extension2-7Shift Right Signed from Unsigned2-8Sign Function2-9Three-Valued Compare Function2-10Transfer of Sign Function2-11Decoding a “Zero Means ” Field2-12Comparison Predicates2-13Overflow Detection2-14Condition Code Result of Add, Subtract, and Multiply2-15Rotate Shifts2-16Double-Length Add/Subtract2-17Double-Length Shifts2-18Multibyte Add, Subtract, Absolute Value2-19Doz, Max, Min2-20Exchanging Registers2-21Alternating among Two or More Values2-22A Boolean Decomposition Formula2-23Implementing Instructions for all 16 Binary Boolean Operations
Problems
-
David de Kloet suggests the following code for the
snoobfunction, for , where the final assignment toyis the result:This is essentially the same as Gosper’s code (page 15), except the right shift is done with a
whileloop rather than with a divide instruction. Because division is usually costly in time, this might be competitive with Gosper’s code if thewhileloop is not executed too many times. Let be the length of the bit strings and , the number of 1-bits in the strings, and assume the code is executed for all values of that have exactly 1-bits. Then for each invocation of the function, how many times, on average, will the body of thewhileloop be executed? -
The text mentions that a left shift by a variable amount is not right-to-left computable. Consider the function [Knu8]. This is a left shift by a variable amount, but it can be computed by
, or
,
which are all right-to-left computable operations. What is going on here? Can you think of another such function?
-
Derive Dietz’s formula for the average of two unsigned integers.
-
Give an overflow-free method for computing the average of four unsigned integers, .
-
Many of the comparison predicates shown on page 23 can be simplified substantially if bit 31 of either or is known. Show how the seven-instruction expression on page 23 can be simplified to three basic RISC, non-comparison, instructions if .
-
Show that if two numbers, possibly distinct, are added with end-around carry, the addition of the carry bit cannot generate another carry out of the high-order position.
-
Show how end-around carry can be used to do addition if negative numbers are represented in one’s-complement notation. What is the maximum number of bit positions that a carry, from any bit position, might be propagated through?
-
Show that the MUX operation, , can be done in three instructions on the basic RISC, which does not have the and-with-complement instruction.
-
Show how to implement in four instructions with and-or-not logic.
Given a 32-bit word and two integer variables and in registers, show code to copy the bit of at position to position . The values of and have no relation, but assume that .
How many binary Boolean instructions are sufficient to evaluate any -variable Boolean function if it is decomposed recursively by the method of the theorem?
Show that alternative decompositions of Boolean functions of three variables are possible, including the negative Davio decomposition and a form involving .
It is mentioned in the text that all 16 binary Boolean operations can be done with the eight instructions shown in Table 2-3, by interchanging the inputs or by having both register fields of the instruction refer to the same register. Show how to do this.
Suppose you are not concerned about the six Boolean functions that are really constants or unary functions, namely , and , but you want your instruction set to compute the other ten functions with one instruction. Can this be done with fewer than eight binary Boolean instruction types, or opcodes?
Exercise 13 shows that eight instruction types suffice to compute any of the 16 two-operand Boolean operations with one R-R, register-register, instruction. Show that six instruction types suffice in the case of R-I, register-immediate, instructions. With R-I instructions, the input operands cannot be interchanged or equated, but the second input operand, the immediate field, can be complemented or, in fact, set to any value at no cost in execution time. Assume for simplicity that the immediate fields are the same length as the general purpose registers.
Show that not all Boolean functions of three variables can be implemented with three binary logical instructions.