MODULE-1 (B) - Machine-Instructions-and-Programs
MODULE-1 (B) - Machine-Instructions-and-Programs
and Architecture
Carl Hamacher, Zvonko Vranesic, Safwat
Zaky,
Computer Organization, 5th
Edition,
Tata McGraw Hill, 2002.
Module-1
Machine Instructions and
Programs
Numbers, Arithmetic
Operations, and
Characters
Introduction
Computers are built using logic
circuits that operate on information
represented by two- valued
electrical signals
Labelled as 0 and 1
We define the amount of information
represented by such a signal as a bit of
information, where bit stands for binary
digit.
The most natural way to represent a
Number Representation
𝐵 = 𝑏𝑛−1 … … 𝑏1𝑏0
Consider an n-bit vector
Where 𝑏𝑖 = 0 𝑜𝑟 1 for 0 ≤ 𝑖
≤𝑛−1
− 1, where
𝑉 𝐵 = 𝑏𝑛−1 × 2𝑛−1 + ⋯ + 𝑏1 × 21 + 𝑏0
× 20
Signed Integer
3 major representations:
Sign-and-magnitude
1’s complement
2’s complement
Assumptions:
4-bit machine word
16 different values can be
represented
Roughly half are positive, half are
negative
Sign-and-Magnitude Representation
-7
-6 +0 +1
1111 0000
-5 1110 +2 +
1101 0010
0001
-4 1100 0 100 = + 4
0011 +3
-3 1011 0100 +4 1 100 = - 4
1010
-2 0101 -
1001 +5
-1 1000 0111 0110
+6
-0 +7
High order bit is sign: 0 = positive (or zero), 1 = negative
Three low order bits is the magnitude: 0 (000) thru 7 (111)
Number range for n bits = +/-2n-1 -1
Two representations for 0
1’s Complement Representation
-0
-1 +0 +1
1111 0000
-2 1110 +2 +
1101 0010
0001
-3 1100 0 100 = +
0011 +3
4
-4 1011 0100 +4 1 011 = -
4
1010
-5 0101 -
1001 +5
-6 1000 0111 0110
+6
-7 +7
Subtraction implemented by addition & 1's
complement
Still two representations of 0! This causes some
problems
Some complexities in addition
2’s Complement Representation
-1
-2 +0 +1
1111 0000
-3 1110 +2 +
like 1's comp 1101 0010
0001
except shifted -4 1100 0 100 = +
one position 0011 +3
4
clockwise -5 1011 0100 +4 1 100 = -
4
1010
-6 0101 -
1001 +5
-7 1000 0111 0110
+6
-8 +7
positive
number
Binary, Signed-Integer
Representations
B Values represented
Page
28 Sign and
b3 b 2 b 1 b 0 magnitude 1's complement 2's complement
0 1 1 1 +7 +7 + 7
0 1 1 0 +6 +6 + 6
0 1 0 1 +5 +5 + 5
0 1 0 0 +4 +4 + 4
0 0 1 1 +3 +3 + 3
0 0 1 0 +2 +2 + 2
0 0 0 1 +1 +1 + 1
0 0 0 0 +0 +0 + 0
1 0 0 0 - 0 -7 - 8
1 0 0 1 - 1 -6 - 7
1 0 1 0 - 2 -5 - 6
1 0 1 1 - 3 -4 - 5
1 1 0 0 - 4 -3 - 4
1 1 0 1 - 5 -2 - 3
1 1 1 0 - 6 - 1 - 2
1 1 1 1 - 7 -0 - 1
through +2𝑛−1 − 1.
Addition and Subtraction – 2’s
Complement..
subtract two numbers 𝑋 and 𝑌,
that is, to perform 𝑋 − 𝑌, form the 2’s
To
+ 2𝑛−1 − 1.
through
Examples
4 -4 1100
0100
+ (-3) 1101
If carry-in to the high +3 0011
order bit = 7 0111 -7 11001
carry-out then ignore
carry
if carry-in differs from 4 0100 -4 1100
carry-out then overflow
-3 1101 +3 0011
1 10001 -1 1111
-1 +0 -1 +0
-2 1111 0000 +1 -2 1111 0000 +1
1110 0001 1110 0001
-3 +2 -3
1101 1101 +2
0010 0010
-4 -4
1100 0011 +3 1100 0011 +3
-5 1011 -5 1011
0100 +4 0100 +4
1010 1010
-6 0101 -6 0101
1001
+5 +5
0110 1001 0110
-7 1000 0111 +6 -7 1000 +6
0111
-8 +7 -8 +7
5 + 3 = -8 -7 - 2 = +7
Overflow Conditions
0111 1000
5 0101 -7 1001
3 0011 -2 1100
-8 1000 7 10111
Overflow Overflow
0000 1111
5 0101 -3 1101
2 0010 -5 1011
7 0111 -8 11000
No overflow No overflow
Overflow when carry-in to the high-order bit does not equal carry out
Characters
In addition to numbers, computers
must be able to handle nonnumeric
text information consisting of
characters.
Characters can be letters of the
alphabet, decimal digits, punctuation
marks, and so on.
They are represented by codes
••
•
Sign bit: b31= 0 for positive numbers
b31= 1 for negative numbers
4 4 5 6 7 4 7 6 5 4
• •
• •
• •
k k k k k k k k k k
2 -4 2 -4 2 -3 2 - 2 - 2 - 4 2 - 2 - 2 -3 2 -
2 1 1 2 4
fraction
𝐵2=
𝐹 𝐵 = −𝑏0 × 𝑏0
0+ 𝑏
−1. ×
𝑏2−1 𝑏−2−2…× 𝑏
−1 + 𝑏 2−−2 + ⋯ +
𝑛−1 × 2− 𝑛−1
𝐹 −1 ≤ 𝐹 ≤ 1 − 2
𝑏− 𝑛−1 − 𝑛−1
where the
is given by range of is
approximately 0 to
For 32-bit format, the range is
implied base of 2
The basic IEEE format is a 32-bit
representation, shown in Figure 6.24a
Based on 2008 version of IEEE (Institute of
Electrical and Electronics Engineers) Standard
754, labelled 754-2008
IEEE Standard for Floating-
Point Numbers..
The leftmost bit represents the sign, 𝑆,
𝐵 = 1. 𝑀 = 1. 𝑏−1𝑏−2 … 𝑏−23
has the value
V 𝐵 = 1+ 𝑏−1 × 2−1 + 𝑏−2 × 2−2 + ⋯ + 𝑏−23 ×
2−23
By convention, when the binary point
IEEE Standard for Floating-
Point Numbers..
𝐸′ = 𝐸 + 127.
exponent field is an unsigned integer
≤
𝐸′ ≤ 2046 for normal values
value ∞ is represented.
[R2]
CPU Organization
Single Accumulator
Result usually goes to the Accumulator
Accumulator has to be saved to
memory quite often
General Register
Registers hold operands thus reduce
memory traffic
Register bookkeeping
Stack
Operands and result are always in the
stack
Basic Instruction Types
Three-Address Instructions
Add R1, R2, R3 R3 ←
R1 + R2
Two-Address Instructions
Add R1, R2 R2 ← R1 + R2
One-Address
Add M Instructions AC ← AC +
Zero-Address [M]
Instructions TOS ← TOS + (TOS –
Add 1)
RISC
LotsInstructions
of registers. Memory is restricted to Load
& Store
Opcode Operand(s) or Address(es)
Basic Instruction Types..
Example: Evaluate (A+B)
(C+D)
Three-Address
1. Add A, B, R1 ; R1 ← [A] +
2. Add C, D, R2 [B]
3. Multipl R1, R2, ; R2 ← [C] +
y [D]
X
; X ← [R1]
[R2]
Basic Instruction Types..
Example: Evaluate (A+B)
(C+D)
Two-Address
1. Move A, R1 ; R1 ← [A]
2. Add B, R1 ; R1 ← [R1] +
[B]
3. Move C, R2 ; R2 ← [C]
4. Add D, R2 ; R2 ← [R2] +
[D]
5. Multiply R1, R2 ; R2 ← [R1]
[R2]
6. Move R2, X ; X ← [R2]
Basic Instruction Types..
Example: Evaluate (A+B)
(C+D)
One-Address
1. Load ; AC ← A
A ; AC ← AC + B
2. Add ; T ← AC
B ; AC ← [C]
3. Store ; AC ← AC +
T [D]
4. Load ; AC ← AC
C [T]
5. Add ; X ← AC
D
Basic Instruction Types..
Example: Evaluate (A+B)
(C+D)
Zero-Address
1. Push ; TOS ← A
A ; TOS ← B
2. Push ; TOS ← (A + B)
B ; TOS ← C
3. Add ; TOS ← D
4. Push ; TOS ← (C + D)
C
; TOS ←
5. Push (C+D)(A+B)
D X
; X ← TOS
6. Add
Basic Instruction
Types..
RISC
Example: Evaluate (A+B)
(C+D)
1. A, R1 ; R1 ← [A]
Load
2. B, R2 ; R2 ← [B]
Load
3. C, R3 ; R3 ← [C]
Load
4. D, R4 ; R4 ← [D]
Load
5. Add R1, R2 ; R2 ← R1 +
R2
6. Add R3, R4 ; R4 ← R3 +
R4
Using Registers
Registers are faster
Shorter instructions
The number of registers is smaller, only
few bits are needed to specify the
register (e.g. 32 registers need 5 bits)
Potential speedup
Minimize the frequency with which
i Move A,R0
Assumptions:
Begin execution here
3-instruction - One memory
i+4 Add B,R0 program
i+8 Move R0,C
segment operand per
instruction
- 32-bit word length
- Memory is byte
A addressable
- Full memory
address can be
B Data for directly specified
the program
in a single-word
Two-phase
instruction
procedure
-Instruction fetch
C
-Instruction
execute
Page 43
Figure 2.8. A program for C +
i Move NUM1,R0
•
•
•
Add NUM n,R0
i + 4n - 4
Move R0,SUM
i + 4n
•
•
•
SUM
NUM1
NUM2
•
•
•
NUM n
Branching LOOP
Determine address of
"Next" number and add
Program "Next" number to R0
loop
Decrement R1
Branch>0 LOOP
Branch target
Move
R0,SUM
Conditional
branch •
•
•
SUM
N n
NUM1
•
•
•
NUM n
Condition Codes
The processor keeps track of
information about the results of
various operations for use by
subsequent conditional branch
instructions.
Accomplished by recording the required
information in individual bits, often called
condition code flags.
These flags are usually grouped
together in a special processor register
called the condition code register or
Condition Codes
Four commonly used flags are
N (negative)
Set to 1 if the result is negative; otherwise, cleared
to 0
Z (zero)
Set to 1 if the result is 0; otherwise, cleared to 0
V (overflow)
Set to 1 if arithmetic overflow occurs; otherwise,
cleared to 0
C (carry)
Set to 1 if a carry-out results from the operation;
otherwise, cleared to 0
Status Bits
Cn-1 A B
Cn ALU
F
V Z N C
Fn-1
Zero Check