Вы находитесь на странице: 1из 52

Computer Organization

 It describes the function and design of the various units of digital computers that store
and process information.
 It also deals with the units of computer that receive information from external sources
and send computed results to external destinations.
“Computer Organization” & “Computer Architecture”
 “Computer Organization” and “Computer Architecture” are the terms used in
describing digital computer systems.
 Computer architecture deals with the instruction sets, address fields, operation
codes, addressing modes, effective utilization of I/O mechanisms and such other
aspects which would interest the users of computers.
 Computer organization include hardware details such as generation of control
signals, interface between the computer and peripherals, memory technologies used,
modular design, flow of data, control mechanisms and so on.
 In brief, a programmer is generally more concerned about the architectural features
and a designer of computer systems is more concerned about the organizational
aspects.
Computer types
Digital computer : It is a fast electronic calculating machine that accepts digitized input
information, processes it according to a list of internally stored instructions, and produces the
resulting output information.
(1) Personal computer: It is the most common form of desktop computers. Desktop
computers have processing and storage units, visual display and audio output units, and a
keyboard that can all be located easily on a home or office desk. The storage media include
hard disks, CD-ROMs and diskettes.
Portable notebook computers are a compact version of the personal computers with all of
these components packaged into single unit the size of a thin briefcase.
(2) Workstations: Work stations with high resolution graphics input/output capability,
although still retaining the dimensions of desktop computers, have significantly more
computational power than personal computers. Workstations are often used in engineering
applications, especially for interactive design works.
(3) Enterprise systems or mainframes: These are used for business data processing in
medium to large corporations that require much more computing power and storage capacity
than workstations can provide.
(4) Servers: Servers contain sizable database storage units and are capable of handling large
volumes of requests to access the data. The Internet and its associated servers have become a
dominant world wide source of all types of information.
(5) Super Computers: Super Computers are used for the large scale numerical calculations
required in applications such as weather forecasting, aircraft design and simulation.
Functional units
A computer consists of five functionally independent main parts:
 Input unit
 Memory unit
 Arithmetic and logic unit (ALU)
 Output unit
 Control unit
The structure of a digital computer is shown below

Operation of a computer
 The computer accepts information in the form of programs and data through an input
unit and stores it in the memory.
 Information stored in the memory is fetched, under program control, into an
arithmetic and logic unit, where it is processed.
 Processed information leaves through an output.
 All activities inside the machine are directed by the control unit.
Input unit
 Computers accept coded information through input units.
 The most well known input device is key board.
 Whenever a key is pressed, the corresponding letter or digit is automatically translated
into its corresponding binary code and transmitted over a cable to either the memory
or the processor.
Memory unit
The function of the memory is to store programs and data. There are two classes of storage,
called primary and secondary.
Primary storage is a fast memory that operates at electronic speeds. Programs must stay in
memory while they are being executed. The memory contains a large number of
semiconductor storage cells, each capable of storing one bit of information.
Random access memory: Memory in which any location can be reached in a short and fixed
amount of time after specifying its address is called random access memory (RAM).
The time required to access one word is called the memory access time.
Cache memory: The small, fast, Ram units are called caches. They are tightly coupled with
the processor and are often contained on the same integrated circuit chip to achieve high
performance.
Main memory: The largest and slowest unit is referred to as the main memory.
Secondary storage: Although primary storage is essential, it tends to be expensive.
Thus additional, cheaper, secondary storage is used when large amounts of data and many
programs have to be stored, particularly for information that is accessed infrequently.
A wide selection of secondary storage devices is available, including magnetic disks,tapes
and optical disks (CD-ROMs)
Arithmetic and logic unit (ALU)
Most computer operations are executed in the arithmetic and logic unit (ALU) of the
processor.
For example, Suppose two numbers are to be added. They are brought into the processor, and
the actual addition is carried out by the ALU.
The sum may then be stored in the memory or retained in the processor for immediate use.
When operands are brought into the processor, they are stored in high-speed storage elements
called registers.
Output unit
The output unit is the counterpart of input unit. Its function is to send processed results to the
outside world. The most familiar example of such a device is a printer.
Some units, such as graphic displays, provide an input function and an output function. The
dual role of such units is the reason for using the single name I/O unit in many cases.
Control unit
The memory, arithmetic and logic, and I/O units store and process information and perform
input and output operations. The operations of these units are coordinated by control unit.
The control unit is effectively the nerve center that sends control signals to other units and
senses their states.
Basic operational concepts
To perform a given task, an appropriate program consisting of a list of instructions is stored
in the memory.
Individual instructions are brought from the memory into the processor, which execute the
specified operations. Data to be used as operands are also stored in the memory.
Consider, the two instruction sequence
Load LOCA, R1
Add R1, R0
The first of these instructions transfers the contents of memory location LOCA into processor
register R1, and second instruction adds the contents of register R1 as well as those of R0 and
places the sum into R0.
Note that this destroys the former contents of R1 as well as R0, where as the original contents
of memory location LOCA are preserved.
Connections between processor and memory

Instruction register (IR)


