Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
72 views181 pages

Unit 1 Coa

This document discusses computer organization and architecture. It defines a computer as a machine that accepts input, processes it according to stored instructions, and produces output. It describes the differences between computer architecture, which deals with functional behavior and hardware, and computer organization, which deals with structural relationships and performance at the implementation level. The key components of a computer system are also summarized, including the CPU, memory, input/output units, buses, and operating system software.

Uploaded by

atummalagunta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views181 pages

Unit 1 Coa

This document discusses computer organization and architecture. It defines a computer as a machine that accepts input, processes it according to stored instructions, and produces output. It describes the differences between computer architecture, which deals with functional behavior and hardware, and computer organization, which deals with structural relationships and performance at the implementation level. The key components of a computer system are also summarized, including the CPU, memory, input/output units, buses, and operating system software.

Uploaded by

atummalagunta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 181

COMPUTER ORGANIZATION

AND
ARCHITECTURE

E. Ilavarasan
What is a computer?
• A computer is a sophisticated electronic calculating machine
that:
– Accepts input information,
– Processes the information according to a list of internally
stored instructions (program) and
– Produces the resulting output information.

2
Block Diagram of Computer with Functional Units
COMPUTER COMPUTER
S.NO ARCHITECTURE ORGANIZATION

Architecture describes what Organization describes how it


1. the computer does. does it.
Deals with functional Deals with structural
2. behavior of computer system. relationship
It deals with high-level It deals with low-level design
3. design issue. issue.
Architecture indicates its Where, Organization indicates
4. hardware. its performance.
For designing a computer,
For designing a computer, its organization is decided after its
5. architecture is fixed first. architecture.
COMPUTER COMPUTER
S.NO ARCHITECTURE ORGANIZATION
Computer Architecture
comprises logical functions Computer Organization
such as instruction sets, consists of physical units like
registers, data types and circuit designs, peripherals and
6. addressing modes. adders.
Architecture coordinates Computer Organization
between the hardware and handles the segments of the
7. software of the system. network in a system.
Types of Computer Architecture
• Von-Neumann Architecture
• Based on the stored-program computer concept, where program
and data are stored in the same memory
• Harvard Architecture
• Harvard architecture is used when data and program code is
present in different memory blocks
• Instruction Set Architecture
• ISA describes the design of a Computer in terms of the basic
operations it must support.
• ISA has a set of instructions that the processor understands. It
has two instruction set one is RISC (reduced instruction set
computer) and CISC (complex instruction set computer).
Von Neumann Vs Harvard architecture
Types of Computer Architecture
• Microarchitecture
• It is the way a given ISA is implemented in a particular
processor
• The Microarchitecture is more concerned with the lower level
implementation of how the instructions are going to be executed
• For example, x86-64 is the ISA used by most
modern laptop and desktop computers. It is implemented by
various micro-architectures, including those designed
by Intel and AMD.
• Software that is compiled for the x86-64 ISA can run on any
micro-architecture designed to use the x86-64 instruction set
Computer Components: Top-Level View
Computer Architecture
Central Processing Unit (CPU)
• The Central Processing Unit (CPU) is the electronic circuit
responsible for executing the instructions of a computer program.
• It is sometimes referred to as the microprocessor or processor.
• The CPU contains the ALU, CU and a variety of registers.
Arithmetic and Logic Unit (ALU)
• The ALU perform arithmetic (add, subtract etc) and logic (AND,
OR, NOT etc) operations.
Registers
• Registers are high speed storage areas in the CPU.
• All data must be stored in a register before it can be processed.

Memory Address Holds the memory location of data that


MAR
Register needs to be accessed
Memory Data Holds data that is being transferred to
MDR
Register or from memory
Where intermediate arithmetic and
AC Accumulator
logic results are stored
Contains the address of the next
PC Program Counter
instruction to be executed
Current Instruction Contains the current instruction during
CIR
Register processing
Control Unit (CU)
• The control unit controls the operation of the ALU, memory
and input/ output devices
• Control unit generates timing signals which determines
“when” a particular operation should takes place.
Memory Unit
• The memory unit consists of RAM. This memory is fast and
directly accessible by the CPU.
• RAM has two parts address and its data / contents (binary).
• The address will uniquely identify every location in the memory
• Loading program and data from permanent memory (hard
drive), into RAM, allows the CPU to operate much quicker
Memory unit
• Memory unit stores instructions and data.
• Processor reads instructions and reads/writes data from/to the
memory during the execution of a program.
– In practice, a group of bits is fetched at a time.
– Group of bits stored or retrieved at a time is termed as word
– Number of bits in a word is termed as the “word length” of
a computer.
• In order to write/read a word to and from memory, a processor
should know where to look:
– “Address” is associated with each word location.

13
Memory unit (contd..)
• Random Access Memory (RAM) provides fixed access time
independent of the location of the word.
– Access time is known as “Memory Access Time”.
• Memory and processor have to “communicate” with each
other in order to read/write information.
– To reduce “communication time”, a small amount of RAM
(known as Cache) is tightly coupled with the processor.
• Modern computers have three to four levels of RAM units
with different speeds and sizes:
– Fastest, smallest known as Cache
– Slowest, largest known as Main memory.

14
Memory unit (contd..)
• Primary storage is insufficient to store large amounts of data
and programs. We can increase capacity, but it is expensive.
• To retain data/information permanently, secondary storage
devices are required
• We Store large amounts of data on secondary storage devices:
– Magnetic disks and tapes,
– Optical disks (CD-ROMs).
– Access to the data stored in secondary storage in slower,
compared to the primary memory
• Cost of a memory unit depends on its access time, lesser
access time implies higher cost.

15
Input unit
Binary information must be presented to a computer in a specific
format. This task is performed by the input unit:
- Interfaces with input devices.
- Accepts binary information from the input devices.
- Presents this binary information in a format expected by the
computer.
- Transfers this information to the memory or processor.

Real world Computer


Memory
Keyboard
Audio input
…… Input Unit

Processor

16
Output unit
- Interface with output devices.
- Accept processed results provided by the computer in specific
binary form.
- Convert the information in binary form to a form understood by
an output device.

Computer
Memory
Real world

Output Unit
Printer
Graphics display
Processor Speakers
……

17
How are the functional units connected?
•For a computer to achieve its operation, the functional units need
to communicate with each other.
•In order to communicate, they need to be connected.

• Functional units may be connected by a group of parallel wires.


