Arithmetic Algorithm
Arithmetic Algorithm
Arithmetic Algorithm
uact,, and
and toto co Pcate
the signs and the magnitudes of the numbers. Usually.
ually, an algorithCOmpat
contain a number of procedural steps which are dependenorithn
nt
previous steps. A convenient method for presenting algorithmo on
ns is a results t
The computational steps are specified in the flowchart insiddlowch flowcha
boxes. The decision steps are indicated inside diamond-shanoa
which two or more alternate paths emerge.
rectangalfroam
-shaped boxes
In this chapter we develop the various arithmetic algorithma
the procedure implementing them with digital hardware. Ne
for
addition, subtraction, multiplication, and division for the following tconsiden
data: type
1. Fixed-point binary data in signed-magnitude representation
2. Fixed-point binary data in signed-2's complement representation
3. Floating-point binary data
4. Binary-coded decimal (BCD) data
Operation +(A - B)
+(A +B) -(B A )
+(A B ) +(A B )
(+A) + (+B) +(B A )
-(A-B)
(+A) +(-B)
-(B A ) +(A B )
+ (+B) -(A + B) +(A B )
)
(-A) + (-B)
(+B) +(A + B) +(A B )
+(B-A)
(+A) (-B) -(A + B) -
B)
-(A
(+A) -
(-A) (+B)
(-4)-(-B)
CHAPTER TEN Computer Aritlhmetic
Hardware Implementation
To implement the two arithmetic operations with hardware, it is first necese
that the two numbers be stored in registers. LetA and B be two registersth
hold the magnitudes of the numbers, and A, and B, be two fip-tlops that hol
the corresponding signs. The result of the operation may be transíerred tba
third register: however, a saving is achieved if the result is transferred into
and A. Thus A and A, together form an accumulator register.
Consider now the hardware implementation of the algorithms above.
First, a parallel-adder is needed to perform the microoperation A + B. Second,
a comparator circuit is needed to establish if A> B, A = B, or A << B. Third,
two parallel-subtractor circuits are needed to perform the microoperations
A B and B - A. The sign relationship can be determined from anexclusive
OR gate with A, and B, as inputs.
and fwo
This procedure requires a magnitude comparator, an adder,
subtractors. However, a different procedure can be found that requires ks
equipment. First, we know that subtraction can be accomplished by meanso
and add. Second, the result of a comparison can be determined
complement
from the end carry after the subtraction. Careful investigation of the altern
tives reveals that the use of 2's complement for subtraction and comparis
an efficient procedure that requires only an adder and a complementer the
AVF holds the overflow bit when A and B are added. The the
flip-flop
provides other microoperations that may be needed when we sP
sequence of steps in the algorithm. (sum)
the
incr
provides an output of B or the complement of B depending on rtes and
mode control M. The complementer consists of ex exclusive-OR gates Chap 4
in
adder consists of full-adder shown in Fig. *M 0
parallel circuits as
outputo
The M signal is also applied to the input carry of the adder. viht
output of B is transferred to the adder, the input carry is 0, ar
SECTION 10-2 Addition and Subtraction 337
B register
Output
carry Parallel adder
Input carry
A register
Hardware Algorithm
The flowchart for the hardware
algorithm is presented in Fig. 10-2. The two
signs A, and B, are compared by an excusive-OR gate. If the output of the gate
is 0, the signs are identical; if it is 1, the
signs are different. For an add operation,
identical signs dictate that the magnitudes be added. Fora subtract operation,
different signs dictate that the magnitudes be added. The magnitudes are
added with a microoperation EA-A + B, where EA is a register that com-
bines E and A. The carry in E atter the addition constitutes an overflow if it is
equal to 1. The value of E is transferred intothe add-overflow flip-flop AVF.
The two magnitudes are subtracted if the signs are different for an add
operation or identical for a subtract operation. The magnitudes are subtracted
Minuend in A
Subtrahend in B
Augend in A
Addend in B
= 0 =1
A,DB, 0
A,®B,
A, B, A, B,
4,+B A,-
EA-A +B+1 EA A +B
AVF+0
0 AVF-E
A <B AB
#0
A-A +I|
4,+A,
END
(result is in A and
A,)
Figure 10-2 Flowchart for add and subtract operations.
The signed-2's
metic algorithmscomplement
for
representation of
tog inn numbers Sec.
3-3 Th
addition and subtraction are introduced
binarynu
are
summarized here for easy reference. The. o f a Signbitisl
represents the sign bit: 0 for positive and 1 fornegative. If the resen
entire number is represented in 2's complementform. Thus +33 is rep
as 0010 SECTION 10-2 Addition and Subtraction
as 100001 and -33 as 339
00100001, and vice 11011111. Note that 011111
versa. 11011111 is the 2's complement of
The addition
of two
adding the numbers with numbers in signed-2'
compie
number. A the sign bits ned-2'ss complement form consists of
complement
carry-out of the sign-bit treated the same
same as
as the other bits of the
the otner
consists of first
taking the 2's position is discarded. The
it to the
minuend. subtractlo
complement of the subtrahend and then adu6
When two
numbers of
n digits
each are added and the sum
digits, we say that an overflow
occupiesn t 1
of two
signed-2's complement occurred.
can be detected
The effect of an overflow
numbers is discussed in Sec. 3-3. An
on the
sunm
by
the two carries areinspecting the last two carries out of the addition.overt
when the output ofapplied
to an exclusive-OR when
gate, the overflow 1s detectea
the gate is
The equal to 1.
register configuration for the
Fig. 10-3. This is the same configurationhardware implementation is shown in
as in Fig. 10-1
are not except that the sign bits
separated from the rest of the registers. We name the A
(accumulator) and the B register BR. The leftmost bit in AC and BRregister
AC
complement addition
and subtraction.
Figure 10-3 Hardware for signed-2's
BR register
Complementer and
parallel adder
Overflow
AC register
340 CHAPTER TEN
Computer Arithmetic
Subtract
Add
Minuend in AC
Subtrahend in BR Augend in AC
Addend in BR
AC-AC+ BR + 1
Voverflow AC-AC+ BR
V-overflow
END
END
Figure10-4 Algorithm for
complement representation. adding and
subtracting numbers in
signed-2
Complementer and
parallel adder
(rightmost bit)
On
Q register
A register
342 CHAPTER TEN Computer Arithmetic
Multiply operation
Multiplicand in B
Multiplier in Q
A,0,B,
0,-0,0B,
A0, E+0
SCn- 1
EAA+B
shr EAQ
SC+SC
0 0
END
(product is in AQ)
SECTION
e correspond to the sign of the Multiplication Algorithms 343
10.3
can
has a string of 1's from 2 to 2
example, the binary number 001110 (+14)
E A Q SC
Multiplicand B
=
10111
add B 0 11011
O. 1; 000
=
01101 10101
Fifth partial product 00
Shift right EAQ
=
0110110101
Final product in AQ
344 CHAPTER TEN Computer Arithmetic
(k 3, mn =
The number can be represented as 2*1_.
1). =
MM X 14, where M is tha- 22
+12
2 14. Therefore, the multiplication
14 the multiplier, can be done as M X 2"- M x 2. Thus th
-2=1
plicand
obtained by shifting the binary multiplicand M four
our times times product
to cany
the let
subtracting M shifted left once.
As in all multiplication schemes, Booth algorithm requires.
of the multiplier bits and shifting of the partial product. Prior to tho
the multiplicand may be added to the partial product, subtracte
partial product, or left unchanged according to the following rules
Complementer and
parallel adder
On
AC register
OR register
SECTION 10.3 Multiplication Algorithms 345
Multiply
Multiplicand in BR
Multiplier in QR
AC
en+10
SC
01
10
Q,n+1
00
= 11 AC-AC+ + BR
AC AC +BR+1
=0
SC
#0
END
complemene
multiplication
of signed-2's
for
algorithm
Booth
10-8
Figure
numbers
CPU Organisation
Memory Address Register (MAR): Used to provide address of of memorymemory location
where data is to be retricved or to which data is to be stored.
from
The contents of PC is modified cither aftcr fctching an instruction or by a bran
instruction. MAR and DR plays important roles in
ransier of data betwcen O
memory. In thecomputersystems which use system bus,MAR is directlyV PU andsSkithe
address bus, while DR is dircctly connected to data bus. DR is also
among scveral other registers.
used toto conne
inted
intercange data
Arithmetic Logic
Unit (ALU)
Data Procesting
Unit
Accumulator
To/from
main memory
or Input/Output
HR (AC)
Con
the
Device
Program Instruction
Counter(PC) Register TR)
Program
Control
Unit
Control
Unit
2.3 AN
ADVANCED STRUCTURE
Let us have few
made more additional features in the
powerful. Some of these simple structure of the CPU such that it
aspects are: can be
Provide additional
accumulator by a setregisters for storing
of registers. Theseoperands and addresses, thus,
registers which are registers can be used as replacing a sing
A Set of these generalmulti-purpose nature (e.g. can store either
in general purpose
of these registers is to registers may be temed as a operands
register file. One of
or
addresses
address. The IBM S/360-370the operands which are needed for calculationtheofkey funcuo
store
ztin or by a bra
data between nch or skip whether the result
Ris CPU and is
General
Purpose Arithmetic
Logic
Register Set Unit ALU
Data
Processing
Programn
Control
Unit
Address
Control
Programn
Control
Unit
Program
Counter
hat it can be Logic for
Stack Address generation Control UnitE
Pointer
lacing a single
urpose Figure 2: CPU with general register organ1sation
raddresses.
e key function The general purpose registers can be in general 8-16. ALU performs operation on the data
of memory stored in these general purpose registers and also stores results in these registers only. There
ion. In some are few special-purpose address registers. Two of these are Program Counter (PC) and Stack
register, ec. Pointer (SP). The status register stores the key characteristics or conditions of the result of
ch have a set the last ALLU operation. A special simple arithmetic logic unit can be attached as an address
generation logic performing the simple fixed point computations. The inputs to the control
unit are from the instruction register which contains the operation code of the instruction to
r have be executed and from status register which help in generating proper control signals on
This branch operation. The system bus plays the role of communication media. Please note the
ivision on direction of arrows on address bus. There are several inermediate buffer registers also which
point help in intermediate
storage of infomation. In this organisation of CPU parallelism can be
implemented within the ALU operation or through the overlapped operations of data
processing and program control unit.
atus
the result,
True False o f re
Ho
2.4 REGISTER ORGANISATION cha
Com
me
n the previous subsection, we have given some
CPU must have. Let hints to the types of Registers a reg
The
us take a more
internal processor general view on register organisation in moderm day exe
memory of a CPU is served by its this seCion.
among various computers is the difference in their registers. One of the key difference
large while some has smaller sets. register sets. Some computers have very Reg
set can be But on the whole, from a user's reg
classified under two basic point of view the register
categories. whE
Programmer Visible Registers: These add
registers can be used by machine or
assembly language programmers to minimize is s
the references to main
Status Control and memory. wh
Registers: These registers can not be used by the
but are used to control
the CPU or the execution of a programmers Co
Different vendors has used some program. Co
of these
stick to these definitions registers interchangeably, therefore you should not har
rigidly. Yet this
register sets of machine. Therefore, let us categorisation will help in better understanding of ovE
discuss more about these
categories. tes
2.4.1 col
Programmer Visible Registers In
These registers can be accessed using machine In
of
programmer visible registers. language. In general we encounter four types ins
General Purpose Registers On
Data Registers wh
cal
Address Registers Th
Condition Codes Registers prc
prc
The general
purpose registers are used for various
general purpose register can contain functions desired by the
2.
operand for any operation code of an operand or can be used for calculationprocessor.
of address
Atuue
towards dedicated instruction. But trends in of Fo
operations. In someregisters, today's machines show drift
for example. some in
machines there is a distinct registers may be dedicated to
registers. separation between data and floating
address
point prc
Co
(The data registers are used for
are not used for only storing intermediate Al
calculation of address of the results or data. These data registers pr
An address register may be a operand.
34 general purpose register, but some
COC
be:
are also
used in several machines.
Examples of dedicated addressdedicated address registers
registers can be
oncnts. In the
llowed by the Segment Pointer
Uscd to point out a
ithmctic
this Block. Index Register These are uscd for segment of memory. Register Organis
Suck Pointer (when index addressing scheme. and Micro-Operi
dern day register reference, therefore, the increased number of memory references results in slower
tion. execution of a program.
ifference Register Length: Another important characteristic related to registers is the length of a
ve very register. Nomally, the length of a register is dependen. on its use. For example, a register
register which is used to calculate address must be long enough to hold the maximum possible
address. Similarly, the length of data register should be long enough to hold the data type it
is supposed to hold. In certain cases two consecutive registers may be used to hold data
whose length is double of the register length.
ammers Condition code registers may only be partially available to the programmers. These register
contains condition codes which are also known as flags. These fags are set by the CPU
hardware while performing anoperation. For taample, an addition operation may set the
ld not overflow flag or on a division by 0 the overflow flag can be set etc. These codes may be
ng of tested by a program for a typical conditional branch operation. The condition codes are
collected in one or more registers. RISC machines have several set of conditional code bits.
In these machines an instruction specifies the set of condition codes which is to be used.
Independent sets of sondition code enable the provisions of having parallelism within the
instruction execution unit.
types
One of the key operation which is needed with the programmer uaable registers happens
when a subroutine call is issued. On a subroutine call all the registers either are saved by the
call statement itself and restored on encountering a retum statement from thesubroutne.
This operation in most machines is automatic yet in certain machines this is done by the
programmers. Similarly while writing interrupt service routine you need to save some or all
programmer usable registers.