The instruction register holds the instruction that is currently being executed.
Its output is available to the control circuits, which generate the timing signals that control the
various processing elements involved in executing the instruction.
Program counter (PC)
The program counter is another specialized register.
It keeps track of the execution of a program.
It contains the memory address of the next instruction to be fetched and executed.
During the execution of an instruction, the contents of of the PC are updated to correspond to
the address of the next instruction to be executed.
It is customary to say that PC points to the next instruction that is to be fetched from the
memory.
Memory address register (MAR) & Memory data register (MDR)
These two registers facilitate communication with the memory.
The MAR holds the address of the location to be accessed.
The MDR contains the data to be written into or read out of the addressed location.
Operating steps for Program execution
Programs are stored in the memory through the input unit.
Execution of the program starts when the PC is set to point to the first instruction of the
program.
The contents of the PC are transferred to the MAR and a Read control signal is sent to the
memory.
After the time required to access the memory elapses, the addressed word (in this case, the
first instruction of the program) is read out of the memory and loaded into the MDR.
Next, the contents of the MDR are transferred to the IR .
At this point, the instruction is ready to be decoded and executed.
If the instruction involves an operation to be performed by the ALU, it is necessary to obtain
the required operands.
If an operand resides in memory ( it could also be in a general purpose register in the
processor), it has to be fetched by sending its address to the MAR and initiating a Read cycle.
When the operand has been read from the memory into the MDR, it is is transferred from the
MDR to the ALU.
After one or more operands are fetched in this way, the ALU can perform the desired
operation.
If the result of the operation is to be stored in the memory, then the result is sent to the MDR.
The address of the location where the result is to be stored is sent to the MAR, and a write
cycle is initiated.
At some point during the execution of the current instruction, the contents of the PC are
incremented so that the PC points to the next instruction to be executed.
Thus, as soon as the execution of the current instruction is completed, a new instruction fetch
may be started.
In addition to transferring data between the memory and the processor, the computer accepts
data from input devices and sends data to output devices. Thus, some machine instructions
with the ability to handle I/O transfers are provided.
Interrupt service routine
Normal execution of a program may be preempted if some device requires urgent servicing.
For example, a monitoring device in a computer-controlled industrial process may detect a
dangerous condition. In order to deal with the situation immediately, the normal execution of
the current program must be interrupted. To do this, the device raises an interrupt signal.
An interrupt is a request from an I/O device for service by the processor.
The processor provides the requested service by executing an appropriate interrupt-service
routine.
Because such diversions may alter the internal state of the processor, its state must be saved
in the memory locations before servicing the interrupt.
Normally, the contents of the PC, the general registers, and some control information are
stored in memory.
When the interrupt service routine is completed, the state of the processor is restored so that
the interrupted program may continue.
Bus structures
A group of lines(wires) that serves as a connecting path for several devices of a computer is
called a bus. In addition to the lines that carry the data, the bus must have lines for address
and control purposes. The simplest way to interconnect functional units is to use a single bus,
as shown below
All units are connected to this bus. Because the bus can be used for only one transfer at a
time, only two units can actively use the bus at any given time.
Bus control lines are used to arbitrate multiple requests for use of the bus.
The main virtue of the single-bus structure is its low cost and its flexibility for attaching
peripheral devices.
Systems that contain multiple buses achieve more concurrency in operations by allowing two
or more transfers to be carried out at the same time. This leads to better performance but at an
increased cost.
Explain how timing differences are smoothed out among processors, memories and I/O
devices.
The devices connected to a bus vary widely in their speed of operation. Electro
mechanical devices such as key board and printers are relatively slow. Others like
magnetic or optical disks, are considerably faster. Memory and processor units operate at
electronic speeds.
A common approach to smooth out the timing differences is to include buffer registers with
the devices to hold the information during transfers.
They prevent a high speed processor from being locked to a slow I/O device during a
sequence of data transfers.
This allows the processor to switch rapidly from one device to another, interweaving its
process activity with data transfers involving several I/O devices.
Thus, buffer registers smooth out timing differences among processors, memories and I/O
devices.
Software

Explain the role of system software in a computer.


System software is responsible for the coordination of all activities in a computing system.
System software is a collection of programs that are executed as needed to perform functions
such as
1) Receiving and interpreting user commands.
2) Entering and editing application programs and storing them as files in secondary storage
devices.
3) Managing the storage and retrieval of files in secondary storage devices.
4) Running standard application programs such as word processors, spread sheets, or games,
with data supplied by the user.
5) Controlling I/O units to receive input information and produce output results.
6) Translating programs from high level language to low level language.
7) Linking and running user-written application programs with existing standard library
routines, such as numerical computation packages.

Explain various components of system software.


Operating system software:

The operating system is a collection of routines that is used to control the sharing of
and interaction among various computer units as they execute application programs. The OS
routines perform the tasks required to assign computer resources to individual application
programs. These tasks include assigning memory and magnetic disk space to program and
data files, moving data between memory and disk units, and handling I/O operations.
Let us consider a system with one processor, one disk, and one printer. Assume that
part of the program’s task involves reading a data file from the disk into the memory,
performing some computations on the data, and printing the results.
A convenient way to illustrate the sharing of the processor time is by a time-line
diagram, as shown in the following diagram.

During the time period t0 to t1, OS routine initiates loading the application program from
disk to memory, waits until the transfer is completed, and then passes execution control to the
application program. A similar pattern of activity occurs during period t2 to t3 and period t4
to t5, when the operating system transfers the data file from the disk and prints the results. At
t5, the operating system may load and execute another application program.
The computer resources can be used more efficiently if several application programs are to be
processed. Notice that the disk and the processor are idle during most of the time period t4 to
t5. The operating system can load the next program to be executed into the memory from the
disk while the printer is operating. Similarly, during t0 to t1, the operating system can arrange
to print the previous program’s results while the current program is being loaded from the
disk.
Thus, the operating system manages the concurrent execution of several application programs
to make the best possible use of computer resources. This pattern of concurrent execution is
called multiprogramming or multitasking.
Translators

Compiler:
A compiler converts high level languages program into a machine level languages
program (byte code). The compiler takes a high level languages program as input and checks
the syntax errors of each statement and generates object code (Machine languages equivalent
of source code).
 After compilation a linker combined our object code with other required object codes
and finally gives executable file.
 A loader copies this executable file into memory and executed by the microprocessor.
The high level languages are platform independent. So a single compiler that
produces different objects code for different platforms.

A high level language statement is usually converted to a sequence of several machine


code instructions.

Assembler:
An assembler converts assembly languages program into a machine language program (byte
code).Assembler is less complex that that compilers. Because each assembles language
instruction corresponds to anon unique machine code instruction.
The assembler takes an assembly languages program as input and converts into object
code. After it follows the linking and loading procedures for execution. Some small
applications written in assembly languages do not need linking other object files directly the
object file get after assembly can be executed.