called a bus.
• Each wire in a bus can transfer one bit of information.
•The number of parallel wires in a bus is equal to the word length
of a computer
18
Buses
• A standard CPU system bus is comprised of a control bus, data
bus and address bus.
• Address Bus- Carries the addresses of data between the
processor and memory and other devices
• Data Bus- Carries data between the processor, the memory
unit and the input/output devices
• Control Bus- Carries control signals/commands from the CPU
(and status signals from other devices) in order to control and
coordinate all the activities within the computer
Computer System Components
Software- Operating System
• An operating system is the most important system software that
runs on a computer
• Every general-purpose computer must have an operating system
to run other programs and applications
• An operating system acts as an interface between the user of a
computer and computer hardware.
• It manages the computer's hardware (memory, CPU input and
output etc..) resources, as well as all of its software
• The purpose of an operating system is to provide an
environment in which a user can execute programs in a
convenient and efficient manner.
Computer booting
• When booting, the computer
performs the following tasks.
• Pressing the power button on
the computer starts up the
power supply, which
subsequently provides power
to all other hardware inside
the computer
• A self diagnostic is
performed, also known as
a POST, to check if all
hardware in the computer is
working properly.
Computer booting Process contd…
• The BIOS checks the hard drive for the boot loader, located in
the first sector of the hard drive.
• The boot loader looks for the operating system on the hard
drive and begins loading the OS (e.g., Linux, or Windows).
• Hardware drivers are loaded, allowing the operating system to
interact and utilize the hardware components
• If configured in the operating system, a login screen is
displayed, allowing a user to enter a username and password to
log in.
• The computer becomes ready for operation
Instructions
• Instructions specify commands to:
– Transfer information within a computer (e.g. from memory to ALU)
– Transfer of information between the computer and I/O devices (e.g.,
from keyboard to computer, or computer to printer)
– Perform arithmetic and logic operations (e.g., Add two numbers,
Perform a logical AND).
• A sequence of instructions to perform a task is called a program,
which is stored in the memory.
• Processor fetches instructions that make up a program from the
memory and performs the operations stated in those instructions.
• What do the instructions operate upon?

25
Data
• Data are the “operands” upon which instructions operate.
• Data could be:
– Numbers,
– Encoded characters.
• Data, in a broad sense means any digital information.
• Computers use data that is encoded as a string of binary digits
called bits.

26
Basic Operational Concepts: Instruction Execution
Assembly Program
Add R0, R1
Move #10, R0

High Level Language


register int R0, R1
R1= R0, R1
R0 = R0 +10

Main Memory
Address Content
1000 Add / Op-code
1001 Move / Op-code
1002 10 / Data
1003 -
Execution of typical Instruction
• (PC)  (MAR)
- The contents of PC transferred to MAR
• (MAR)  (Address bus)
- Select a particular memory location
• CU issues RD control signal
• Reads instruction present in memory and loaded into MDR
• MDR content will be placed in IR ( MDR  IR )
• Instruction code in IR is decoded by CPU to understand the
type of operation it has to perform
• Increments the contents of PC by 1 (to point next instruction)
• Perform the operation, if data is available in register (CPU)
Execution of typical Instruction contd..
• If data is present in memory following sequence is performed
• Address of the data in PC  MAR
• MAR  Address bus
• Select memory location based on MAR and generated RD signal
• Reads data via data bus  MDR
• From MDR data can be directly routed to ALU or it can be
placed in register and then operation can be performed
• Results of the operation can be directed towards output device,
memory or register
• One or more steps are performed in one clock pulse
Example : Add R1, R2

T1  Enable R1

T2  Enable R2

Enable ALU for addition operation


T3 

T4 
•Control unit works with a reference
signal called processor clock
T1
•Processor divides the operations
T2 into basic steps

•Each basic step is executed in one


clock cycle

R1 R2

R2
Evolution of Computers
FIRST GENERATION (1945 – 1955)
• Program and data reside in the same memory (stored program
concepts – John von Neumann)
• First generation computers relied on ‘machine language’ which
is the most fundamental programming language that can be
understood by computers
• Vacuum tubes were used to implement (ALU & CU design)
• Magnetic core and magnetic tape storage devices are used
• Using electronic vacuum tubes, as the switching components
• The two eminent machines of this era were the UNIVAC
(Universal Automatic Computer) and ENIAC (Electronic
Numerical Integrator and Computer) machines
SECOND GENERATION (1955 – 1965)
• Transistor were used to design ALU & CU
• Computers moved from machine language to symbolic, or
assembly languages, which allowed programmers to specify
instructions in words. (ADD, SUB, INR.. etc)
• Symbolic (‘assembly’) languages were used to write programs
• High-level Programming languages (early versions of COBOL
and FORTRAN) were being developed
• To convert HLL to ML compiler were used
• Separate I/O processor were developed to operate in parallel
with CPU, thus improving the performance
• Invention of the transistor which was faster, smaller and
required considerably less power to operate
• Consume less electricity and generate less heat
THIRD GENERATION (1965-1975)
• IC technology improved
• Improved IC technology helped in designing low cost, high
speed processor and memory modules
• First computers where users interacted utilizing keyboards and
monitors with the help of operating systems (OS)
• OS- system program controls the resources of computers
• Multiprogramming, pipelining concepts were incorporated
• Cache and virtual memory concepts were developed
• More than one circuit on a single silicon chip became available
FOURTH GENERATION (1975-1985)
• The microprocessor brought the fourth generation of
computers, as thousands of integrated circuits were built onto a
single silicon chip using VLSI technology
• INTEL, MOTOROLA, TEXAS,NATIONAL semiconductors
started developing microprocessor (CPU /Processor)
• Workstations, microprocessor (PC) & Notebook computers
were developed
• Different computers are interconnected for better
communication by LAN, MAN,WAN
• Fourth generation computers also saw the development
of GUIs, the mouse and handheld devices.
• Processors like Digital Signal Processor were also developed
BEYOND THE FOURTH GENERATION
(1985 – TILL DATE)
• Fifth generation computing devices, based on artificial
intelligence, are still in development, though there are some
applications, such as voice recognition, that are being used
today
• The goal of fifth-generation computing is to develop devices
that respond to natural language input and are capable of
learning and self-organization
• E-Commerce, E- banking, home office
• ARM, AMD, INTEL, MOTOROLA
• High speed processor - GHz speed
COMPUTER TYPES

Computers are classified based on the


parameters like
• Speed of operation
• Cost
• Computational power
• Type of application
DESKTOP COMPUTER
• The most common computer is the personal computer
• Processing & storage units, visual display & audio units,
keyboards
• Storage media-Hard disks, CD-ROMs
• Eg: Personal computers which is used in homes and offices
• Advantage: Cost effective, easy to operate, suitable for general
purpose educational or business application

NOTEBOOK COMPUTER
• Compact form of personal computer (laptop)
• Advantage is portability
WORK STATIONS
• More computational power than PC
• Costlier
• Used to solve complex problems which arises in
engineering application (graphics, CAD/CAM etc)

ENTERPRISE SYSTEM (MAINFRAME)


•More computational power
•Larger storage capacity
•Used for business data processing in large organization
•Commonly referred as servers or super computers
SERVER SYSTEM
• Supports large volumes of data which frequently need to
be accessed or to be modified
• Supports request response operation

SUPER COMPUTERS
• Faster than mainframes
• Helps in calculating large scale numerical and algorithm
calculation in short span of time
• Used for aircraft design and testing, military application
and weather forecasting
HANDHELD
• Also called a PDA (Personal
Digital Assistant).
• A computer that fits into a
pocket, runs on batteries,
and is used while holding
the unit in your hand.
• Typically used as an
appointment book, address
book, calculator, and
notepad.
• Can be synchronized with a
personal microcomputer as
a backup.
Basic Terminology
• Computer • Software
– A device that accepts input, – A computer program that
processes data, stores tells the computer how to
data, and produces output, perform particular tasks.
all according to a series of
stored instructions.
• Network
– Two or more computers
• Hardware
and other devices that are
– Includes the electronic and connected, for the purpose
mechanical devices that of sharing data and
process the data; refers to programs.
the computer as well as
peripheral devices.
• Peripheral devices
– Used to expand the
computer’s input, output
and storage capabilities.
Basic Terminology
Interpreter
Converts HLL to MLL, does this job statement by
statement

