21cs401 CA Unit I
21cs401 CA Unit I
Computer Types - Functional Units – Basic Operational Concepts - Number Representation and Arithmetic
Operations - Performance Measurements- Instruction Set Architecture: Memory Locations and Addresses -
Instructions and Instruction Sequencing - Addressing Modes
CONTROL UNIT
• It is also called as a brain of the processor.
• It fetches and analyses the instructions one-by-one and issue control signals to all other
units to perform various operations.
• For a given instruction, the exact set of operations required is indicated by the control
signals. The results of instructions are stored in memory.
• The component of the processor that commands the datapath, memory, and I/O devices
according to the instructions of the program.
MEMORY UNIT
• Memory is nothing but a storage device. It stores the program and data.
• It is divided into ‘n’ number of cells. Each cell is capable of storing one bit information at
a time.
• There are 2 classes of memory.1.Primary 2.Secondary.
Primary Memory
• It is made up of semiconductor material. So it is called Semiconductor memory
• Data storage capacity is less than secondary memory.
• Cost is too expensive than secondary memory.
• CPU can access data directly. Because it is an internal memory.
• Data accessing speed is very fast than secondary memory.
• Ex.RAM & ROM
RAM ROM
Random Access Memory Read Only Memory
Volatile memory Non volatile memory
Data lost when the power turns off and that is It retains data even in the absence of a power
used to hold data and program while they are source and that is store programs between runs.
running.
Temporary storage medium Permanent storage medium
User perform both read and write operation User can perform only read operation
RAM: There are two types of memory available namely, 1. SRAM 2. DRAM
SRAM DRAM
Static RAM Dynamic RAM
Information is stored in 1 bit cell called Flip Information is represented as charge across a
Flop. capacitor
Information will be available as long as power It retains data for few ms based on the charge of
is available capacitor even in the absence of a power
No refreshing is needed Refreshing is needed
Less packaging density High packaging density
More complex Hardware Less complex hardware
More expensive Less expensive
No random access Random access is possible
Access time 10 ns Access time 50 ns
Cache Memory: A small, fast memory that acts as a buffer for a slower, larger memory.
Secondary memory
• Secondary memory (Nonvolatile storage) is a form of storage that retains data
• even in the absence of a power source and that is used to store the programs between
runs.
• It is made up of magnetic material. So it is called magnetic memory.
• Data storage capacity is high than primary memory.
• Cost is too low than primary memory.
• CPU cannot access data directly. Because it is an external memory.
• Data accessing speed is very slow than primary memory.
• Ex. Magnetic disk, Hard Disk, CD, DVD, Floppy Disk
Magnetic Disk
• It consists of a collection of platters, which rotate on a spindle at 5400 revolution/min.
• The metal platters are covered with magnetic recording material on both sides.
• Also called hard disk. A form of nonvolatile secondary memory composed of rotating
platters coated with a magnetic recording material.
• Because they are rotating mechanical devices, access times are about 5 to 20
milliseconds and cost per gigabyte in 2012 was $0.05 to $0.10.
Optical Disk: Include both Compact Disk (CD) and Digital Video Disk(DVD).
Read-Only CD/DVD
• Data is recorded in a spiral fashion, with individual bits being recorded by burning
small pit.
• The disk is read by shining a laser at the CD surface and determining by examining the
reflected light whether there is a pit or flat surface.
Rewritable CD/DVD
• Use different recording surface that as a crystal line, reflective materials, pits are
formed that are not reflective.
Erase CD/DVD
• The surface is heated and cooled slowly, allowing an annealing process to restore the
surface recording layer to its crystalline structure.
Flash Memory
• A nonvolatile semiconductor memory. It is cheaper and slower than DRAM but more
expensive per bit and faster than magnetic disks. Access times are about 5 to 50
microseconds and cost per gigabyte in 2012 was $0.75 to $1.00.
OUTPUT UNIT
• A mechanism that conveys the result of a computation to a user, such as a display, or to
another computer.
Liquid Crystal Display
• A display technology using a thin layer of liquid polymers that can be used to transmit
or block light according to whether a charge is applied.
• All laptops and desktop computers use Liquid Crystal Display (LCD) to get a thin,
low-power display. A tiny transistor switch at each pixel to control current and make
sharper images.
• The image is composed of a matrix of picture elements or pixels, which can be
represented as a matrix of bits called bitmap.
• Depending on the size of the screen and the resolution, the display matrix ranges in size
from 640*480 to 2560*1600.
• A red, green, blue (RGB) associated with each dot on the display determines the
intensity of the three color components in the final image.
• Pixel: The smallest individual picture element. Screens are composed of hundreds of
thousands to millions of pixels, organized in a matrix.
2. Measuring Performance
CPU execution time: Also called CPU time. The actual time the CPU spends computing for
a specific task.
User CPU time: The CPU time spent in a program itself.
System CPU time: The CPU time spent in the operating system performing tasks on behalf of
the program.
Clock cycle: Also called tick, clock tick, clock period, clock, or cycle. The time for one
clock period, usually of the processor clock, which runs at a constant rate.
Clock period: This is the length of each clock cycle.
Clock rate: This is the inverse of the clock period.
3. CPU Performance and Its Factors
• A simple formula relates the most basic metrics (clock cycles and clock cycle time) to
CPU time.
• This formula makes it clear that the hardware designer can improve performance by
reducing the number of clock cycles required for a program or the length of the clock
cycle.
Example
4. Instruction Performance
• Execution time is that it equals the number of instructions executed multiplied by the
average time per instruction.
• Therefore, the number of clock cycles required for a program can be written as
Amdahl’s Law
A rule stating that the performance enhancement possible with a given improvement is
limited by the amount that the improved feature is used.
1.6 INSTRUCTION SET ARCHITECTURE:
MEMORY LOCATIONS AND ADDRESSES
▪ The memory consists of many millions of storage cells, each of which can store a bit of
information having the value 0 or 1.
▪ Because a single bit represents a very small amount of information, bits are handled
individually.
▪ The usual approach is to deal with them in groups of fixed size. For this purpose, the
memory is organized so that a group of n bits can be stored or retrieved in a single, basic
operation.
▪ Each group of n bits is referred to as a word of information, and n is called the word length.
▪ Modern computers have word lengths that typically range from 16 to 64 bits. If the word
length of a computer is 32 bits, a single word can store a 32-bit 2’s-complement number or
four ASCII characters, each occupying 8 bits
▪ A unit of 8 bits is called a byte.
▪ Machine instructions may require one or more words for their representation.
▪ Accessing the memory to store or retrieve a single item of information, either a word or a
byte, requires distinct names or addresses for each item location.
▪ It is customary to use numbers from 0 through 2k −1, for some suitable value of k, as the
addresses of successive locations in the memory.
▪ The 2 k addresses constitute the address space of the computer, and the memory can have
up to 2 k addressable locations.
▪ For example, a 24-bit address generates an address space of 224 (16,777,216) locations. This
number is usually written as 16M (16 mega), where 1M is the number 220 (1,048,576). A
32-bit address creates an address space of 232 or 4G (4 giga) locations, where 1G is 230.
Other notational conventions that are commonly used are K (kilo) for the number 210
(1,024), and T (tera) for the number 240.
BYTE ADDRESSABILITY
• There are three basic information quantities to deal with: the bit, byte, and word.
• A byte is always 8 bits, but the word length typically ranges from 16 to 64 bits.
• It is impractical to assign distinct addresses to individual bit locations in the memory.
• The most practical assignment is to have successive addresses refer to successive byte
locations in the memory.
• The term byte-addressable memory is used for this assignment. Byte locations have
addresses 0, 1, 2, . . .. Thus, if the word length of the machine is 32 bits, successive words
are located at addresses 0, 4, 8, . . ., with each word consisting of four bytes.
BIG-ENDIAN AND LITTLE-ENDIAN ASSIGNMENTS
• The name big-endian is used when lower byte addresses are used for the more significant
bytes (the leftmost bytes) of the word.
• The name little-endian is used for the opposite ordering, where the lower byte addresses are
used for the less significant bytes (the rightmost bytes) of the word.
WORD ALIGNMENT
• In the case of a 32-bit word length, natural word boundaries occur at addresses 0, 4,8, . . .,
as shown in the above Figure. The word locations have aligned addresses. In general, words
are said to be aligned in memory if they begin at a byte address that is a multiple of the
number of bytes in a word.
• The number of bytes in a word is a power of 2. Hence, if the word length is 16 (2 bytes),
aligned words begin at byte addresses 0, 2, 4, . . ., and for a word length of 64 (23 bytes),
aligned words begin at byte addresses 0, 8, 16, . . ..
Memory Operations
1. Load
2. Store
1. Load:
• The data transfer instruction that copies data from memory to a register is traditionally
called load.
• The format of the load instruction is the name of the operation followed by the register to
be loaded, then a constant and register used to access memory.
• The sum of the constant portion of the instruction and the contents of the second register
forms the memory address.
• The actual MIPS name for this instruction is lw, standing for load word.
Example: Let’s assume that A is an array of 100 words and that the compiler has associated the
variables g and h with the registers $s1 and $s2 as before. Let’s also assume that the starting
address, or base address, of the array is in$s3. Compile this C assignment statement:
g = h + A[8];
Solution:
lw $t0,32($s3) # Temporary reg $t0 gets A[8]
add $s1,$s2,$t0 # g = h + A[8]
The constant in a data transfer instruction (8) is called the off set, and the register added to
form the address ($s3) is called the base register.
2. Store:
• The instruction complementary to load is traditionally called store; it copies data from
a register to memory. The format of a store is similar to that of a load.
Example:
Assume variable h is associated with register $s2 and the base address of the array A is in $s3.
What is the assembly code for the C assignment statement below?
A[12] = h + A[8];
Solution:
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]
Instruction Fields:
op: operation code (opcode)
rs: first source register number
rt: second source register number
rd: destination register number
shamt: shift amount (00000 for now)
funct: function code (extends opcode)
Example:
2. I-Type 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 hardware knows whether to treat the last half of the instruction as three fields(R-type) or as a
single field (I-type).
3. J-Format:
j target- J-type is short for "jump type". The format of an J-type instruction looks like:
Opcode Address
The semantics of the j instruction (j means jump) are:
PC <- PC31-28 IR25-0 00
where PC is the program counter, which stores the current address of the instruction being executed.
You update the PC by using the upper 4 bits of the program counter, followed by the 26 bits of the
target (which is the lower 26 bits of the instruction register), followed by two 0's, which creates a
32 bit address.
LOGICAL OPERATIONS
The packing and unpacking of bits into words. These instructions are called logical
operations.
1. SHIFT:
3. OR
• A logical bit-by bit operation with two operands that calculates a 1 if there is a 1 in
either operand.
Ex: or $t0,$t1,$t2
4. 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.
5. 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. If one
operand is zero, then it is equivalent to NOT:
A NOR 0= NOT (A OR 0) = NOT (A).
• If the register $t1 is unchanged from the preceding example and register $t3has the
value 0, the result of the MIPS instruction \
nor $t0,$t1,$t3 # reg $t0 = ~ (reg $t1 | reg $t3)
• register $t0
1111 1111 1111 1111 1100 0011 1111 1111two
1.7.2 INSTRUCTION SEQUENCING
Conditional Operations
1. Branch to a labeled instruction if a condition is true. Otherwise, continue sequentially.
beq rs, rt, L1
if (rs == rt) branch to instruction labeled L1;
bne rs, rt, L1
if (rs != rt) branch to instruction labeled L1;
2. Set result to 1 if a condition is true. Otherwise, set to 0.
slt rd, rs, rt
if (rs < rt) rd = 1; else rd = 0;
slti rt, rs, constant
if (rs < constant) rt = 1; else rt = 0;
3. Use in combination with beq, bne
slt $t0, $s1, $s2 # if ($s1 < $s2)
bne $t0, $zero, L # branch to L
4. Jump Instructions
Unconditional jump to instruction labeled L1
j L1
Jump Address Table: Also called jump table. A table of addresses of alternative instruction
sequences.
5. Procedure call: jump and link.
Jump-and-Link Instruction: An instruction that jumps to an address and simultaneously saves
the address of the following instruction in a register($rain MIPS).
jal ProcedureLabel
Address of following instruction put in $ra.
Jumps to target address.
6. Procedure return: jump register
Return address: A link to the calling site that allows a procedure to return to the proper address in
MIPS it is stored in register $ra.
jr $ra
Copies $ra to program counter. Can also be used for computed jumps. e.g., for case/switch
statements.
Procedure: A stored subroutine that performs a specific task based on the parameters with
which it is provided.
Caller: The program that instigates a procedure and provides the necessary parameter values.
Callee: A procedure that executes a series of stored instructions based on parameters provided by
the caller and then returns control to the caller.
Program counter (PC): The register containing the address of the instruction in the program being
executed.
Stack: A data structure for spilling registers organized as a last-in first-out queue.
Stack pointer: A valued noting the most recently allocated address in a stack that shows where
registers should be spilled or where old register values can be found. In MIPS, it is register $sp.
Push: Add element to stack.
Pop: Remove element from stack.
Global pointer: The register that is reserved to point to the static area.
INSTRUCTIONS FOR MAKING DECISIONS –CONTROL OPERATIONS
• It is ability to make decisions. Based on the input data and the values created during
computation.
• 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;
Code
bne $s3,$s4,else # go to Else if i ≠ j
add $s0,$s1,$s2 # f = g + h (skipped if i ≠ j)
j Exit # go to Exit
else: sub $s0,$s1,$s2 # f = g – h (skipped if i = j)
Exit:
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.
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?
Code:
Loop: sll $t1,$s3,2 # Temp reg $t1 = i * 4
add $t1,$t1,$s6 # $t1 = address of save[i]
lw $t0,0($t1) # Temp reg $t0 = save[i]
bne $t0,$s5, Exit # go to Exit if save[i] ≠ k
addi $s3,$s3,1 #i=i+1
j Loop # go to Loop
Exit:
Basic Block
• A sequence of instructions without branches (except possibly at the end) and without
branch targets or branch labels (except possibly at the beginning).
Set on Less Than Instruction
• Compares two registers and sets a third register to 1 if the first is less than the second;
otherwise, it is set to 0.
• The MIPS instruction is called set on less than, or slt. For example,
slt $t0, $s3, $s4 # $t0 = 1 if $s3 < $s4
• It means that register $t0 is set to 1 if the value in register $s3 is less than the value in
register $s4; otherwise, register $t0 is set to 0.
• Constant operands are popular in comparisons, so there is an immediate version of the
set on less than instruction. To test if register $s2 is less than the constant10, we can
just write
slti $t0,$s2,10 # $t0 = 1 if $s2 < 10
Signed versus Unsigned Comparison
• Suppose register $s0 has the binary number
1111 1111 1111 1111 1111 1111 1111 1111two and that register $s1 has the binary
number 0000 0000 0000 0000 0000 0000 0000 0001two
What the values are of registers $t0 and $t1 after these two instructions?
Solution
slt $t0, $s0, $s1 # signed comparison
sltu $t1, $s0, $s1 # unsigned comparison
• The value in register $s0 represents -1ten if it is an integer and 4,294,967,295tenif it is
an unsigned integer.
• The value in register $s1 represents 1ten in either case. Then register $t0 has the value
1, since -1ten<1ten, and register $t1 has the value 0, since 4,294,967,295ten>1ten.
1.8 ADDRESSING MODE
Multiple forms of addressing are generically called addressing modes. The following diagram
shows how operands are identified for each addressing mode.
1. Register mode
The operand is the contents of a processor register; the name (address) of the register is given in the
instruction.
e.g. Move R1, R2
The processor registers are used as temporary storage locations where the data in a register are
accessed using the register mode. In the above data transfer ‘Move’ instruction R1, R2 are register
names and hence are register addressing modes.
2. Absolute mode (Direct Mode)
The operand is in a memory location; the address of this location is given explicitly in the
instruction.
Move LOC, R2
uses these two modes. Processor registers are used as temporary storage locations where the data
in a register are accessed using the Register mode. The Absolute mode can represent global
variables in a program. ‘LOC’ is a name for the memory address, so the data in memory is referred
in Direct mode.
3. Immediate mode — The operand is given explicitly in the instruction. Address and data constants
can be represented in assembly language using the Immediate mode.
Move #200, R0
places the value 200 in register R0. Clearly, the Immediate mode is only used to specify the value
of a source operand. A common convention is to use the sharp sign (#) in front of the value to
indicate that this value is to be used as an immediate operand.
4. Indirect mode
In the following addressing modes, the instruction does not give the operand or its address
explicitly. Instead, it provides information from which the memory address of the operand can be
determined. This address is referred to as the effective address (EA) of the operand. The effective
address of the operand is the contents of a register or memory location whose address appears in
the instruction.
Add (R1), R0
5. Index mode
The effective address of the operand is generated by adding a constant value to the contents of a
register. It is useful in dealing with lists and arrays.
The register used may be either a special register provided for this purpose, or, more commonly;
it may be any one of a set of general-purpose registers in the processor. In either case, it is
referred to as an index register. The Index mode is symbolically represented as
X(Ri )
where X denotes the constant value contained in the instruction and Ri is the name of the register
involved. The effective address of the operand is given by
EA = X + [Ri ]
6. Base with Index
A variation of the basic index addressing mode provides a very efficient access to memory
operands in practical programming situations. For example, a second register may be used to
contain the offset X, in which case we can write the Index mode as
(Ri,R j )
The effective address is the sum of the contents of registers Ri and Rj . The second register is
usually called the base register.
Effective Address (EA) = Base register (Ri) + Index register (Rj)
Can perform only Register to Register Can perform REG to REG or REG to MEM
Arithmetic operations or MEM to MEM
Simple and limited addressing modes. Complex and more addressing modes.
Here, addressing modes are less. Here, addressing modes are more.