Interpreters:
An interpreter decodes each line of source code to machine language. If any errors occur
while interpreting the code, immediately the error is thrown. Interpreters are faster in
debugging the code. It consumes less memory space. Some of the famous programming
languages using interpreters are Python and Ruby. The interpreter procedure is shown in the
following figure:

Linker
A linker is a computer program that takes one or more object files generated by
a compiler and combines them into one, executable program. Computer programs are usually
made up of multiple modules that span separate object files, each being a compiled computer
program. The program as a whole refers to these separately-compiled object files using
symbols. The linker combines these separate files into a single, unified program; resolving
the symbolic references as it goes along.
Dynamic linking is a similar process, available on many operating systems, which postpones
the resolution of some symbols until the program is executed. When the program is run,
these dynamic link libraries are loaded as well. Dynamic linking does not require a linker.
The linker bundled with most Linux systems is called ld;
Loaders
A loader is the part of an operating system that is responsible for
loading programs and libraries. It is one of the essential stages in the process of starting a
program, as it places programs into memory and prepares them for execution.
Loading a program involves reading the contents of the executable file containing the
program instructions into memory, and then carrying out other required preparatory tasks to
prepare the executable for running.
Once loading is complete, the operating system starts the program by passing control
to the loaded program code.
All operating systems that support program loading have loaders, apart from highly
specialized computer systems that only have a fixed set of specialized programs.
Embedded systems typically do not have loaders, and instead the code executes directly from
ROM.
In order to load the operating system itself, as part of booting, a specialized boot
loader is used. In many operating systems the loader is permanently resident in memory,
although some operating systems that support virtual memory may allow the loader to be
located in a region of memory that is page table.

Text editor: Another important system program that all programmers use is a text editor. It is
used for entering and editing application programs. The user of this program interactively
execute commands that allow statements of a source program entered at a keyboard to be
accumulated in a file.
Computer Levels of Programming Languages:
Language Categories: Computer programming languages are divided into three type
categories.

1. High level languages


2. Assembly languages
3. Machine language
High level languages: The languages with the highest level of abstraction are collectively
referred to as high-level languages.
High level languages are platform independent.
The same program code can be converted and run a computer with different microprocessors
and operating system without modification.
Eg: C++, Java, FORTRAN…….etc
Assembly languages: The languages with the lower level of abstraction are called assembly
languages.
Assembly languages are platform dependent.
Each microprocessor has its own assembly languages instruction set. I.c a program written in
the assembly languages of one microprocessor cannot be run on a computer that has different
microprocessor.
The new version microprocessor is able to execute the assembly language program designed
for previous versions.
Eg: Intel’s Pentium III, Microprocessor can run program written in the assembly languages of
the Pentium II, Pentium pro, and Pentium…..etc.
Machine languages: The languages contain the binary values that cause the microprocessor
to perform certain operations.
When a microprocessor reads and executes an instruction it is a machine languages
instruction.
Programs written in high level assembly level languages are converted into machine level for
executed by the microprocessor. Machine language code is backward compatible.

Multiprocessors
 Large computer systems may contain a number of processor units, in which case they
are called multiprocessor systems.
 These systems either execute a number of different application tasks in parallel, or
they execute subtasks of a single large task in parallel.
 All processors usually have access to all of the memory in such systems, and the term
shared-memory multiprocessor systems is often used to make this clear.
 The high performance of these systems comes with much increased complexity and
cost. In addition to multiple processors and memory units, cost is increased because of
the need for more complex interconnection networks.
Multi-computers
 In contrast to multiprocessor systems, it is possible to use an interconnected group of
complete computers to achieve high total computational power.
 The computers normally have access only to their own memory units. When the
tasks they are executing need to communicate data, they do so by exchanging
messages over a communication network.
 This property distinguishes them from shared-memory multiprocessors, leading to the
name message-passing multi- computers.
Data Representation
Data types : The data types found in the registers of digital computers are classified into three
categories.
1) Numbers used in arithmetic computations.
2) Letters used in data processing.
3) Other symbols used for specific purpose.
All types of data, except binary numbers, are represented in computer registers in binary-
coded form.
Number systems
A number system of base (or radix) r, uses r distinct symbols for its digits.
Base 10 uses 10 symbols (Decimal system). 0 1 2 3 4 5 6 7 8 9
The number 892.5 is 8 (10)2 + 9(10)1 + 2 (10)0 + 5 (10)-1
Common base values
 Binary : 0,1
 Octal : 0,1,2,3 4,5,6,7
 Hexadecimal : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , A , B , C , D , E , F A,B,C,D,E,F
correspond to the decimal numbers 10,11,12,13,14,15 respectively.
Convert to decimal
(101101)2 = 1(25) + 0(24) + 1(23) + 1(22) + 0(21) + 1(20)
= (45)10
(672.4)8 = 6(82)+7 (81)+ 2 (80) + 7(80) + 4.(8-1)
= 6(64) + 7(8) + 2(1) + 4 (1/8)
= (442.5)10
(FADE)16 = 15.(16)3+10.(16)2+13.(16)1+14.(16)0
= 15.(4096)+10.(256)+13.(16) + 14
= (64222)10
(DEAF)16= (57007)10
Decimal integer to base r
Repeatedly divide decimal integer by r and collect reminders
First remainder a is ar0
Second remainder b is br1 and so on
(11)10 = (1011)2
11/2 = 5 remainder 1
5/2 = 2 remainder 1
2/2 = 1 remainder 0
1/2 = 0 remainder 1
(11)10 = (1011)2
Decimal fraction to base r
Repeatedly multiply decimal fraction by r and collect integers.
First integer a is a.r -1.
Second integer b is b.r-2 and so on.
Ex. (.375)10 = (.011)2
(.375) /2 = 0 . 750
(.750)/ 2 = 1 . 500
(.500)/ 2 = 1 . 000
(.375)10 = (.011)2
Binary Coded Decimal (BCD) system
Need 4 bits to represent decimal digits
 0 0000
 1 0001
 2 0010
 3 0011
 4 0100
 5 0101
 6 0110
 7 0111
 8 1000
 9 1001