System software
Program routines which aid the user in the
execution of programs eg: Assemblers,
Compilers

Operating system
Collection of routines responsible for controlling
and coordinating all the activities in a computer
system
Clock
• Synchronizes Processor and Bus operations
• Clock cycle = Clock period = 1 / Clock rate

Cycle 1 Cycle 2 Cycle 3

• Clock rate = Clock frequency = Cycles per second


• Cycle per second is also called Hertz
– 1 Hz = 1 cycle/sec 1 KHz = 103 cycles/sec
– 1 MHz = 106 cycles/sec 1 GHz = 109 cycles/sec
– 2 GHz clock has a cycle time = 1/(2×109) = 0.5
nanosecond (ns)
• Clock cycles measure the execution of instructions
Time Calculation
• 8085- based Microprocessor with 4 MHz clock frequency
• For Instruction of MOV A,B, take 4 t-state or clock cycles
• Clock frequency f = 4 MHz
• Clock period T = 1/f = 1/4 x 10-6
= 0.25 μs
• Time to execute MOV A, B
= 4 t-states x 0.25 μs
= 1 μs
• For Processor with 4 GHz clock
• T = 1/f = 1/4 x 10-9
= 0.25 ns
• Time to execute MOV A, B
= 4 t-states x 0.25 ns = 1 ns
MOV A,B – timing
• T1 : microprocessor places the memory
address from PC onto address bus
• T2: Control unit sends RD signal to enable memory
• T3: Instruction ( op-code) from memory location is
placed in data bus
• T4: Op-code is placed in IR register and is decoded and
executed
Basic Performance Equation
• The processor time required to execute a program is given by
T = N x S / R, where,
– N is the actual number of machine instructions executions
– S is the average number of basic steps needed to execute
one machine instruction, where each basic step is executed
in one clock cycle
– R is the clock rate in cycle per second
Note : Actual number of instruction execution is not necessarily equal
to the number of machine instructions in the object program
Higher performance is achieved by reducing N and S values or/and
increasing the R
Problem
A program contains 1000 instructions. Out of that 25%
instructions requires 4 clock cycles, 40% instruction
requires 5 cycles and remaining requires 3 clock cycles for
execution. Find the total time required to execute the
program in a 1 GHz machine

Solution
N =1000
25% of N = 250 instructions require 4 clock cycles
40% of N = 400 instructions require 5 clock cycles
35% of N = 350 instructions require 3 clock cycles
T = (N x S) R
= (250*4+400*5+350*3) / 1x109
= (1000+2000+1050) / 1x109
= 4.05 μs
Performance Measurement
• Computer designer use performance estimates to
evaluate the effectiveness of new features
– Practice is to use benchmarks available to all
manufacturers e.g. SPEC benchmark (www.spec.org)
– SPEC rating of 50 means that the computer under
test is 50 times faster than the reference computer.
– The test is repeated for various programs in the
SPEC suite. The overall rating for the computer is
given by
Running time on the reference computer
SPEC rating 
Running time on the computer under test
n 1
SPEC rating  (  SPEC i ) n

i 1

where n is the number of programs


49
Instruction Execution Cycle
• Fetch PC program
• Decode
I-1 I-2 I-3 I-4
• Fetch operands
• Execute memory fetch
• Store output op1
read
op2
registers registers
instruction
I-1 register

decode
write

write

flags ALU

execute
(output)
50
Instruction pipelining
• Instruction pipelining is a technique that implements a
form of parallelism called instruction-level
parallelism within a single processor.
• It therefore allows faster CPU throughput (the number of
instructions that can be executed in a unit of time) than
would otherwise be possible at a given clock rate.
• The basic instruction cycle is broken up into a series
called a pipeline. Rather than processing each instruction
sequentially (finishing one instruction before starting the
next), each instruction is split up into a sequence of
dependent steps so different steps can be executed
in parallel and instructions can be processed
concurrently (starting one instruction before finishing the
previous one).
A non-pipelined processor. Many wasted cycles.

Stages
S1 S2 S3 S4 S5 S6
1 I-1
2 I-1
3 I-1
4 I-1
5 I-1
Cycles

6 I-1
7 I-2
8 I-2
9 I-2
10 I-2
11 I-2
12 I-2
52
Pipelined Execution
• More efficient use of cycles, greater throughput of instructions:

S ta g e s
S1 S2 S3 S4 S5 S6
1 I-1
2 I-2 I-1
Cycles

3 I-2 I-1
4 I-2 I-1
5 I-2 I-1
6 I-2 I-1
7 I-2

For k states and n instructions, the number of


required cycles is: k + (n – 1)
Superscalar Processor
• A superscalar processor is a CPU that implements a form
of parallelism called instruction-level parallelism within a
single processor.
• A superscalar processor can execute more than one instruction
during a clock cycle by simultaneously dispatching multiple
instructions to different execution units on the processor.
Hence, provide higher throughput
• The CPU dynamically checks for data dependencies between
instructions at run time
• Each execution unit is not a separate processor (or a core if the
processor is a multi-core processor), but an execution resource
within a single CPU such as an arithmetic logic unit.
Simple superscalar pipeline.
By fetching and dispatching two instructions at a time, a maximum
of two instructions per cycle can be completed.
(IF = Instruction Fetch, ID = Instruction Decode, EX = Execute,
MEM = Memory access, WB = Register write back, i = Instruction
number, t = Clock cycle [i.e., time])
Multi-core Processor
• A multi-core processor is a single computing component with
two or more independent actual processing units (called
"cores"), which are units that read and execute program
instructions.
• The instructions are ordinary CPU instructions (such as add,
move data, and branch), but the single processor can run
multiple instructions on separate cores at the same time,
increasing overall speed for programs amenable to parallel
computing.
• Manufacturers typically integrate the cores onto a
single integrated circuit die (known as a chip multiprocessor or
CMP), or onto multiple dies in a single chip package.
A generic dual-core processor with CPU-local
level-1 caches and a shared, on-die level-2 cache.
Multiprocessors and Multicomputers
• The multiprocessor can be viewed as a parallel computer with a
main memory system shared by all the processors
• Tightly-coupled systems
• Tasks they execute communicate data by read and write operations
on shared memory
• The multicomputer can be viewed as a distributed computing in
which each processor has its own local memory.
• The memory address space is not shared among the processors; that
is, a processor only has direct access to its local memory and not to
the other processors' local memories.
• Loosely-coupled systems
• Tasks they execute communicate data by exchanging messages over
communication network (send, receive)
Distributed Systems
• A distributed system is a collection of
independent computers that appears to its users
as a single coherent system
• A distributed system is a system in which
components are located on different networked
computers, which can communicate and
coordinate their actions by passing messages to
one another.
• The components interact with one another in
order to achieve a common goal

Distributed Systems 59
Architecture of Distributed Systems

Distributed Systems 60
Distributed Operating System

DOS runs on multiple, autonomous CPUs which provides


its users an illusion of an ordinary Centralized Operating
System that runs on a Virtual Uniprocessor
Distributed Systems 61
Parallel Vs Distributed Systems

Distributed Systems 62
MEMORY LOCATIONS AND ADDRESSES

• Main memory is the second major subsystem in a computer.


