UNIT 1
UNIT 1
UNIT 1
FUNCTIONAL UNITS
A computer consists of five functionally independent main parts:
• Input unit
• Memory unit
• Arithmetic And Logic unit
• Output unit And
• Control units.
The input unit accepts coded information from human operators using devices such as
keyboards or from other computers over digital communication lines. The information received
is stored in the computer’s memory, either for later use or to be processed immediately by the
arithmetic and logic unit. The processing steps are specified by a program that is also stored in
the memory. Finally, the results are sent back to the outside world through the output unit. All of
these actions are coordinated by the control unit. An interconnection network provides the means
for the functional units to exchange information and coordinate their actions.
We refer to the arithmetic and logic circuits, in conjunction with the main control circuits,
as the processor. Input and output equipment is often collectively referred to as the input-output
(I/O) unit. A program is a list of instructions which performs a task. Programs are stored in the
memory. The processor fetches the program instructions from the memory, one after another,
and performs the desired operations.
Data are numbers and characters that are used as operands by the instructions. Data are
also stored in the memory. The instructions and data handled by a computer must be encoded in
a suitable format. Each instruction, number, or character is encoded as a string of binary digits
called bits, each having one of two possible values, 0 or 1, represented by the two stable states.
Input Unit
Computers accept coded information through input units. The most common input device
is the keyboard. Whenever a key is pressed, the corresponding letter or digit is automatically
translated into its corresponding binary code and transmitted to the processor. Many other kinds
of input devices for human-computer interaction are available, including the touchpad, mouse,
joystick, and trackball.
Memory Unit
The function of the memory unit is to store programs and data. There are two classes of storage,
called primary and secondary.
Primary Memory
Primary memory, also called main memory, is a fast memory that operates at electronic
speeds. Programs must be stored in this memory while they are being executed. The memory
consists of a large number of semiconductor storage cells, each capable of storing one bit of
information. These cells are rarely read or written individually. Instead, they are handled in
groups of fixed size called words. The memory is organized so that one word can be stored or
retrieved in one basic operation. The number of bits in each word is referred to as the word
length of the computer, typically 16, 32, or 64 bits.
To provide easy access to any word in the memory, a distinct address is associated with
each word location. Addresses are consecutive numbers, starting from 0, that identify successive
locations.
A memory in which any location can be accessed in a short and fixed amount of time
after specifying its address is called a random-access memory (RAM). The time required to
access one word is called the memory access time.
Cache Memory
As an adjunct to the main memory, a smaller, faster RAM unit, called a cache, is used to hold
sections of a program that are currently being executed, along with any associated data. The
cache is tightly coupled with the processor and is usually contained on the same integrated-
circuit chip. At the start of program execution, the cache is empty. All program instructions and
any required data are stored in the main memory. As execution proceeds, instructions are fetched
into the processor chip, and a copy of each is placed in the cache. When the execution of an
instruction requires data located in the main memory, the data are fetched and copies are also
placed in the cache.
Now, suppose a number of instructions are executed repeatedly as happens in a program
loop. If these instructions are available in the cache, they can be fetched quickly during the
period of repeated use. Similarly, if the same data locations are accessed repeatedly while copies
of their contents are available in the cache, they can be fetched quickly.
Secondary Storage
Although primary memory is essential, it tends to be expensive and does not retain
information when power is turned off. Thus additional, less expensive, permanent secondary
storage is used when large amounts of data and many programs have to be stored, particularly
for information that is accessed infrequently. Access times for secondary storage are longer than
for primary memory. A wide selection of secondary storage devices is available, including
magnetic disks, optical disks (DVD and CD), and flash memory devices.
Arithmetic and Logic Unit
Most computer operations are executed in the arithmetic and logic unit (ALU) of the
processor. Any arithmetic or logic operation, such as addition, subtraction, multiplication,
division, or comparison of numbers, is initiated by bringing the required operands into the
processor, where the operation is performed by the ALU. For example, if two numbers located in
the memory are to be added, they are brought into the processor, and the addition is carried out
by the ALU. The sum may then be stored in the memory or retained in the processor for
immediate use.
Output Unit
The output unit is the counterpart of the input unit. Its function is to send processed
results to the outside world. Examples of output unit are monitor and printer.
Control Unit
The memory, arithmetic and logic, and I/O units store and process information and perform input
and output operations. The operation of these units must be coordinated in some way.
This is the responsibility of the control unit. The control unit is effectively the nerve center that
sends control signals to other units and senses their states.
Control circuits are responsible for generating the timing signals that govern the transfers
and determine when a given action is to take place. Data transfers between the processor and the
memory are also managed by the control unit through timing signals.
The term clock cycles per instruction, which is the average number of clock cycles each
instruction takes to execute, is often abbreviated as CPI.
INSTRUCTIONS: LANGUAGE OF THE COMPUTER
To command a computer’s hardware, you must speak its language. The words of a
computer’s language are called instructions, and its vocabulary is called an instruction set. We
will take a quick look at three popular instruction sets.
1. MIPS is an elegant example of the instruction sets designed since the 1980s.
2. ARMv7 is an older instruction set also from ARM Holdings plc, but with 32-bit
addresses instead of ARMv8’s 64 bits. More than 14 billion chips with ARM processors
were manufactured in 2015, making them the most popular instruction sets in the world.
3. The final example is the Intel x86, which powers both the PC and the Cloud of the post-
PC era.
OPERATIONS OF THE COMPUTER HARDWARE
Every computer must be able to perform arithmetic. The MIPS assembly language notation
add a, b, c
instructs a computer to add the two variables b and c and to put their sum in a. This notation is
rigid in that each MIPS arithmetic instruction performs only one operation and must always have
exactly three variables. For example, suppose we want to place the sum of four variables b, c, d,
and e into variable a. The following sequence of instructions adds the four variables:
add a, b, c # The sum of b and c is placed in a
add a, a, d # The sum of b, c, and d is now in a
add a, a, e # The sum of b, c, d, and e is now in a
Thus, it takes three instructions to sum the four variables. The words to the right of the sharp
symbol (#) on each line above are comments for the human reader, so the computer ignores them.
Compiling Two C Assignment Statements into MIPS
Example: This segment of a C program contains the five variables a, b, c, d, and e. Since Java
evolved from C, this example and the next few work for either high-level programming language:
a = b + c;
d = a – e;
The translation from C to MIPS assembly language instructions is performed by the compiler.
Show the MIPS code produced by a compiler.
Answer: A MIPS instruction operates on two source operands and places the result in one
destination operand. Hence, the two simple statements above compile directly into these two
MIPS assembly language instructions:
add a, b, c
sub d, a, e
Compiling a Complex C Assignment into MIPS
Example: A somewhat complex statement contains the five variables f, g, h, i, and j:
f = (g + h) – (i + j);
What might a C compiler produce?
Answer: The above two statements are converted into following MIPS assembly language
add t0,g,h # temporary variable t0 contains g + h
add t1,i,j # temporary variable t1 contains i + j
sub f,t0,t1 # f gets t0 – t1, which is (g + h) – (i + j)
Actual MIPS memory addresses and contents of memory for those words.
Compiling Using Load and Store
Example: Assume variable h is associated with register $s2 and the base address of the
array A is in $s3. What is the MIPS assembly code for the C assignment statement below?
A[12] = h + A[8];
Answer: We use the proper offset for byte addressing in the load word instruction to select A[8],
and the add instruction places the sum in $t0:
lw $t0,32($s3) # Temporary reg $t0 gets A[8]
add $t0,$s2,$t0 # Temporary reg $t0 gets h + A[8]
The final instruction stores the sum into A[12], using 48 (4*12) as the offset and register $s3 as
the base register.
sw $t0,48($s3) # Stores h + A[8] back into A[12]
Constant or Immediate Operands
Constant variables are used as one of the operand for many arithmetic operations in
MIPS. The constants would have been placed in memory when the program was loaded.
For example, to add the constant 4 to register $s3, we could use the code
lw $t0, AddrConstant4($s1) # $t0 = constant 4
add $s3,$s3,$t0 # $s3 = $s3 + $t0 ($t0 == 4)
assuming that $s1 + AddrConstant4 is the memory address of the constant 4.
An alternative that avoids the load instruction is to offer versions of the arithmetic
instructions in which one operand is a constant. This quick add instruction with one constant
operand is called add immediate or addi. To add 4 to register $s3, we just write
addi $s3,$s3,4 # $s3 = $s3 + 4
Constant operands occur frequently, and by including constants inside arithmetic instructions,
operations are much faster and use less energy than if constants were loaded from memory.
Each of these segments of an instruction is called a field. The first and last fields (containing 0
and 32 in this case) in combination tell the MIPS computer that this instruction performs
addition. The second field gives the number of the register that is the first source operand of the
addition operation (17 = $s1), and the third field gives the other source operand for the addition
(18 = $s2). The fourth field contains the number of the register that is to receive the sum
(8 = $t0). The fifth field is unused in this instruction, so it is set to 0. Thus, this instruction adds
register $s1 to register $s2 and places the sum in register $t0. This instruction can also be
represented as fields of binary numbers as opposed to decimal:
I-FORMAT
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.
Example: lw $t0, 32($s3) # Temporary reg $t0 gets A[8]
Here, 19 (for $s3) is placed in the rs field, 8 (for $t0) is placed in the rt field, and
32 is placed in the address field. Note that the meaning of the rt field has changed for this
instruction: in a load word instruction, the rt field specifies the destination register, which
receives the result of the load.
J-FORMAT
J format or J-type is short for "jump type". The format of a J-type instruction looks like:
Opcode Address
6 bits 26 bits
Example: j 10000
LOGICAL OPERATIONS
The packing and unpacking of bits into words are called as logical operations.
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 0s. For example, if register $s0 contained
0000 0000 0000 0000 0000 0000 0000 0011two = 3ten
and the instruction to shift left by 2 was executed, the new value would be:
0000 0000 0000 0000 0000 0000 0000 1100two = 12ten
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). The following instruction performs
the operation above, assuming that the original value was in register $s0 and the result should go
in register $t2:
sll $t2,$s0,2 # reg $t2 = reg $s0 << 2 bits
We delayed explaining the shamt fi eld in the R-format. Used in shift instructions, it
stands for shift amount. Hence, the machine language version of the instruction above is
op rs rt rd shamt funct
0 0 16 10 2 0
The encoding of sll is 0 in both the op and funct fields, rd contains 10 (register $t2), rt
contains 16 (register $s0), and shamt contains 4. The rs field is unused and thus is set to 0. The
above sll shifts by 2, which gives the same result as multiplying by 22 or 4. The first bit pattern
above represents 3, and 3*22=3*4=12, the value of the second bit pattern.
AND: A logical bit by- bit operation with two operands that calculates a 1 only if there is a 1 in
both operands.
OR: A 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.
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.
INSTRUCTIONS FOR MAKING DECISIONS
MIPS assembly language includes two decision-making instructions, similar to an if
statement with a go to. The first instruction is
beq register1, register2, L1
This instruction means go to the statement labeled L1 if the value in register1 equals the value in
register2. The mnemonic beq stands for branch if equal. The second instruction is
bne register1, register2, L1
It means go to the statement labeled L1 if the value in register1 does not equal the value in
register2. The mnemonic bne stands for branch if not equal. These two instructions are
traditionally called conditional branches.
Compiling if-then-else into Conditional Branches
In the following code segment, f, g, h, i, and j are variables. If the five variables f through
j correspond to the five registers $s0 through $s4, what is the compiled MIPS code for this C if
statement?
if (i == j)
f = g + h;
else
f = g – h;
The above figure shows a flowchart of what the MIPS code should do. The first expression
compares for equality, so it would seem that we would want the branch if registers are equal
instruction (beq). In general, the code will be more efficient if we test for the opposite condition
to branch over the code that performs the subsequent then part of the if (the label Else is defined
below) and so we use the branch if registers are not equal instruction (bne):
Conditional branch: An instruction that requires the comparison of two values and that allows
for a subsequent transfer of control to a new address in the program based on the outcome of the
comparison.
This example introduces another kind of branch, oft en called an unconditional branch.
This instruction says that the processor always follows the branch. To distinguish between
conditional and unconditional branches, the MIPS name for this type of instruction is jump,
abbreviated as j (the label Exit is defined at the last).
Loops
Decisions are important both for choosing between two alternatives—found in if statements—
and for iterating a computation—found in loops. The same assembly instructions are the building
blocks for both cases.
Compiling a while Loop in C
Here is a traditional loop in C:
while (save[i] == k)
i += 1;
Assume that i and k correspond to registers $s3 and $s5 and the base of the
array save is in $s6. What is the MIPS assembly code corresponding to this C segment?
The first step is to load save[i] into a temporary register. Before we can load save[i] into a
temporary register, we need to have its address. Before we can add i to the base of array save to
form the address, we must multiply the index i by 4 due to the byte addressing problem.
Fortunately, we can use shift left logical, since shifting left by 2 bits multiplies by 22 or 4.
We need to add the label Loop to it so that we can branch back to that instruction at the end of
the loop:
Loop: sll $t1,$s3,2 # Temp reg $t1 = i * 4
To get the address of save[i], we need to add $t1 and the base of save in $s6:
add $t1,$t1,$s6 # $t1 = address of save[i]
Now we can use that address to load save[i] into a temporary register:
lw $t0,0($t1) # Temp reg $t0 = save[i]
Th e next instruction performs the loop test, exiting if save[i] ≠ k:
bne $t0,$s5, Exit # go to Exit if save[i] ≠ k
The next instruction adds 1 to i:
addi $s3,$s3,1 #i=i+1
The end of the loop branches back to the while test at the top of the loop. We just add the Exit
label after it, and we’re done:
j Loop # go to Loop
Exit:
Case/Switch Statement
Most programming languages have a case or switch statement that allows the
programmer to select one of many alternatives depending on a single value. The simplest way to
implement switch is via a sequence of conditional tests, turning the switch statement into a chain
of if-then-else statements. Sometimes the alternatives may be more efficiently encoded as a table
of addresses of alternative instruction sequences, called a jump address table or jump table, and
the program needs only to index into the table and then jump to the appropriate sequence. The
jump table is then just an array of words containing addresses that correspond to labels in the
code. Th e program loads the appropriate entry from the jump table into a register. It then needs
to jump using the address in the register. To support such situations, computers like MIPS
include a jump register instruction (jr), meaning an unconditional jump to the address specified
in a register. Then it jumps to the proper address using this instruction.
4. PC-relative addressing, where the branch address is the sum of the PC and a constant in the
instruction
5. Pseudodirect addressing, where the jump address is the 26 bits of the instruction concatenated
with the upper bits of the PC
Example: j 10000