Alphanumeric Character Set
 Need codes to represent every character
 Lectures of the alphabet A B C a b c
 Special symbols $@%*({[
 Numbers 0 1 2
 ASCII (American Standard Code for Information Interchange) dominates 7-bit
code Truncate 3 left bits to get BCD value for numbers
 EBCDIC (Extended BCD Interchange Code) still used on IBM mainframes.
(r - 1)’s Complement
 Number N in base r having n digits
 Complement of N is (rn - 1) – N
 9’s complement for base 10
Complement of 1000 = (104 - 1) -1000 = 8999
Complement of 1234 = 9999 – 1234 = 8765
 1’s complement for base 2
Complement of 1000 = (24 - 1) – 1000 = 0111
Complement of 10110 = 11111 – 10110 = 01001
Complement of 0 = (21 - 1) - 0 = 1
r’s Complement
Number N is base r having n digits
 r’s Complement of N = rn – N for N ≠0, else 0
 Add 1 to ( r– 1)’s complemented value of N.

10’s complement for base 10


 Complement of 1000 = 104 – 1000 = 9000
 Complements of 1234 = 10000 – 1234 = 8766
2’s complement for base 2
 Complement of 1000 is 24 –1000 = 1000
 Complement of 10110 is 100000 – 10110 = 01010
 Complement of 0 is 0
The 2’s complement can be formed by leaving all least significant 0’s and the first 1
unchanged, and then replacing 1’s by 0’s and 0’s by 1’s in all other higher significant bits.
Identify the rightmost 1 and complement all the bits on its left.
Ex. The 2’s complement of 1101100 = 0010100
Ex. The 2’s complement of 1101111 = 0010001
Subtraction of Unsigned Number
 The subtraction of two n digit numbers, M – N, where N ≠ 0
 Add M to the r’s complement of N
 M + (rn - N) = M - N + rn
 If M ≥ N, sum produces discarded end carry rn
 If M ≤ N, sum produces r’s complement of N-M
Take r’s complement of sum and prefix with –
Ex. 62387 – 41923 = 62387 + 58077 = 20464.
Ex. 31486 – 80653 = 31486 + 19347 = 50833
No carry, so negated 10’s complement is – 49167.

Binary Integer Representation


Computer Arithmetic
 Signed-magnitude awkward.
 Signed-1’s complement has two representations for zero: -0 and +0.
 Therefore, signed -2’s complement is used.
Arithmetic Addition
 Simply add the two numbers and discard any leftmost carry out bit.
 Negative numbers are in 2’s complement
 Including the result!
Example
+7 0000 0111 -7 1111 1001
+11 0000 1011 +11 0000 1011
+18 0001 0010 +4 0000 0100

+7 0000 0111 -7 1111 1001


-11 1111 0101 -11 1111 0101
-4 1111 1100 -18 1110 1110
Arithmetic Subtraction
 Minuend – subtrahend = result
 Minuend + subtrahend’s r’s complement = result
 Simply add the two numbers and discard any leftmost carry out bit
 Negative numbers are in 2’s complement
 Including the result!
Example
 (-5) – (-14) = 9
 (1111 1011) - (1111 0010) = 0000 1001
 (1111 1011) + (0000 1110) =
1 0000 1001 = 0000 1001
Overflow
 When two numbers of n digits are added and the sum occupies ‘n+1’ digits, we say
that an overflow occurred.
 An overflow is a problem in digital computers because the width of the registers is
finite.
 A result that contains ‘n+1’ bits cannot be accommodated in a register with a standard
length of n bits.
Overflow Detection
 An overflow condition can be detected by observing the carry into the sign bit
position and the carry out of the sign bit position.
 If these two carries are not equal, an overflow condition is produced.
 If the two carries are applied to an exclusive –OR gate, an overflow will be detected
when the output of the gate is equal to1.
carry in = carry out in high-order digit
No overflow
Carry in ≠ carry out in high-order digit
Overflow
Overflow Examples
Carries: 0 1 Carries: 1 0
+70 0 1000110 -70 1 0111010
+80 0 1010000 -80 1 0110000
+150 1 0010110 -150 0 1101010
Floating Point Representation
 A floating point is always interpreted to represent a number in the following form:
 m.re
 r is base (radix)
 e is exponent
 Only the mantissa ‘m’ and the exponent ‘e’ are physically represented in the
register(including their sign)
 A floating-point binary number is said to be normalized if the most significant digit of
the mantissa ‘m’ is nonzero.
 Normalized numbers provide the maximum possible precision for the floating-point
number.
 A zero cannot be normalized because it does not have a nonzero digit. It is
usually represented in the floating point by all 0’s in the mantissa and exponent.
 Decimal number +6132.789 is represented in floating point with a fraction and an
exponent as follows:
Mantissa: +.6132789 Exponent:+04
 Binary number +1001.11 is represented with an 8-bit fraction and a six bit exponent
as follows:
Mantissa: 01001110 Exponent: 0000100
 The fraction has a zero in the leftmost position to denote positive.
 The binary point of the fraction follows the sign bit but is not shown in the register.
 The floating point number is equivalent to m × 2e = + (.10001110)2 × 2+4.
 Arithmetic operations with floating-point numbers are more complicated than
arithmetic with fixed point numbers and their execution takes longer and requires
more complex hardware.
 However, floating point representation is a must for scientific computations because
of the scaling problems involved with fixed-point computations.

Gray code
Only one bit flip flops for each successive number in the sequence.
 The advantage of Gray code over straight binary numbers is that the Gray code
changes by only one bit as it sequences from one number to the next.
 In other words, the change from any number to the next in sequence is recognized by
a change of only one bit from 0to1 or from 1 to 0.
 A typical application of the Gray code occurs when the analog data is represented by
the continuous change of a shaft position.
 The shaft is partitioned in to segments with each segment is assigned a number.
 If adjacent segments are made to correspond to adjacent gray code numbers,
ambiguity is reduced when the shaft position is in the line that separates any two
segments.
Error detection codes
 Binary information transmitted through some form of communication medium is
subject to external noise that could change bits from 1 to 0, and vice versa.
 An error detection code is a binary code that detects digital errors during
transmission.
 The most common error detection code used is the parity bit.
 A parity bit is an extra bit included with a binary message to make the total number
of 1’s either odd or even.
 A message of three bits and two possible parity bits is shown below.
 The P(odd) bit is chosen in such a way as to make sum of 1’s (all four bits) odd.
 The P(even) bit is chosen to make sum of all 1’s even.
 In either case the sum is taken over the message and the P bit.
 At the sending end, the message is applied to a parity generator, where the required
parity bit is generated.
 The message, including the parity bit, is transmitted to its destination.
 At the receiving end, all the incoming bits are applied to a parity checker that checks
the proper parity adopted (odd or even).
 An error is detected if the checked parity does not conform to the adopted parity.
 The parity method detects the presence of one, three, or any odd number of errors.An
even number of errors is not detected.
 The even- parity scheme has disadvantage of having a bit combination of all 0’s,
while in the odd parity there is always one bit that is 1.
 Note that P(odd) is the complement of P(even)
MEMORY LOCATIONS & ADDRESSES:
Memory is a storage device in computers. It stores information like numbers, characters,
operands and instructions.
Before, we are going to learn about memory locations and address. We need to know about
memory – The memory consists millions of storage cells, each cell stores a bit of information
i.e. the value 0 or 1.
 The cell stores the value 0 or 1 which is small amount of information, so it is difficult
to handle individual cells.
 So, The general approach to follow is store information in groups of fixed size.
 The memory is organized so that a group of n bits to store or retrieve in a single
operation.
 Each group of n bits is referred to as a word of information.
 Here n is called memory word length.
 The memory of computer systematically represented as collection of words as shown
below.

Memory Words
 Modern computers have word length of 16 to 64 bits.
Example:-
A computer memory word of 32 bits stores 4 ASCII characters each ASCII character takes 8
bits.
In the same way a computer memory word 32 bits also stores a number in 2’s complement
form.
A unit of 8 bits is called byte.
 Accessing the memory to store or retrieve a single item of information, either a word
or a byte, required distinct name or address for each item location.
 It is customary to use numbers from 0 to 2k-1 for some suitable value k, as the address
of successive locations in the memory.
 The memory with 2k address is called address space and the memory can have up to
2k addressable locations.
Example:-
A 24 bit address generates 224 address space and 16,777,216 memory locations.

BYTE ADDRESSABILITY
The memory divide into successive n bit memory words. Each memory word consists of
successive memory locations or addresses. But, in modern computers we assign a address for
each byte in a memory word. This assignment to memory is called byte addressable memory.
Example:-

BIG-ENDIAN AND LITTLE-ENDIAN ASIGNMENTS:-


There are two ways that byte addresses can be assigned across words, as shown in fig.

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.
In addition to specifying the address ordering of bytes within a word, it is also necessary to
specify the labelling of bits within a byte or a word. The same ordering is also used for
labelling bits within a byte, that is, b7, b6, …., b0, from left to right.

WORD ALIGNMENT:-
In the case of a 32-bit word length, natural word boundaries occur at addresses 0, 4, 8, …, as
shown in above fig. We say that 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 memory 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 bytes addresses 0,8,16 ….
There is no fundamental reason why words cannot begin at an arbitrary byte address. In that
case, words are said to have unaligned addresses. While the most common case is to use
aligned addresses, some computers allow the use of unaligned word addresses.
Accessing Numbers, Characters and Character Strings:
- A number usually occupies one memory word. It can be accessed in the memory by
specifying the address.
Ex: an 8 bit memory word stores a number 5

- In the same way individual character can be accessed by their byte address.
Ex: The ascii decimal value of A is 65 and binary number 0100 0001

- Characters string of variable length. The beginning of the string is indicated by giving
the address of the byte containing its first character and successive location of
successive characters.
- End of the string represented by using special character or special memory word
location or processor register can contain a number indicating length of the string in
bytes.
Ex: “hello” string storing into the memory as shown below.
Memory Operations:
Whatever the instruction executing in the processor either data operand or program
instruction, the program controller transfers those from memory to processor and vice
versa.
There are two basic operations we have to perform this.
a) Load
b) Store
Load/Read/Fetch
 This operation transfers a copy of the contents memory to processor.
 To start Load operation the processor send a address of the desired location tot
the memory and request to read its contents.
 The memory reads data stored at that address and send them to the processor.
 In this operation memory contents are not destroyed.
Store/Write
 The Store operation transfers an item of information from the processor to a
specific memory location.
 It destroying former contents of that location.
 The processor sends the address of the desired location to the memory together
with data to be written into the location.
Note: The data transfer may be one memory word or one byte for single operation.

Instructions and Instruction Sequencing:


If you want to perform any operation by using computer (processor) we need to give
instructions/ operations to processor. Like, reading from the keyboard, addition of two
numbers, work on particular condition and data transfer instructions.
We have four type of instructions.
i) Data transfer between the memory and the processor register
ii) Program sequencing and control instructions.
iii) Arithmetic and logic operations.
iv) I/O transfer instructions.
1. Data Transfer (Copy) Operations
1. Load a 8-bit number in a Register
2. Copy from Register to Register
3. Copy between Register and Memory
4. Copy between Input/Output Port and Accumulator
5. Load a 16-bit number in a Register pair
6. Copy between Register pair and Stack memory
Example Data Transfer (Copy) Operations / Instructions