It consists of a collection of storage locations, each with a
unique identifier, called an address
• Data is transferred to and from memory in groups of bits
called words
words.. A word can be a group of 8 bits, 16 bits, 32 bits
or 64 bits
• If the word is 8 bits, it is referred to as a byte
byte.. The term “byte”
is so common in computer science that sometimes a 16- 16-bit
word is referred to as a 2-2-byte word, or a 32-
32-bit word is
referred to as a 4-
4-byte word.
Main Memory (MM) Organization
• In a main memory with address
0 word 0
m-bit addresses, word 1
1
0 to 2m-1 words are available. 2 word 2
:
:
• Each word stores n bits
:
i word i
• m, n are independent

• m specifies the number of


units; n specifies the unit size
m
2 -1 word 2m -1

m bits n bits

64
10-address lines, 16-data lines
Address space
• The total number of uniquely identifiable locations in
memory is called the address space.

• For example, a memory with 64 kilobytes (16


address line required) and a word size of 1 byte has an
address space that ranges from 0 to 65,535.
i
Memory addresses are defined using unsigned
binary integers.
Example 1
A computer has 32 MB (megabytes) of memory.
memory. How many bits
are needed to address any single byte in memory?
Solution
The memory address space is 32 MB, or 225 (25 × 220). This
means that we need log2 225, or 25 bits
bits,, to address each byte
byte..

Example 2
A computer has 128 MB of memory.
memory. Each word in this computer
is eight bytes.
bytes. How many bits are needed to address any single
word in memory?
Solution
The memory address space is 128 MB, which means 227.
However, each word is eight (23) bytes, which means that we
have 224 words
words.. This means that we need log2 224, or 24 bits
bits,, to
address each word
word..
Memory: Contents of a Word (I)
Here is an example of a 32-bit word.

b31 b30 b1 b0

32 bits (word length)


A word can store information. For example,

1. Four English characters, each encoded in a common 8-bit code


ASCII: American Standard Code for Information Interchange
or
EBCDIC: Extended Binary Coded Decimal Interchange Code

char 3: 8-bit char 2: 8-bit char 1: 8-bit char 0: 8-bit


69
Memory: Contents of a Word (II)
2. Two Chinese characters
16 bits 16 bits
Big5/GB code
3. A 32-bit Signed integer
b31 b30 b1 b0
sign bit
b31=0 means positive integer, b31=1 means negative integer
magnitude = b30230 + … + b0 20
4. A 32-bit machine instruction
8 bits 12 bits 12 bits
operation address field address field 70
code
Assignment of byte addresses
• Little Endian (e.g., in DEC, Intel)
» low order byte stored at lowest address
» byte 0 byte1 byte 2 byte 3

• Eg: 46,78,96,54 (32 bit data)


• H BYTE L BYTE

• 8000 54
• 8001 96
• 8002 78
• 8003
46
• 8004
|
Big Endian
• Big Endian (e.g., in IBM, Motorolla, Sun, HP)
» high order byte stored at lowest address
» byte 3 byte 2 byte 1 byte 0

• Programmers/protocols should be careful when


transferring binary data between Big Endian and Little
Endian machines
Big-Endian and Little-Endian Assignments
Big-Endian: lower byte addresses are used for the most significant
bytes of the word
Little-Endian: opposite ordering. lower byte addresses are used for the
less significant bytes of the word
Word
address Byte address Byte address

0 0 1 2 3 0 3 2 1 0

4 4 5 6 7 4 7 6 5 4

• •
• •
• •

k k k k k k k k k k
2 -4 2 -4 2 -3 2- 2 2 - 1 2 - 4 2- 1 2 - 2 2 -3 2 -4

(a) Big-endian assignment (b) Little-endian assignment

Byte and word addressing.


• In case of 16 bit data, aligned words begin at byte
addresses of 0,2,4,………………………….
• In case of 32 bit data, aligned words begin at byte
address of 0,4,8,………………………….
• In case of 64 bit data, aligned words begin at byte
addresses of 0,8,16,………………………..
• In some cases words can start at an arbitrary byte
address also then, we say that word locations are
unaligned
MEMORY OPERATIONS
• Today, general
general--purpose computers use a set of
instructions called a program to process data.
• A computer executes the program to create output data
from input data
• Both program instructions and data operands are stored
in memory
• Two basic operations requires in memory access
• Load operation (Read or Fetch)-Contents of
specified memory location are read by processor
• Store operation (Write)- Data from the processor
is stored in specified memory location
• INSTRUCTION SET ARCHITECTURE:-
Complete instruction set of the processor

• BASIC 4 TYPES OF OPERATION:-


• Data transfer between memory and
processor register
• Arithmetic and logic operation
• Program sequencing and control
• I/O transfer
Register transfer notation (RTN)
Transfer between processor registers & memory,
between processor register & I/O devices
Memory locations, registers and I/O register names
are identified by a symbolic name in uppercase
alphabets
• LOC,PLACE,MEM are the address of memory
location
• R1 , R2,… are processor registers
• DATA_IN, DATA_OUT are I/O registers
• Contents of location is indicated by using square
brackets [ ]

• RHS of RTN always denotes a values, and is


called Source

• LHS of RTN always denotes a symbolic name


where value is to be stored and is called destination

• Source contents are not modified

• Destination contents are overwritten


Examples of RTN statements

• R2 [LOCN]

• R4 [R3] +[R2]
ASSEMBLY LANGUAGE NOTATION (ALN)
• RTN is easy to understand and but cannot be
used to represent machine instructions
• Mnemonics can be converted to machine
language, which processor understands using
assembler
Eg:
1. MOVE LOCN, R2
2. ADD R3, R2, R4
3. ADD B A= A + B
4. MOV A, B A=B
A Typical Instruction
• Add LOCA, R0
• Add the operand at memory location LOCA to the
operand in a register R0 in the processor.
• Place the sum into register R0.
• The original contents of LOCA are preserved.
• The original contents of R0 is overwritten.
• Instruction is fetched from the memory into the processor
– the operand at LOCA is fetched and added to the
contents of R0 – the resulting sum is stored in register R0.
TYPE OF INSTRUCTION
Three address instruction
• Syntax: Operation source 1, source 2, destination
• Eg: ADD D, E, F where D,E,F are memory location
• Advantage: Single instruction can perform the complete
operation
• Disadvantage : Instruction code will be too large to fit in
one word location in memory
TWO ADDRESS INSTRUCTION
•Syntax : Operation source, destination

•Eg: MOVE E, F F=E

ADD D, F F = F+D
ADD A, B B = A+ B

The operation C =[A]+[B] can be performed with


2 instructions
MOVE B, C
ADD A, C
Disadvantage: Single instruction is not sufficient to
perform the entire operation.
ONE ADDRESS INSTRUCTION

• Syntax- Operation source / destination


• In this type either a source or destination
operand is mentioned in the instruction
• Other operand is implied to be a
processor register called Accumulator
• Eg: ADD B (general)
• Load D; ACC [memlocation _D]
• ADD E; ACC (ACC) +(E)
• STORE F; memlocation_ F (ACC )
Zero address instruction
• Location of all operands are defined implicitly
• Operands are stored in a structure called
pushdown stack
C=A+B
 If processor supports ALU operations one data in
memory and other in register then the instruction
sequence is
• MOVE A, Ri
• ADD B, Ri
• MOVE Ri, C
 If processor supports ALU operations only with registers
