CO R20 - UNIT-3 (Ref-3)
CO R20 - UNIT-3 (Ref-3)
COMPUTER ARITHMETIC
Introduction:
If we want to solve a problem then we use a sequence of well-defined steps. These steps
are collectively called algorithm. To solve various problems we give algorithms.
In order to solve the computational problems, arithmetic instructions are used in digital
computers that manipulate data. These instructions perform arithmetic calculations.
And these instructions perform a great activity in processing data in a digital computer.
As we already stated that with the four basic arithmetic operations addition, subtraction,
multiplication and division, it is possible to derive other arithmetic operations and solve
scientific problems by means of numerical analysis methods.
A processor has an arithmetic processor(as a sub part of it) that executes arithmetic
operations. The data type, assumed to reside in processor, registers during the execution
of an arithmetic instruction. Negative numbers may be in a signed magnitude or signed
complement representation. There are three ways of representing negative fixed point -
binary numbers signed magnitude, signed 1’s complement or signed 2’s complement.
Most computers use the signed magnitude representation for the mantissa.
Addition and Subtraction :
Addition and Subtraction with Signed –Magnitude Data
We designate the magnitude of the two numbers by A and B. Where the signed numbers
are added or subtracted, we find that there are eight different conditions to consider,
depending on the sign of the numbers and the operation performed. These conditions are
listed in the first column of Table 4.1. The other columns in the table show the actual
operation to be performed with the magnitude of the numbers. The last column is needed
to present a negative zero. In other words, when two equal numbers are subtracted, the
result should be +0 not -0. The algorithms for addition and subtraction are derived from
the table and can be stated as follows (the words parentheses should be used for the
subtraction algorithm).
Addition and Subtraction of Signed-Magnitude Numbers
Computer Organization
www.Jntufastupdates.com 1
Computer Organization
E Output Input
Parallel Adder
Carry Carry
S
As A Register Load Sum
Complementer and
V
Parallel Adder
Overflow
AC
Algorithm
Subtract Add
Minuend in AC Augend in AC
Subtrahend in B Addend in B
AC m AC + B’+ 1 AC m AC + B
V m overflow V m overflow
END END
Algorithm:
The flowchart is shown in Figure 7.1. The two signs A, and B, are
compared by an exclusive-OR gate.
Computer Organization
www.Jntufastupdates.com 2
Computer Organization
The two magnitudes are subtracted if the signs are different for an add
operation or identical for a subtract operation. The magnitudes are subtracted
by adding A to the 2's complemented B. No overflow can occur if the numbers
are subtracted so AVF is cleared to 0.
1 in E indicates that A >= B and the number in A is the correct result. If this
numbs is zero, the sign A must be made positive to avoid a negative zero.
0 in E indicates that A < B. For this case it is necessary to take the 2's
complement of the value in A. The operation can be done with one
microoperation A A' +1.
However, we assume that the A register has circuits for microoperations
complement and increment, so the 2's complement is obtained from these two
microoperations.
In other paths of the flowchart, the sign of the result is the same as the sign of
A. so no change in A is required. However, when A < B, the sign of the result
is the complement of the original sign of A. It is then necessary to complement
A, to obtain the correct sign.
The final result is found in register A and its sign in As. The value in AVF
provides an overflow indication. The final value of E is immaterial.
Figure 7.2 shows a block diagram of the hardware for implementing the
addition and subtraction operations.
It consists of registers A and B and sign flip-flops As
and Bs. Subtraction is done by adding A to the 2's
complement of B.
Computer Organization
www.Jntufastupdates.com 3
Computer Organization
Multiplication Algorithm:
In the beginning, the multiplicand is in B and the multiplier in Q. Their
corresponding signs are in Bs and Qs respectively. We compare the signs
of both A and Q and set to corresponding sign of the product since a
double-length product will be stored in registers A and Q. Registers A and
E are cleared and the sequence counter SC is set to the number of bits of
the multiplier. Since an operand must be stored with its sign, one bit of the
word will be occupied by the sign and the magnitude will consist of n-1
bits.
Computer Organization
www.Jntufastupdates.com 4
Computer Organization
Booth’s algorithm :
Booth algorithm gives a procedure for multiplying binary
integers in signed- 2’s complement representation.
It operates on the fact that strings of 0’s in the multiplier require no addition
but just
Computer Organization
www.Jntufastupdates.com 5
Computer Organization
shifting, and a string of 1’s in the multiplier from bit weight
2k to weight 2m can be treated as 2k+1 – 2m.
For example, the binary number 001110 (+14) has a string 1’s
from 23 to 21 (k=3, m=1). The number can be represented as 2k+1 –
2m. = 24 – 21 = 16 – 2 = 14. Therefore, the multiplication M X 14,
where M is the multiplicand and 14 the multiplier, can be done
as M X 24 – M X 21.
Thus the product can be obtained by shifting the binary
multiplicand M four times to the left and subtracting M shifted
left once.
Computer Organization
www.Jntufastupdates.com 6
Computer Organization
If the two bits are equal to 10, it means that the first 1 in a string
of 1 's has been encountered. This requires a subtraction of the
multiplicand from the partial product in AC.
If the two bits are equal to 01, it means that the first 0 in a string
of 0's has been encountered. This requires the addition of the
multiplicand to the partial product in AC.
When the two bits are equal, the partial product does not change.
Division Algorithms
Division of two fixed-point binary numbers in signed magnitude
representation is performed with paper and pencil by a process of
successive compare, shift and subtract operations. Binary division is much
simpler than decimal division because here the quotient digits are either 0
or 1 and there is no need to estimate how many times the dividend or
partial remainder fits into the divisor. The division process is described in
Figure
Computer Organization
www.Jntufastupdates.com 7
Computer Organization
The devisor is compared with the five most significant bits of the dividend. Since the 5-bit number is smaller
than B, we again repeat the same process. Now the 6-bit number is greater than B, so we place a 1 for the
quotient bit in the sixth position above the dividend. Now we shift the divisor once to the
right and subtract it from the dividend. The difference is known
as a partial remainder because the division could have stopped
here to obtain a quotient of 1 and a remainder equal to the partial
remainder. Comparing a partial remainder with the divisor
continues the process. If the partial remainder is greater than or
equal to the divisor, the quotient bit is equal to
1. The divisor is then shifted right and subtracted from the partial
remainder. If the partial remainder is smaller than the divisor, the
quotient bit is 0 and no subtraction is needed. The divisor is
shifted once to the right in any case. Obviously the result gives
both a quotient and a remainder.
Computer Organization
www.Jntufastupdates.com 8
Computer Organization
\Algorithm:
Basic Considerations :
m x re
.53725 x 103
+ (1 – 2-35) x 22047
Computer Organization
www.Jntufastupdates.com 10
Computer Organization
Computers with shorter word lengths use two or more words to represent a
floating-point number. An 8-bit microcomputer uses four words to
represent one floating-point number. One word of 8 bits are reserved for
the exponent and the 24 bits of the other three words are used in the
mantissa.
Arithmetic operations with floating-point numbers are more complicated
than with fixed-point numbers. Their execution also takes longer time and
requires more complex hardware. Adding or subtracting two numbers
requires first an alignment of the radix point since the exponent parts must
be made equal before adding or subtracting the mantissas. We do this
alignment by shifting one mantissa while its exponent is adjusted until it
becomes equal to the other exponent. Consider the sum of the following
floating-point numbers:
.5372400 x 102
+ .1580000 x 10-1
The operations done with the mantissas are the same as in fixed-point
numbers, so the two can share the same registers and circuits. The
operations performed with the exponents are compared and incremented
(for aligning the mantissas), added and subtracted (for multiplication) and
division), and decremented (to normalize the result). We can represent the
exponent in any one of the three representations - signed-magnitude, signed
2’s complement or signed 1’s complement.
Biased exponents have the advantage that they contain only positive
numbers. Now it becomes simpler to compare their relative magnitude
without bothering about their signs. Another advantage is that the smallest
possible biased exponent contains all zeros. The floating-point
Computer Organization
www.Jntufastupdates.com 11
Computer Organization
F = m x re
where m: Mantissa
r: Radix
e: Exponent
Bs B b BR
Parallel Adder
E Parallel Adder and Comparator
As A1 A a AC
Qs Q q QR
In the similar way, register BR is subdivided into Bs, B, and b and QR into
Computer Organization
www.Jntufastupdates.com 12
Computer Organization
Qs, Q and q. A parallel-adder adds the two mantissas and loads the sum
into A and the carry into E. A separate parallel adder can be used for the
exponents. The exponents do not have a district sign bit because they are
biased but are represented as a biased positive quantity. It is assumed that
the floating- point number are so large that the chance of an exponent
overflow is very remote and so the exponent overflow will be neglected.
The exponents are also connected to a magnitude comparator that provides
three binary outputs to indicate their relative magnitude.
The number in the mantissa will be taken as a fraction, so they binary point
is assumed to reside to the left of the magnitude part. Integer representation
for floating point causes certain scaling problems during multiplication and
division. To avoid these problems, we adopt a fraction representation.
If the magnitudes were subtracted, there may be zero or may have an underflow in
the result. If the mantissa is equal to zero the entire floating-point number in the
Computer Organization
www.Jntufastupdates.com 13
Computer Organization
AC is cleared to zero. Otherwise, the mantissa must have at least one bit that is
equal to 1. The mantissa has an underflow if the most significant bit in position
A1, is 0. In that case, the mantissa is shifted left and the exponent decremented.
The bit in A1 is checked again and the process is repeated until A1 = 1. When A1
= 1, the mantissa is normalized and the operation is completed.
Computer Organization
www.Jntufastupdates.com 14
Computer Organization
Computer Organization
www.Jntufastupdates.com 15
Computer Organization
Multiplication:
=0
BR
z0
=0 AC
Qs m As + Bs z 0
Q m 0 SC
QR m 0 m n-1
EA m A+B’+1
divide
by 0
1 E 0
A>=B A<B
A m A+B A m A+B
shr A a
m a+1
a m a+b’+1
a m a+bias
qma
Computer Organization
www.Jntufastupdates.com 16
Computer Organization
UNIT III
Basic Computer Organization and Design
Instruction codes. Computer Registers Computer instructions, Timing and
Control, Instruction cycle. Memory Reference Instructions, Input – Output and
Interrupt, Complete Computer Description.
-----------------------------------------------------------------------------------
-------------------------
Instruction Formats:
Computer Organization
www.Jntufastupdates.com 17
Computer Organization
Computer Organization
www.Jntufastupdates.com 18
Computer Organization
Using zero, one, two, or three address instruction. We will use the
symbols ADD, SUB, MUL, and DIV for the four arithmetic operations;
MOV for the transfer-type operation; and LOAD and STORE for transfers
to and from memory and AC register. We will assume that the operands are
in memory addresses A, B, C, and D, and the result must be stored in
memory at address X.
Three-Address Instructions
Computers with three-address instruction formats can use each
address field to specify either a processor register or a memory operand.
The program in assembly language that evaluates X = (A + B) 䌫 (C + D) is
shown below, together with comments that explain the register transfer
Computer Organization
www.Jntufastupdates.com 19
Computer Organization
Two-Address Instructions
Two address instructions are the most common in commercial computers.
Here again each address field can specify either a processor register or a
memory word. The program to evaluate X = (A + B) 䌫 (C + D) is as
follows:
MOV R1, A R1 ← M [A]
ADD R1, B R1 ← R1 + M [B]
MOV R2, C R2 ← M [C]
ADD R2, D R2 ← R2 + M [D]
MUL R1, R2 R1 ← R1䌫R2
MOV X, R1 M [X] ← R1
Computer Organization
www.Jntufastupdates.com 20
Computer Organization
One-Address Instructions
One-address instructions use an implied accumulator (AC) register for
all data manipulation. For multiplication and division there is a need for a
second register. However, here we will neglect the second and assume that
the AC contains the result of tall operations. The program to evaluate X =
(A + B) 䌫 (C + D) is
LOAD A AC ← M [A]
ADD B AC ← A [C] + M [B]
STORE T M [T] ← AC
LOAD C AC ← M [C]
ADD D AC ← AC + M [D]
MUL T AC ← AC 䌫 M [T]
STORE X M [X] ← AC
All operations are done between the AC register and a memory
operand. T is the address of a temporary memory location required for
storing the intermediate result.
Zero-Address Instructions
A stack-organized computer does not use an address field for the
instructions ADD and MUL. The PUSH and POP instructions, however,
need an address field to specify the operand that communicates with the
stack. The following program shows how X = (A + B) 䌫 (C + D) will be
written for a stack organized computer. (TOS stands for top of stack)
PUSH A TOS ← A
PUSH B TOS ← B
ADD TOS ← (A + B)
PUSH C TOS ← C
PUSH D TOS ← D
ADD TOS ← (C + D)
MUL TOS ← (C + D) 䌫 (A + B)
POP X M [X] ← TOS
Computer Organization
www.Jntufastupdates.com 21
Computer Organization
Format of Instruction
The format of an instruction is depicted in a rectangular box symbolizing the bits
of an instruction. Basic fields of an instruction format are given below:
1. An operation code field that specifies the operation to be performed.
2. An address field that designates the memory address or register.
3. A mode field that specifies the way the operand of effective address is
determined.
Addressing Modes
To understand the various addressing modes to be presented in this section,
it is imperative that we understand the basic operation cycle of the computer.
The control unit of a computer is designed to go through an instruction cycle
that is divided into three major phases:
Computer Organization
www.Jntufastupdates.com 22
Computer Organization
3 Register Mode: In this mode the operands are in registers that reside within
the CPU.The particular register is selected from a register field in the instruction. A k-
bit field can specify any one of 2k registers.
4 Register Indirect Mode: In this mode the instruction specifies a register in the
CPUwhose contents give the address of the operand in memory. In other words, the
Computer Organization
www.Jntufastupdates.com 23
Computer Organization
selected register contains the address of the operand rather than the operand itself.
Before using a register indirect mode instruction, the programmer must
ensure that the memory address fo the operand is placed in the processor
register with a previous instruction. A reference to the register is then
equivalent to specifying a memory address. The advantage of a register
indirect mode instruction is that the address field of the instruction sues
fewer bits to select a register than would have been required to specify a
memory address directly.
5 Auto increment or Auto decrement Mode: This is similar to the register indirect
modeexcept that the register is incremented or decremented after (or before) its value is
used to access memory. When the address stored in the register refers to a table of data
in memory, it is necessary to increment or decrement the register after every access to
the table. This can be achieved by using the increment or decrement instruction.
However, because it is such a common requirement, some computers incorporate a
special mode that automatically increments or decrements the content of the register
after data access.
6 Direct Address Mode: In this mode the effective address is equal to the address part
ofthe instruction. The operand resides in memory and its address is given directly by
the address field of the instruction. In a branch-type instruction the address field
specifies the actual branch address.
7 Indirect Address Mode: In this mode the address field of the instruction gives
theaddress where the effective address is stored in memory. Control fetches the
instruction from memory and uses its address part to access memory again to read the
Computer Organization
www.Jntufastupdates.com 24
Computer Organization
effective address.
8 Relative Address Mode: In this mode the content of the program counter is added to
theaddress part of the instruction in order to obtain the effective address. The address
part of the instruction is usually a signed number (in 2’s complement representation)
which can be either positive or negative. When this number is added to the content of
the program counter, the result produces an effective address whose position in
memory is relative to the address of the next instruction. To clarify with an example,
assume that the program counter contains the number 825 and the address part of the
instruction contains the number 24. The instruction at location 825 is read from
memory during the fetch phase and the program counter is then incremented by one
to 826 + 24 = 850. This is 24 memory locations forward from the address of the next
instruction. Relative addressing is often used with branch-type instructions when the
branch address is in the area surrounding the instruction word itself. It results in a
shorter address field in the instruction format since the relative address can be
specified with a smaller number of bits compared to the number of bits required to
designate the entire memory address.
9 Indexed Addressing Mode: In this mode the content of an index register is added
to theaddress part of the instruction to obtain the effective address. The index
register is a special CPU register that contains an index value. The address field of
the instruction defines the beginning address of a data array in memory. Each
operand in the array is stored in memory relative to the beginning address. The
distance between the beginning address and the address of the operand is the index
value stores in the index register. Any operand in the array can be accessed with
the same instruction provided that the index register contains the correct index
value. The index register can be incremented to facilitate access to consecutive
operands. Note that if an index-type instruction does not include an address field in
its format, the instructionconverts to the register indirect mode of operation. Some
computers dedicate one CPU register to function solely as an index register. This
register is involved implicitly when the index-mode instruction is used. In
computers with many processor registers, any one of the CPU registers can contain
the index number. In such a case the register must be specified explicitly in a
register field within the instruction format.
10 Base Register Addressing Mode: In this mode the content of a base register is
added tothe address part of the instruction to obtain the effective address. This is
similar to the indexed addressing mode except that the register is now called a base
register instead of an index register. The difference between the two modes is in
the way they are used rather than in the way that they are computed. An index
Computer Organization
www.Jntufastupdates.com 25
Computer Organization
register is assumed to hold an index number that is relative to the address part of
the instruction. A base register is assumed to hold a base address and the address
field of the instruction gives a displacement relative to this base address. The base
register addressing mode is used in computers to facilitate the relocation of
programs in memory. When programs and data are moved from one segment of
memory to another, as required in multiprogramming systems, the address values
of the base register requires updating to reflect the beginning of a new memory
segment.
Numerical Example
Computer Organization
www.Jntufastupdates.com 26
Computer Organization
Computer Registers
Computer Organization
www.Jntufastupdates.com 27
Computer Organization
Computer Instructions:
The basic computer has 16 bit instruction register (IR) which can denote either
memory reference or register reference or input-output instruction.
1. Memory Reference – These instructions refer to memory address as an
operand. The other operand is always accumulator. Specifies 12 bit
address, 3 bit opcode (other than 111) and 1 bit addressing mode for direct
and indirect addressing.
Example –
IR register contains = 0001XXXXXXXXXXXX, i.e. ADD after fetching
and decoding of instruction we find out that it is a memory reference
instruction for ADD operation.
Hence, DR <- M[AR]
AC <- AC+ DR, SC <- 0
2. Register Reference – These instructions perform operations on registers
rather than memory addresses. The IR(14-12) is 111 (differentiates it from
memory reference) and IR(15) is 0 (differentiates it from input/output
instructions). The rest 12 bits specify register operation.
Example –
IR register contains = 0111001000000000, i.e. CMA after fetch and decode
cycle we find out that it is a register reference instruction for complement
accumulator.
Hence, AC <- ~AC
3. Input/Output – These instructions are for communication between
computer and outside environment. The IR(14-12) is 111 (differentiates it
from memory reference) and IR(15) is 1 (differentiates it from register
reference instructions). The rest 12 bits specify I/O operation.
Example –
IR register contains = 1111100000000000, i.e. INP after fetch and decode
cycle we find out that it is an input/output instruction for inputing
character. Hence, INPUT character from peripheral device.
Computer Organization
www.Jntufastupdates.com 28
Computer Organization
Instruction Cycle
1. Fetch instruction
2. Decode
3. Fetch operand
4. Execute
1. Program execution
a. Instruction 1
i. Fetch instruction
ii. Decode
iii. Fetch operand
iv. Execute
b. Instruction 2
i. Fetch instruction
ii. Decode
iii. Fetch operand
iv. Execute
c. Instruction 3 ...
T0: AR ← PC
T1: IR ← M[AR], PC ← PC + 1
Computer Organization
www.Jntufastupdates.com 29