2. Arithmetic Operations
1. Addition of two 8-bit numbers
2. Subtraction of two 8-bit numbers
3. Increment/ Decrement a 8-bit number
Example Arithmetic Operations / Instructions

3. Logical & Bit Manipulation Operations


1. AND two 8-bit numbers
2. OR two 8-bit numbers
3. Exclusive-OR two 8-bit numbers
4. Compare two 8-bit numbers
5. Complement
6. Rotate Left/Right Accumulator bits
Example Logical & Bit Manipulation Operations / Instructions

4.Branching Operations
These operations are used to control the flow of program execution
1.Jumps
• Conditional jumps
• Unconditional jumps
2.Call & Return
• Conditional Call & Return
• Unconditional Call & Return
Example Branching Operations / Instructions

5. Machine Control Instructions


These instructions affect the operation of the processor.
For e.g. HLT Stop program execution
NOP Do not perform any operation
Register Transfer Notation
Before going study about types of instructions we are need to know the notations to follow which is
used in instructions.
In computers we transfer information from one location to another location. The possible locations
are
- Memory locations.
- Processor locations
- I/O locations.
In many computers memory location are indentified by providing name to it.
Example: LOC, PLACE, A, Var2….
In the same way processor registers have names.
Example: R0, R1, R2…
Similarly, I/O registers names may be. DATAIN, DATAOUT.