then one has to follow the instruction sequence given
below
F=D+E
• LOAD D, Ri
• LOAD E, Rj
• ADD Ri, Rj
• MOVE Rj, F
Basic Instruction Cycle
• Basic computer operation cycle
– Fetch the instruction from memory into a control
register (PC)
– Decode the instruction
– Locate the operands used by the instruction
– Fetch operands from memory (if necessary)
– Execute the operation in processor registers
– Store the results in the proper place
– Go back to step 1 to fetch the next instruction
INSTRUCTION EXECUTION & STRIAGHT LINE
SEQUENCING
Address Contents
Begin execution here i Move A,R0
i+4 Add B,R0
3-instruction program
i+8 Move R0,C
. segment
.
.

A
.
.
.

B Data for Program


. C [A]+[B]
.

C
• PC – Program counter: hold the address of the
next instruction to be executed
• Straight line sequencing: If fetching and
executing of instructions is carried out one by
one from successive addresses of memory, it is
called straight line sequencing.
• Major two phase of instruction execution
• Instruction fetch phase: Instruction is fetched
form memory and is placed in instruction register
IR
• Instruction execute phase: Contents of IR is
decoded and processor carries out the
operation either by reading data from memory or
registers.
BRANCHING

A straight line program for adding n


numbers
Using a loop to add n numbers
Adding N numbers
• Move N, R1
• Clear Ro
• Move Num1, Next
• Loop ADD Next, Ro
• INC Next
• DEC R1
• Branch > 0 Loop
• Move R0, Sum
BRANCHING
• Branch instruction are those which changes the normal
sequence of execution.
• Sequence can be changed either conditionally or
unconditionally.
• Accordingly we have conditional branch instructions and
unconditional branch instruction.
• Conditional branch instruction changes the sequence only
when certain conditions are met.
• Unconditional branch instruction changes the sequence of
execution irrespective of condition of the results.
CONDITION CODES
 CONDITIONAL CODE FLAGS: The processor keeps track
of information about the results of various operations for use
by subsequent conditional branch instructions

• N – Negative 1 if results are Negative


0 if results are Positive
• Z – Zero 1 if results are Zero
0 if results are Non zero
• V – Overflow 1 if arithmetic overflow occurs
0 non overflow occurs
• C – Carry 1 if carry and from MSB bit
0 if there is no carry from MSB bit
Figure Format and different instruction types
Types of Addressing Modes
The different ways in which the location of the operand is specified
in an instruction are referred to as addressing modes

• Immediate Addressing
• Direct Addressing
• Indirect Addressing
• Register Addressing
• Register Indirect Addressing
• Relative Addressing
• Indexed Addressing
• Auto Increment Mode
• Auto Decrement Mode
Implementation of Variables and Constants
• In assembly language, a variable is represented by allocating a
register or a memory location to hold its value.
• We access an operand by specifying the name of the register or
the address of the memory location

• Effective Address (EA):


• Effective address is the address of the exact memory
location where the value of the operand is present
Immediate Addressing
• Operand is given explicitly in the instruction
• Operand = Value
• e.g. ADI 05 ; A= A+5
– Add 05 to contents of accumulator
– 5 is operand
• e.g Move #200, R0 ; R0 = 200
(sharp sign # is used to indicate a value)
• No memory reference to fetch data
• Fast
Instruction
• Limited range
opcode operand
A = B+6
• Move B, R1
• ADD #6, R1
• Move R1, A
Absolute/ Direct Addressing
• Address field contains address of operand
• Effective address (EA) = address field (A)
• e.g. Move LOC, R2
– The contents of memory Location LOC is copied into
register R2
– Look in memory at address LOC for operand
• Single memory reference to access data
• No additional calculations to work out effective address
Direct Addressing Diagram
Move LOC, R2

Instruction
Opcode Address LOC
Memory

Operand / 200

After Execution R2 = 200


Indirect Addressing
• Some instructions des not give the operand or its address
explicitly, instead, they provide information from which the
memory address of the operand is determined. This address is
referred as effective address
• In the indirect addressing, the effective address of the operand
is the contents of register or memory location whose address
appears in the instruction
• The register or memory location that contains the address of an
operand is called a pointer
Indirect Addressing Diagram
Instruction
Opcode Address A
Memory

Pointer to operand

Operand
Indirect Addressing
• Add (R1), R0 Add (A), R0
Main Memory Main Memory
Add (R1), R2 Add (A), R2
- -
- -
B Operand /20 A B (Memory Address)

B Operand /20

Registers Registers

R1 B (Memory Address) R1
R2 10 R2 10

R2 30 R2 30
Through a general purpose register Through a memory location
Register Addressing
• The instruction which uses processor registers to represent
operands is the instruction in register addressing mode.
• The effective address is a register where the value of the
operand is present. i.e. EA=R
• Advantage: In the register addressing mode there are no
memory references as the value to be operated is present in the
register.
• Very fast execution
• Disadvantage: Registers have limited address space. So, it has
a limit on the size of value that can be stored.
Register Addressing
• ADD R4, R3 R3 = R4+R3

Instruction
Indexed Addressing
• The effective address of the operand is generated by
adding a constant value to the contents of a index
register / GP register
• EA = X + [R]
• Address field hold two values
– X = constant value (offset) / displacement
– R = register that holds address of memory locations
– or vice versa
(Offset given as constant or in the index register)
Add 20(R1),R2 or Add 1000(R1),R2
Indexed Addressing Diagram

Instruction
Opcode Register R Constant Value
Memory

Registers

Pointer to Operand + Operand


Relative Addressing
• A version of displacement addressing
• Here, the effective address is determined by the index
mode using the program counter in place of the GP
register
• R = Program counter, PC
• EA = X + (PC)
• i.e. get operand from X bytes away from current location
pointed to by PC
• c.f locality of reference & cache usage
Auto increment mode
• The effective address of the operand is the contents of a
register specified in the instruction.
• After accessing the operand, the contents of this register
are automatically incremented to point to the next item in
the list
• Automatically increment the contents of the register by a
value that corresponds to the size of the accessed
operand
• Increment by 1 for byte-sized operand, 2 for 16-bit
operands, 4 for 32-bit operands in a byte addressable
memory
• EA = [R1], increment Ri ----- (Ri)+
Eg: Add (R2)+,R0
Auto decrement mode
• The contents of a register specified in the instruction are
first automatically decremented and are then used as the
effective address of the operand

• Decrement Ri; EA= [Ri] ----- - (Ri)


• Note that the address is decremented before it is used in
the autodecrement and incremented after it is used in
the autoincrement
Adding list of Nos. (indirect addressing)
• Move N, R1 N – No. of Nos.
• Move #Num, R2 Num - address
• Clear R0
• LOOP Add (R2), R0 Address Data
• Add #4, R2 1000 32-bit Data
• Decrement R1 1004 32-bit Data
• Branch > 0 LOOP
1008 32-bit Data
• Move R0, Sum
100C 32-bit Data
Adding list of Nos. (auto increment mode)
• Move N, R1 N – No. of Nos.
• Move #Num, R2 Num - address
• Clear R0
• LOOP Add (R2)+, R0 Address Data
• Decrement R1 1000 32-bit Data
• Branch >0 LOOP 1004 32-bit Data
• Move R0, Sum
1008 32-bit Data

100C 32-bit Data


Instruction Set Architecture
• RISC (Reduced Instruction Set Computer) Architectures
– Memory accesses are restricted to load and store instruction, and data
manipulation instructions are register to register.
– Addressing modes are limited in number.
– Instruction formats are all of the same length.
– Instructions perform elementary operations
– Large register file
– Control unit: hardwired
• CISC (Complex Instruction Set Computer) Architectures
– Memory access is directly available to most types of instruction.
– Addressing mode are substantial in number.
– Instruction formats are of different lengths.
– Instructions perform both elementary and complex operations.
– Register file: smaller than in a RISC
– Control unit: micro-programmed
Characteristics of RISC Vs CISC
processors
No RISC CISC
1 Simple instructions taking one Complex instructions taking
cycle multiple cycles
2 Instructions are executed by Instructions are executed by
hardwired control unit microprogramed control unit
3 Few instructions Many instructions

4 Fixed format instructions Variable format instructions

5 Few addressing mode, and most Many addressing modes


instructions have register to
register addressing mode
6 Multiple register set Single register set

7 Highly pipelined Not pipelined or less pipelined


Assembly Language
Adding list of Nos. (indirect addressing)
• Move N, R1 N – No. of Nos.
• Move #Num, R2 Num - address
• Clear R0
• LOOP Add (R2), R0 Address Data
• Add #4, R2 1000 32-bit Data
• Decrement R1 1004 32-bit Data
• Branch > 0 LOOP
1008 32-bit Data
• Move R0, Sum
100C 32-bit Data
Adding list of Nos. (auto increment mode)
• Move N, R1 N – No. of Nos.
• Move #Num, R2 Num - address
• Clear R0
• LOOP Add (R2)+, R0 Address Data
• Decrement R1 1000 32-bit Data
• Branch >0 LOOP 1004 32-bit Data
• Move R0, Sum
1008 32-bit Data

100C 32-bit Data


Assembler and Loader
• Programming language processor that translates an
assembly language program (the source program) to the
machine language program (the object program)
executable by a computer.
• A loader is a program used by an operating system to
load programs from a secondary to main memory so as
to be executed.
• The loader should know the length of the program and
the address in the memory where it will be stored.
Assembler Directives
• Directives do many things;
– some tell the assembler to set aside space for variables,
– others tell the assembler to include additional source files,
– and others establish the start address for your program.
• Assembler directives supply data to the program and
control the assembly process
Assembler Directives contd….
• EQU
Assigns a value to a symbol used in the program(same as =)
• Eg. SUM EQU 100, inform the assembler to that the name SUM
should be replaced by the value 100 wherever it appears in the
program
• ORG
Sets the current origin to a new value. This is used to set the
program or register address during assembly.
• For example, ORIGIN 0100h tells the assembler to assemble all
subsequent code starting at address 0100h.
• DATAWORD
This directive used to perform initialization before the
execution of the code is
Eg. N DATAWORD 100
Assembler Directive contd..
• RESERVE directive declare that a memory block of 400
bytes is to be reserved for data
• The END directive tells the assembler that this is the end
of the source program text
• The END directive includes a label START, which is the
address of the location at which execution of the
program is to begin
• The RETURN directive identifies the point at which
execution of the program should be terminated
GENERAL FORMAT OF A STATEMENT
• Most assembly languages require statements in a source
program to be written in the form:
Label Operation Operands Comment
1) Label is an optional name associated with the memory-
address where the machine language instruction produced
from the statement will be loaded.
2) Operation Field contains the OP-code mnemonic of the
desired instruction or assembler.
3) Operand Field contains addressing information for accessing
one or more operands, depending on the type of instruction.
4) Comment Field is used for documentation purposes to make
program easier to understand.
Assembly Language Program – Addition of N Nos.
Memory Addressing
Address Operation or Data
Label Information

