Question and Key
Question and Key
Question and Key
PART- B
10. A.
Representing Instructions in the Computer
Instructions are kept in the computer as a series of high and low electronic signals and
may be represented as numbers. In fact, each piece of an instruction can be considered as an
individual number, and placing these numbers side by side forms the instruction.
Since registers are referred to by almost all instructions, there must be a convention to
map register names into numbers. In MIPS assembly language, registers $s0 to $s7 map onto
registers 16 to 23, and registers $t0 to $t7 map onto registers 8 to 15. Hence, $s0 means
register 16, $s1 means register 17, $s2 means register 18,…, $t0 means register 8, $t1 means
register 9, and so on. We’ll describe the convention for the rest of the 32 registers in the
following sections.
Example
Let’s do the next step in the refinement of the MIPS language as an example. We’ll
show the real MIPS language version of the instruction represented symbolically as
add $t0,$s1,$s2
Answer
This layout of the instruction is called the instruction format. As you can see from
counting the number of bits, this MIPS instruction takes exactly 32 bits—the same size as a
data word. In keeping with our design principle that simplicity favors regularity, all MIPS
instructions are 32 bits long.
It would appear that you would now be reading and writing long, tedious strings of
binary numbers. We avoid that tedium by using a higher base than binary that converts easily
into binary. Since almost all computer data sizes are multiples of 4, hexadecimal (base 16)
numbers are popular. Since base 16 is a power of 2, we can trivially convert by replacing
each group of four binary digits by a single hexadecimal digit, and vice versa. Figure 1.11
converts between hexadecimal and binary.
hexadecimal
Because we frequently deal with different number bases, to avoid confusion we will
subscript decimal numbers with ten, binary numbers with two, and hexadecimal numbers
with hex. (If there is no subscript, the default is base 10.) By the way, C and Java use the
notation 0xnnnn for hexadecimal numbers.
Example
Convert the following hexadecimal and binary numbers into the other base:
eca8 6420hex
Answer
Using Figure 2.4, the answer is just a table lookup one way:
MIPS Fields
■ rd: The register destination operand. It gets the result of the operation.
■ shamt: Shift amount. (Section 2.6 explains shift instructions and this term; it will
not be used until then, and hence the field contains zero in this section.)
■ funct: Function. This field, often called the function code, selects the specific
variant of the operation in the op field.
Opcode The field that denotes the operation and format of an instruction.
A problem occurs when an instruction needs longer fields than those shown above.
For example, the load word instruction must specify two registers and a constant. If the
address were to use one of the 5-bit fields in the format above, the constant within the load
word instruction would be limited to only 2 5 or 32. This constant is used to select elements
from arrays or data structures, and it often needs to be much larger than 32. This 5-bit field is
too small to be useful.
Hence, we have a conflict between the desire to keep all instructions the same length
and the desire to have a single instruction format. This leads us to the final hardware design
principle:
The compromise chosen by the MIPS designers is to keep all instructions the same
length, thereby requiring different kinds of instruction formats for different kinds of
instructions. For example, the format above is called R-type (for register) or R-format.
A second type of instruction format is called I-type (for immediate) or I-format and is
used by the immediate and data transfer instructions. The fields of I-format are
The 16-bit address means a load word instruction can load any word within a
region of ±215 or 32,768 bytes (±213 or 8192 words) of the address in the base
register rs.
Similarly, add immediate is limited to constants no larger than ±2 15. We see
that more than 32 registers would be difficult in this format, as the rs and rt
fields would each need another bit, making it harder to fit everything in one
word.
Although multiple formats complicate the hardware, we can reduce the complexity by
keeping the formats similar. For example, the first three fields of the R-type and I-type
formats are the same size and have the same names; the length of the fourth field in I-type is
equal to the sum of the lengths of the last three fields of R-type.
In case you were wondering, the formats are distinguished by the values in the first
field: each format is assigned a distinct set of values in the first field (op) so that the hardware
knows whether to treat the last half of the instruction as three fields (R-type) or as a single
field (I-type). Figure 1.12 shows the numbers used in each field for the MIPS instructions
covered so far.
Figure 1.12 MIPS instruction encoding.In the table above, “reg” means a register number
between 0 and 31, “address” means a 16-bit address, and “n.a.” (not applicable) means this field does
not appear in this format. Note that add and sub instructions have the same value in the op field; the
hardware uses the funct field to decide the variant of the operation: add (32) or subtract (34).
These principles lead to the stored-program concept; its invention let the computing genie
out of its bottle. Figure1.13 shows the power of the concept; specifically, memory can
contain the source code for an editor program, the corresponding compiled machine code, the
text that the compiled program is using, and even the compiler that generated the machine
code.
Figure 1.13 The stored-program concept.
Stored programs allow a computer that performs accounting to become, in the blink of
an eye, a computer that helps an author write a book. The switch happens simply by loading
memory with programs and data and then telling the computer to begin executing at a given
location in memory. Treating instructions in the same way as data greatly simplifies both the
memory hardware and the software of computer systems. Specifically, the memory
technology needed for data can also be used for programs, and programs like compilers, for
instance, can translate code written in a notation far more convenient for humans into code
that the computer can understand.
B. Logical Operations
Although the first computers operated on full words, it soon became clear that it was
useful to operate on fields of bits within a word or even on individual bits. Examining
characters within a word, each of which is stored as 8 bits, is one example of such an
operation. It follows that operations were added to programming languages and instruction
set architectures to simplify, among other things, the packing and unpacking of bits into
words. These instructions are called logical operations. Figure 1.14 shows logical operations
in C, Java, and MIPS.
Figure 1.14 C and Java logical operators and their corresponding MIPS instructions.MIPS
implements NOT using a NOR with one operand being zero.
The first class of such operations is called shifts. They move all the bits in a
word to the left or right, filling the emptied bits with 0 s.
The dual of a shift left is a shift right. The actual name of the two
MIPS shift instructions are called shift left logical (sll) and shift right logical
(srl).
Another useful operation that isolates fields is AND. AND is a bit-by-bit operation
that leaves a 1 in the result only if both bits of the operands are 1
As you can see, AND can apply a bit pattern to a set of bits to force 0 s where
there is a 0 in the bit pattern. Such a bit pattern in conjunction with AND is
traditionally called a mask, since the mask “conceals” some bits.
ORA logical bit-by-bit operation with two operands that calculates a 1 if there
is a 1 in either operand.
NOT A logical bit-by-bit operation with one operand that inverts the bits;
that is, it replaces every 1 with a 0, and every 0 with a 1.
In keeping with the three-operand format, the designers of MIPS decided to include
the instruction NOR (NOT OR) instead of NOT. If one operand is zero, then it is equivalent
to NOT: A NOR 0=NOT (A OR 0)=NOT (A).
NOR A logical bit-by-bit operation with two operands that calculates the
NOT of the OR of the two operands. That is, it calculates a 1 only if there is a
0 in both operands.