By using these names we can perform operations. For example to read the contents of location into a
register R1 [LOC]
another example
R3 [R1] + [R2]
The operation is add the contents of R1,R2 and store into R3.

Assembly Language Notation:


We use another notation to represent machine instructions called assembly language format.
Mov Loc, R1
Move from memory location to R1 register.
Add R1,R2,R3
Add the contents of registers R1,R2 and store it in R3.

Basic Instruction Types(Instruction Formats):


Instructions are categorized according to no of memory address or registers used in the
operation.
Here we are considering the high level language statement C = A + B
The machine instructions are four different types.
1. Three address instructions
2. Two address instructions.
3. One address instructions.
4. Zero address instructions.
1. Three address Instruction:
The three address instruction format as shown below.
Operation S1, S2, Destination.
We can write three address instruction symbolically as
Add A,B,C
Here the operands A,B called source operands C is destination operand and performing addition
operation.
Adv:
The three address instruction used to evaluate arithmetic expressions in short programs.
Dis:
It needed more than one memory word to execute this instruction.
2. Two address instructions:
The two address instruction format.
Operation source, destination
We can write two address instructions
Add A,B
Mov B,C
Here, first operand source and second is destination.
If you want to write the instructions without modifying the contents of A and B.
We can write like this
Mov B,C
Add A,C
Dis:
Even two address instructions will not normally fit into one word lengths and address sizes.
3. One address Instructions:
The one address instruction format
Operation operand
We can the instruction.
Load A
Add B
Store C
In one address instructions we use a processor register accumulator (AC) for all data manipulations.
4. Zero address Instructions:
- A stack organized computer does not use an address field for the instructions like ADD and MUL.
- It uses PUSH and POP operations which require operand that communication with stack.
- To evaluate arithmetic operations in stack computer it is necessary to convert express into
reverse polish notation.
- The name zero addess is given to this type of computer because of the absence of an address filed
in the computational instructions.
We can write
PUSH A
PUSH B
ADD
POP C
Example of reverse polish notation:
x+y = xy+
(Infix notation) (Postfix notation)

Addressing Modes:
The term addressing modes refers to the way in which the operand of an instruction is
specified. Information contained in the instruction code is the value of the operand or the
address of the result/operand.
The different ways in which the location of an operand is specified in an instruction is
referred as addressing modes.
 Immediate
 Register
 Direct
 Indirect
 Displacement (Indexed)
 Auto Increment mode
 Auto Decrement mode
Immediate:

o Operand is part of instruction


o Operand = operand field
o e.g. ADD 5
—Add 5 to contents of accumulator
—5 is the operand
MOV #200, R0
The value 200 move to R0 register.
o No memory reference to access data
o Fast
o Range of operands limited to # of bits in operand field (< word size)
Instruction format:

Register addressing mode:

o Operand is held in register named in address filed.


o The register contains the value of the operand.
o Register addressing mode transfers a copy of byte or word from the source
register to destination register.
o Ex:- Add R4,R3
Advantages:
• Very small address field
—Shorter instructions
—Faster instruction fetch
• Faster memory access to operand(s)
Disadvantage:
• Very limited address space
• Multiple registers helps performance
—Requires good assembly programming or compiler writing
—C language has a dedicated keyword: register int a;
Instruction format:
Direct Addressing mode:

o Address field contains address of operand


o Effective address (EA) = address field (A)
o e.g. ADD A
—Add contents of cell A to accumulator
—Look in memory at address A for operand
MOV LOC, R1
--- Move contents in location to register R1
o Single memory reference to access data
o No additional calculations to work out effective address
o Range of addresses limited by # of bits in A (< word length)
o Direct addressing mode is also used for data transfer between the processor
and I/O devices.
e.g. IN 00H
OUT 01H
Instruction format:

Indirect addressing mode:


Effective Address(EA): Providing information from which the memory address
of operand can be determined.
o Memory cell pointed to by address field contains the address of (pointer to)
the operand
o EA = (A)
—Look in A, find address (A) and look there for operand
o e.g. ADD (A)
—Add contents of cell pointed to by contents of A to accumulator
o Advantage: Large address space
—2n where n = word length
o Disadvantage: Multiple memory accesses to find operand slower
o May be nested, multilevel, cascaded
—e.g. EA = (((A)))
Ex:-
Add (R1), R0(general purpose register example)

Add (A), R0 (Memory location)

Instruction format:

Displacement (Indexed):

o The effective address of the operand is generated by adding a constant value.