A. Directive SUM EQU 200


ORIGIN 204
N DATAWORD 100
NUM1 RESERVE 400
ORIGIN 100
Statement START MOVE N, R1
that
generate MOVE #NUM1, R2
machine CLR R0
instructions LOOP ADD (R2), R0
ADD #4, R2
DEC R1
BGTZ LOOP
MOVE R0, SUM
A.Directive RETURN
END START
Memory Arrangement
Label Address Instruction
100 Move N, R1
104 Move #NUM1, R2
108 Clear R0
LOOP 112 Add (R2), R0
116 Add #4, R2
120 Decrement R1
124 Branch > 0 LOOP
128 Move R0, SUM
132
….
SUM 200
N 204 100
NUM1 208
NUM2 212

NUMn 604
Input / Output Operation
• The data on which the instructions operate
are not necessarily already stored in memory.
• Data need to be transferred between
processor and outside world (disk, keyboard,
etc.)
• I/O operations are essential, the way they are
performed can have a significant effect on the
performance of the computer.
Program-Controller I/O
• In this method the processor constantly
checks the status flags , and when it finds that
the flag is set it performs the appropriate
operation.
Program-Controlled I/O Example
• Read in character input from a keyboard and
produce character output on a display screen.
 Rate of data transfer (keyboard, display, processor)
 Difference in speed between processor and I/O device creates
the need for mechanisms to synchronize the transfer of data.
 A solution: on output, the processor sends the first character
and then waits for a signal from the display that the character
has been received. It then sends the second character. Input is
sent from the keyboard in a similar way.
Bus

Processor
DATAIN DATAOUT

SIN SOUT

Key board Display

Figure 2.19 Bus connection for processor


, keyboard, and display
.

Program-Controlled I/O Example

- Registers
- Flags
- Device interface
• In order to perform I/O transfer, we need machine instructions
that can check the state of the status flags and transfer data
between the processor and I/O device.
• Eg. The processor can monitor the keyboard status flag SIN and
transfer a character from DATAIN to register R1 by the following
sequence of operations
READWAIT Branch to READWAIT if SIN = 0
Input from DATAIN to RI (MoveByte DATAIN, R1)

WRITEWAIT Branch to WRITEWAIT if SOUT = 0


Output from R1 to DATAOUT (MoveByte R1, DATAOUT)
• Generally, SIN and SOUT are particular bit in status register
INSTATUS and OUTSTATUS
• The read operation be implemented by machine instruction
sequence
• READWAIT Testbit #3, INSTATUS
Branch = 0 READWAIT
MoveByte DATAIN, R1
• The write operation be implemented by machine instruction
sequence
• WRITEWAIT Testbit #3, OUTSTATUS
Branch = 0 WRITEWAIT
MoveByte R1, DATAOUT
Note : Testbit instruction tests the state of one bit in the
destination location, where the bit position to be tested is
indicated by the first operand
Stacks and Queue
Stack
• A stack is a conceptual structure consisting of a set of
homogeneous elements and is based on the principle of last
in first out (LIFO).
• It is a commonly used abstract data type with two major
operations, namely push and pop.
• Push and pop are carried out on the topmost element, which
is the item most recently added to the stack.
• The push operation adds an element to the stack while the
pop operation removes an element from the top position.
• The stack concept is used in programming and memory
organization in computers.
Stack Organization

Current
Top of Stack
• LIFO TOS 0
Last In First Out 1
2
3
4
5
SP 6 0 1 2 3
7 0 0 5 5
FULL EMPTY 8 0 0 0 8
9 0 0 2 5
Stack Bottom 10 0 0 1 5
Stack
Stack Organization