o EA = A + (R)
o Address field holds two values
—A = base value
—R = register that holds displacement
—or vice versa
o Has many versions, of which we mention these 3:
—Relative
—Base-register
—Indexing
Relative (to PC) Addressing
It’s a version of displacement addressing
• R = Program counter, PC
• EA = A + (PC)
—The operand is A cells away from the current cell (the one pointed to by PC) •
Remember:
—locality of reference
—cache usage
Base-Register Addressing
It’s a version of displacement addressing It’s a generalized relative addressing, where
other registers can play the role of PC
• A holds displacement
• R holds pointer to base address
• EA = A + (R)
—R may be explicit or implicit
• E.g. six segment registers in 80x86: CS, DS, ES, FS, GS, SS
Indexed Addressing
It’s a version of displacement addressing Very similar to base-register addressing
• A = base
• (R) = displacement
• EA = A + (R), but roles are reversed!
• Good for accessing array
Instruction Format:

Autoincrement 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 to point to the next item in a list.
(Ri)+
Autodecrement 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.
-(Ri)
Example:

Instruction Execution/ Instruction Cycle:


To execute a program (set of instructions) the processor “reads” each instruction from memory;
“interprets” it, then “executes” it.
To use the right way of execution of an instruction is called instruction cycle. The instruction cycle is
--- The microprocessor fetches each instruction
--- decodes it.
--- then executes it.
This sequence is continued until all the instructions are performed.
In the processor the instruction execution in two phases
 Instruction Fetch
 Instruction Execution
Instruction Fetch:
1. The instruction is fetched from memory location and whose address placed is in PC.
2. The instruction placed in IR (Instruction Register) in the processor.
Instruction Execution:
1. The instruction in IR is examined to determine which operation is too performed.
2. The specified operation performed by the processor.
In this instruction execution process we perform following steps.
a) fetching operands.
b) performing operations.
c) storing the results.

We can execute programs in the processor two ways.


 Sequential Execution
 Control flow execution(Branching)
Sequential Execution of a program:
Consider a high level programming language(C language) statement. C [A] + [B].
The instructions for above statement
Mov A, R0
Add B, R0
Mov R0,C
Here, We assume that one word per an instruction. Each memory word contains 323 bits. The
instructions are stored in successive locations starting from i.

Let us consider program execution


- To execute instruction it stores in program counter.
- The processor control circuit uses this instruction to fetch and executes instructions one at a
time in the order of increasing addresses.
- This is called straight line sequencing.
- During the execution of each instruction, the PC is incremented by 4 to point to the next
instruction.
- Thus, after the Move instruction at location i + 8 is executed, the PC contains the value i + 12,
which is the address of the first instruction of the next program segment.
Branching:
If you want to execute same instruction repeatedly we use the programmed loop. The loop is
a straight line sequence of instructions executed as many times as needed.
To perform this in processor we use the branch instructions. This type of instructions loads a
new value into the program counter. As a result, the processor fetches and executes the
instruction at the new address, called branch target, instead of the instruction at the location
that follows the branch instruction sequential address order. A conditional branch instruction
causes a branch only if a specified condition is satisfied. If the condition is not satisfied, the
PC is incremented in the normal way , and the next instruction in sequential address order is
fetched and executed.
Conditional Codes/ Conditional Flags:
Conditional codes/flags are used to track the information about the results of various
operations for use by subsequent conditional branch instructions.
This is accomplished by recording the required information in individual bits, often called
Conditional Codes/flags.
These flags are grouped together in a special processor register called the conditional code
register or status register.
Here individual flag set to 1 or cleared to 0 depending on the outcome of the operation.
Four commonly used flags.
N(Negative) : Set to 1 if the result of the operation is negative, otherwise cleared 0.
Z(Zero): Set to 1 if arithmetic operation result is 0, otherwise cleared 0.
V(Overflow):set to 1 if arithmetic overflow occurs; otherwise cleared 0.
C(Carry): set to 1, if a carry out results from the operation; otherwise cleared 0.
Basic I/O Operations
When we are talking about basic operations we get the instruction memory to processor.
Now, We consider how data transfer between the memory of a computer and outside world. It
is performed by using Input Output operations. These operations have significant effect on
the performance of computer.
We have two types of I/O operations.
 Program controlled I/O
 Memory mapped I/O
Program controlled I/O
 To read character input from keyboard and produce character output on display screen
we use the method programmed controlled I/O.
 But here we have a problem rate of data transfer i.e human typing speed less than
processor speed. In the same way display device speed less than processor speed.
 The difference in speed between processor and I/O devices creates a need for
mechanisms to synchronize the transfer.
 To overcome this problem, on output, the processor sends first character and waits for
a signal from the display that character has been received. It then sends second
character and so on.
 Input is sent from the keyboard in the similar way the processor waits for signal
indicating that a character has been struck and that its code is available in some
buffer register associated with the keyboard.
 We show diagrammatically from below diagram.

 From the above diagram, We will see that input operation is done in separate block
and output in separate block.
 The operation of reading a character code is from the keyboard to the processor.
Striking a key stores the corresponding character code in 8 bit buffer register
associated with keyboard. This is called DATAIN register.
 To inform about that the status control flag SIN is set to 1.
 The program controller checks SIN is 1 and processor read that character and it clear
SIN to 0.
 The operation of display from the processor a buffer register DATAOUT and status
flag SOUT used.
 When SOUT equals to 1, the display is ready to receive a character and the processor
send a character to DATAOUT.
Note: The buffer registers DATAIN and DATAOUT and status flags SIN and SOUT are prat
of hardware circuitry commonly known as device interface. This circuit connected through
bus.
Memory mapped I/O:

 In this I/O operation some memory address values are used to refer to peripherals
device buffers registers, such as DATAIN and DATAOUT.

 No special instruction is needed to access the contents of these registers.

 To perform this we use MOVE,LOAD,STORE instructions.

 Example:- MoveByte DATAIN, R1


MoveByte R1, DATAOUT

 The status flags SIN and SOUT are automatically cleared when the buffer register
DATAIN and DATAOUT are referenced respectively.

 Here two DATA register two address locations. In the same way status flags also two
distinct addresses.

 To track the status flag device status register used.

 Let us assume that bit b3 the status bit the following read and write operation by using