Current 1 6 9 0
Top of Stack
• PUSH TOS 0
SP ← SP – 1 1
2
M[SP] ← DR 3
If (SP = 0) then (FULL ← 1) 4
5 1 6 9 0
EMPTY ← 0
SP 6 0 1 2 3
7 0 0 5 5
FULL EMPTY 8 0 0 0 8
9 0 0 2 5
Stack Bottom 10 0 0 1 5
Stack
Stack Organization
Current
Top of Stack
• POP TOS 0
DR ← M[SP] 1
2
SP ← SP + 1 3
If (SP = 11) then (EMPTY ← 1) 4
5 1 6 9 0
FULL ← 0
SP 6 0 1 2 3
7 0 0 5 5
FULL EMPTY 8 0 0 0 8
9 0 0 2 5
Stack Bottom 10 0 0 1 5
Stack
Stack Operations
Push
– Subtract #4, SP
– Move NewItem, (SP)
Pop
– Move (SP), Item
– Add #4, SP
Subroutine
A routine or subroutine also referred to as
a function, procedure, and subprogram is a
portion of code that may be called and executed
anywhere in a program.

A subroutine is a sequence of program


instructions that perform a specific task, packaged
as a unit. This unit can then be used in programs
wherever that particular task should be performed.
Subroutine contd..
• When a subroutine is called, the address of the next
instruction to be executed in the program counter (PC) is
pushed into the stack / saved in the link register
• The address of the subroutine ( eg. CALL address) is
loaded into the program counter (PC).
• The called subroutine is executed
• When the execution of a subroutine completes, the
return address in the stack is popped out into the
program counter (PC) and continue the execution of the
main program.
PARAMETER PASSING
• The exchange of information between a calling-
program and a subroutine is referred to as Parameter
Passing
• The parameters may be placed in registers or in
memory-location, where they can be accessed by the
subroutine.
• Alternatively, parameters may be placed on the
processor-stack used for saving the return-address
Parameter Passing thro - Register
Parameter Passing thro - Register
• General purpose registers are limited. Not
possible to pass many parameters through
register
• Stack is highly flexible to handle a large
number of parameters
Parameter Passing thro - Stack
STACK FRAME
• Stack Frame refers to locations that constitute a private work-
space for the subroutine.
• The work-space is
→ created at the time the subroutine is entered &
→ freed up when the subroutine returns control to the
calling-program
• Frame Pointer (FP) is used to access the parameters passed
→ to the subroutine &
→ to the local memory-variables.
• The contents of FP remains fixed throughout the execution of
the subroutine, unlike stack-pointer SP, which must always
point to the current top element in the stack.
Assumption
• Four parameters are passed to the subroutine
• Three local variables are used within
subroutine
• Register R0, R1 need to be saved
Layout for information in stack frame
Operation on Stack Frame
• Initially SP is pointing to the address of old TOS.
• The calling-program saves 4 parameters on the stack (See Fig.)
• The Call instruction is now executed, pushing the return-
address onto the stack.
• Now, SP points to this return-address, and the first instruction
of the subroutine is about to be executed.
• Now, FP is to be initialized (memory address of next location in
the stack) and its old contents have to be stored. Hence, the
first 2 instructions in the subroutine are:
– Move FP,-(SP)
– Move SP,FP

• The FP is initialized to the value of SP i.e. both FP and SP point


to the saved FP address
Operation on Stack Frame contd..
• We can access the parameters and the local
variables using the index addressing mode.
• The parameters can be accessed by using
addresses 8(FP), 12(FP),..
• The local variable can be accessed by using
addresses -4(FP), -8(FP),...
Operation on Stack Frame contd..
• The 3 local variables may now be pushed onto the stack. Space
for local variables is allocated by executing the instruction
- Subtract #12,SP
• Finally, the contents of processor-registers R0 and R1 are saved
in the stack.
• At this point, the stack frame has been set up as shown in the
figure
• The subroutine now executes its task. When the task is
completed, the subroutine pops the saved values of R1 and R0
back into those registers, removes the local variables from the
stack frame by executing the instruction.
– Add #12, SP
• And subroutine pops saved old value of FP back into FP. At this
point, SP points to return-address, so the Return instruction can
be executed, transferring control back to the calling-program.
Number, Arithmetic Operations,
and Characters
Number, and Characters
• Computers are built using logic circuits that operate on
information represented by two valued electrical signals HIGH
represent a ‘logic 1’ and LOW represent a ‘logic 0’
• Generally, a logic “1” represents a higher voltage, such as 5
volts, while a logic “0” represents a low voltage, such as 0 volts
or ground.
• These two discrete voltage levels representing the digital values
of “1’s” (one’s) and “0’s” (zero’s) are commonly called bits
• The most natural way to represent a number in a computer
system is by a string of bits called a binary number.
• A text character can also be represented by a sting of bits called
a character code
Number Representation
• Three systems are used for representing integer :
Sign-and-magnitude
1’s complement
2’s complement
• In all three systems, the leftmost bit is 0 for positive numbers
and 1 for negative numbers
• Positive values have identical representations in all systems
• Assumptions:
4-bit machine word
16 different values can be represented
Roughly half are positive, half are negative
Binary, Signed-Integer Representations

B Values represented

Sign and
b 3 b 2 b1 b 0 magnitude 1's complement 2's complement

0 1 1 1 +7 +7 + 7
0 1 1 0 +6 +6 + 6
0 1 0 1 +5 +5 + 5
0 1 0 0 +4 +4 + 4
0 0 1 1 +3 +3 + 3
0 0 1 0 +2 +2 + 2
0 0 0 1 +1 +1 + 1
0 0 0 0 +0 +0 + 0
1 0 0 0 - 0 -7 - 8
1 0 0 1 - 1 -6 - 7
1 0 1 0 - 2 -5 - 6
1 0 1 1 - 3 -4 - 5
1 1 0 0 - 4 -3 - 4
1 1 0 1 - 5 -2 - 3
1 1 1 0 - 6 - 1 - 2
1 1 1 1 - 7 -0 - 1

Binary, signed-integer representations.


Sign-and-Magnitude Representation
-7 +0
-6 11 11 00 00 +1
11 10 00 01
-5 +2 +
11 01 00 10
-4 11 00 00 11 +3 0 100 = + 4

-3 10 11 01 00 +4 1 100 = - 4
10 10 01 01
-2 +5 -
10 01 01 10
-1 10 00 01 11 +6
-0 +7

High order bit is sign: 0 = positive (or zero), 1 = negative


Three low order bits is the magnitude: 0 (000) thru 7 (111)
Number range for n bits = +/-2n-1 -1
Two representations for 0
1’s Complement Representation
In 1’s-complement representation, negative values are
obtained by complementing each bit of the corresponding
positive number.
The representation for −3 is obtained by complementing
each bit in the 0011 to yield 1100.
The same operation, bit complementing, is done to
convert a negative number to the corresponding positive
value.
Converting either way is referred to as forming the 1’s-
complement of a given number.
For n-bit numbers, this operation is equivalent to
subtracting the number from 2n − 1. In the case of the 4-bit
numbers , we subtract from 24 − 1 = 15, or 1111 in binary.
1’s Complement Representation
-0 +0
-1 11 11 00 00 +1
11 10 00 01
-2 +2 +
11 01 00 10
-3 11 00 00 11 +3 0 100 = + 4

-4 10 11 01 00 +4 1 011 = - 4
10 10 01 01
-5 +5 -
10 01 01 10
-6 10 00 01 11 +6
-7 +7

• Subtraction implemented by addition & 1's complement


• Still two representations of 0! This causes some problems
• Some complexities in addition
2’s Complement Representation

Finally, in the 2’s-complement system, forming the