memory mapped I/O.

A complete program to read a character and write a character I/O operations


Stacks and queues(Pushdown Stack)
 A computer program often needs to perform a particular subtask using the familiar
subroutine structure. It is achieved through subroutines (functions).
 In order to organize the control and information linkage between the main program
and the subroutine, a data structure called a stack is used.
 A stack is a list of data elements, usually words or bytes, with the accessing
restriction that elements can be added or removed at one end of the list only.
 This end is called the top of the stack, and the other end is called the bottom.
 Another descriptive phrase, last-in-first-out (LIFO) stack, is also used to describe this
type of storage mechanism; the last data item placed on the stack is the first one
removed when retrieval begins.
 The terms push and pop are used to describe placing a new item on the stack and
removing the top item from the stack, respectively.
Figure Checking for empty and full errors in pop and push operations
Subroutines
In a given program, it is often necessary to perform a particular subtask many times on
different data-values. Such a subtask is usually called a subroutine. For example, a
subroutine may evaluate the sine function or sort a list of values into increasing or decreasing
order.
It is possible to include the block of instructions that constitute a subroutine at every place
where it is needed in the program. However, to save space, only one copy of the instructions
that constitute the subroutine is placed in the memory, and any program that requires the use
of the subroutine simply branches to its starting location. When a program branches to a
subroutine we say that it is calling the subroutine. The instruction that performs this branch
operation is named a Call instruction.
After a subroutine has been executed, the calling program must resume execution,
continuing immediately after the instruction that called the subroutine. The subroutine is said
to return to the program that called it by executing a Return instruction.
The way in which a computer makes it possible to call and return from subroutines is
referred to as its subroutine linkage method. The simplest subroutine linkage method is to
save the return address in a specific location, which may be a register dedicated to this
function. Such a register is called the link register. When the subroutine completes its task,
the Return instruction returns to the calling program by branching indirectly through the link
register.

The Call instruction is just a special branch instruction that performs the following
operations

• Store the contents of the PC in the link register


• Branch to the target address specified by the instruction
The Return instruction is a special branch instruction that performs the operation
• Branch to the address contained in the link register .
Fig a illustrates this procedure
SUBROUTINE NESTING AND THE PROCESSOR STACK:- A common programming
practice, called subroutine nesting, is to have one subroutine call another. In this case, the
return address of the second call is also stored in the link register, destroying its previous
contents. Hence, it is essential to save the contents of the link register in some other location
before calling another subroutine. Otherwise, the return address of the first subroutine will be
lost.

Subroutine nesting can be carried out to any depth. Eventually, the last subroutine called
completes its computations and returns to the subroutine that called it. The return address
needed for this first return is the last one generated in the nested call sequence. That is,
return addresses are generated and used in a last-in-first-out order. This suggests that the
return addresses associated with subroutine calls should be pushed onto a stack. A particular
register is designated as the stack pointer, SP, to be used in this operation. The stack pointer
points to a stack called the processor stack. The Call instruction pushes the contents of the
PC onto the processor stack and loads the subroutine address into the PC. The Return
instruction pops the return address from the processor stack into the PC.

PARAMETER PASSING:- When calling a subroutine, a program must provide to the


subroutine the parameters, that is, the operands or their addresses, to be used in the
computation. Later, the subroutine returns other parameters, in this case, the results of the
computation. This exchange of information between a calling program and a subroutine is
referred to as parameter passing. Parameter passing may be accomplished in several ways.
The parameters may be placed in registers or in memory locations, where they can be
accessed by the subroutine. Alternatively, the parameters may be placed on the processor
stack used for saving the return address.

The purpose of the subroutines is to add a list of numbers. Instead of passing the actual list
entries, the calling program passes the address of the first number in the list. This technique
is called passing by reference. The second parameter is passed by value, that is, the actual
number of entries, n, is passed to the subroutine.

THE STACK FRAME:- Now, observe how space is used in the stack in the example.
During execution of the subroutine, six locations at the top of the stack contain entries that
are needed by the subroutine. These locations constitute a private workspace for the
subroutine, created at the time the subroutine is entered and freed up when the subroutine
returns control to the calling program. Such space is called a stack frame. Fig a A subroutine
stack frame example.
The above figure shows an example of a commonly used layout for information in a stack
frame. In addition to the stack pointer SP, it is useful to have another pointer register, called
the frame pointer (FP), for convenient access to the parameters passed to the subroutine and
to the local memory variables used by the subroutine. These local variables are only used
within the subroutine, so it is appropriate to allocate space for them in the stack frame
associated with the subroutine. We assume that four parameters are passed to the subroutine,
three local variables are used within the subroutine, and registers R0 and R1 need to be saved
because they will also be used within the subroutine.

The pointers SP and FP are manipulated as the stack frame is built, used, and dismantled for a
particular of the subroutine. We begin by assuming that SP point to the old top-of-stack
(TOS) element in fig b. Before the subroutine is called, the calling program pushes the four
parameters onto the stack. The call instruction is then executed, resulting in the return address
being pushed onto the stack. Now, SP points to this return address, and the first instruction of
the subroutine is about to be executed. This is the point at which the frame pointer FP is set to
contain the proper memory address. Since FP is usually a general-purpose register, it may
contain information of use to the Calling program. Therefore, its contents are saved by
pushing them onto the stack. Since the SP now points to this position, its contents are copied
into FP.

Thus, the first two instructions executed in the subroutine are

Move FP, -(SP)


Move SP, FP

After these instructions are executed, both SP and FP point to the saved FP contents.

Subtract #12, SP

Finally, the contents of processor registers R0 and R1 are saved by pushing them onto the
stack. At this point, the stack frame has been set up as shown in the fig.

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 pops the saved old value of FP back into FP. At this point, SP points to the return
address, so the Return instruction can be executed, transferring control back to the calling
program.
Stack Frame for Nested Subroutines

Вам также может понравиться