2’s-complement of an n-bit number is done by
subtracting the number from 2n.
Hence, the 2’s-complement of a number is obtained
by adding 1 to the 1’s-complement of that number.
2’s Complement Representation

-1 +0
-2 11 11 00 00 +1
11 10 00 01
-3 +2 +
11 01 00 10
-4 11 00 00 11 +3 0 100 = + 4

-5 10 11 01 00 +4 1 100 = - 4
10 10 01 01
-6 +5 -
10 01 01 10
-7 10 00 01 11 +6
-8 +7

• Only one representation for 0


• One more negative number than positive number
Binary, Signed-Integer Representations

B Values represented

Sign and
b 3 b 2 b1 b 0 magnitude 1's complement 2's complement

0 1 1 1 +7 +7 + 7
0 1 1 0 +6 +6 + 6
0 1 0 1 +5 +5 + 5
0 1 0 0 +4 +4 + 4
0 0 1 1 +3 +3 + 3
0 0 1 0 +2 +2 + 2
0 0 0 1 +1 +1 + 1
0 0 0 0 +0 +0 + 0
1 0 0 0 - 0 -7 - 8
1 0 0 1 - 1 -6 - 7
1 0 1 0 - 2 -5 - 6
1 0 1 1 - 3 -4 - 5
1 1 0 0 - 4 -3 - 4
1 1 0 1 - 5 -2 - 3
1 1 1 0 - 6 - 1 - 2
1 1 1 1 - 7 -0 - 1

Figure 2.1. Binary, signed-integer representations.


Addition and Subtraction – 2’s Complement

4 0100 -4 1100
+3 0011 + (-3) 1101
If carry-in to the high order bit =
carry-out then ignore carry 7 0111 -7 11001

if carry-in differs from


carry-out then overflow
4 0100 -4 1100
-3 1101 +3 0011
1 10001 -1 1111

Simpler addition scheme makes 2’s complement the most common


choice for integer number systems within digital systems
2’s-Complement Add and Subtract Operations
(a) 0010 ( + 2) (b) 0100 ( + 4)
+ 0011 ( + 3) + 1010 (- 6)
0101 ( + 5) 1110 (- 2)
(c) 1011 (- 5) (d) 0111 ( + 7)
+ 1110 (- 2) + 1101 ( - 3)
1001 (- 7) 0100 ( + 4)
1101
(e) 1101 (- 3) + 0111
- 1001 (- 7)
0100 ( + 4)
0010
(f) 0010 ( + 2) + 1100
- 0100 ( + 4)
1110 ( - 2)
0110
(g) 0110 ( + 6) + 1101
- 0011 ( + 3)
0011 ( + 3)
1001
(h) 1001 ( - 7) + 0101
- 1011 (- 5) 1110 ( - 2)
1001
(i) 1001 (- 7) + 1111
- 0001 ( + 1) 1000 ( - 8)
0010
+ 0011
(j) 0010 ( + 2)
- 1101 ( - 3) 0101 ( + 5)

Figure 2.4. 2's-complement Add and Subtract operations.


Overflow - Add two positive numbers to get a negative
number or two negative numbers to get a positive number

-1 +0 -1 +0
-2 1111 0000 +1 -2 1111 0000 +1
1110 0001 1110 0001
-3 +2 -3
1101 1101 +2
0010 0010
-4 -4
1100 0011 +3 1100 0011 +3
-5 1011 -5 1011
0100 +4 0100 +4
1010 1010
-6 0101 -6 0101
1001
+5 +5
0110 1001
0110
-7 1000 0111 +6 -7 1000 +6
0111
-8 +7 -8 +7

5 + 3 = -8 -7 - 2 = +7
Overflow Conditions
In the 2’complement number representation system, n-bits can represent value
in the range -2n-1 to 2n-1 – 1. When the result of an arithmetic operation is
outside the representable range, an arithmetic overflow has occur.
0111 1000
5 0101 -7 1001
3 0011 -2 1100
-8 1000 7 10111
Overflow Overflow
0000 1111
5 0101 -3 1101
2 0010 -5 1011
7 0111 -8 11000
No overflow No overflow
Overflow when carry-in to the high-order bit does not equal carry out
Sign Extension
To represent a signed number in 2’s complement form using a larger number
of bits, repeat the sign bit as many times as needed to the left. This operation
is called sign extension
• Task:
– Given w-bit signed integer x
– Convert it to w+k-bit integer with same value
• Rule:
– Make k copies of sign bit:
– X  = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0 w
X • • •
k copies of MSB

• • •

X • • • • • •
k w
2’ s Complement Arithmetic
Let X = 7 0111
Y=5 0101

Find X – Y = X+(-Y)
X= 0111
-Y = 1011
X+(-Y) = 10010 (Ans 2) (Discard carry)

Find Y – X = Y+(-X)
Y = 0101
-X = 1001
Y+(-X) = 1110 ( Ans is –Ve and its is in 2’s comp
- 0010 (-2)
2’ s Complement Arithmetic

• X = 4+16+64 = 84
• Y= 1+2+64 = 67
• X-Y = 17 (0010001)
• Y – X = -17(1101111)
• - 0010001
1’ s Complement Arithmetic
Let X = 7 0111
Y=5 0101

Find X – Y = X+(-Y)
11
X= 0111
-Y = 1010
X+(-Y) = 10001 (Add end-around carry)
1
0010 (Ans 2)
Find Y – X = Y+(-X)
Y = 0101
-X = 1000
Y+(-X) = 1101 ( Ans is –Ve and its is in 1’s comp
- 0010 (-2 Ans)
1’ s Complement Arithmetic

X = 4+16+64 = 84
Y= 1+2+64 = 67
X-Y = 17 (0010001)
Y – X = -17(1101110)
- 0010001
SHIFT AND ROTATE INSTRUCTIONS
• There are many applications that require the bits of an
operand to be shifted right or left some specified number of
bit positions.
• In C language we have, << and >> operators for shift left and
shift right respectively
• The details of how the shifts are performed depend on
whether the operand is a signed number or some more
general binary-coded information.
• For general operands, we use a logical shift.
• For a signed number, we use an arithmetic shift, which
preserves the sign of the number.
LOGICAL and ARITHMETIC SHIFTS
• Two logical shift instructions are
1) Shifting left (LShiftL) &
Syntax: LShiftL count, dest
2) Shifting right (LShiftR).
Syntax: LShiftL count, dest
• These instructions shift an operand over a number of bit positions
specified in a count operand contained in the instruction.
• Arithmetic Shift Right (AshiftR)
Syntax: LShiftR count, dest
• The arithmetic shift right instruction preserve position
• Confirm with the rules for signed 2’s complement numbers
Logical Shift Left

Before : 0 0 1 1 1 0 . . . 0 1 1

First Shift : 0 1 1 1 0 . . . 0 1 1 0

Second Shift : 1 1 1 0 . . . 0 1 1 0 0
Logical Shift Right
Arithmetic Shift
ROTATE INSTRUCTIONS

Before : 0 0 1 1 1 0 . . . 0 1 1

First Shift : 0 1 1 1 0 . . . 0 1 1 0

Second Shift : 1 1 1 0 . . . 0 1 1 0 1
ROTATE INSTRUCTIONS

Before : 0 0 1 1 1 0 . . . 0 1 1

First Shift : 0 1 1 1 0 . . . 0 1 1 0

Second Shift : 1 1 1 0 . . . 0 1 1 0 0

You might also like