1.
Machine Instruction Characteristics
Elements of a Machine Instruction
The operation of the processor is
determined by the instructions it executes,
referred to as machine instructions or
computer instructions.
The collection of different instructions that
the processor can execute is referred to as
the processor’s instruction set.
Each instruction must contain the
information required by the processor for
execution.
1
What are the typical elements of a machine instruction?
These elements are as follows;
Operation code , Source operand reference, Result operand reference
and Next instruction reference
■ Operation code: Specifies the operation to be performed (e.g.,
ADD, I/O). The operation is specified by a binary code, known as
the operation code, or opcode.
■ Source operand reference: The operation may involve one or
more source operands, that is, operands that are inputs for the
operation.
2
Elements of a Machine Instruction
Figure shows the steps involved in instruction execution
and defines the elements of a machine instruction.
Instruction Cycle State Diagram
3
Instruction Address Calculation − The address of the next instruction is
computed. A permanent number is inserted to the address of the earlier
instruction.
Instruction Fetch − The instruction is read from its specific memory
location to the processor.
Instruction Operation Decoding − The instruction is interpreted and the
type of operation to be implemented and the operand(s) to be used are
decided.
Operand Address Calculation − The address of the operand is evaluated
if it has a reference to an operand in memory or is applicable through the
Input/Output.
Operand Fetch − The operand is read from the memory or the I/O.
Data Operation − The actual operation that the instruction contains is
executed.
Store Operands − It can store the result acquired in the memory or
transfer it to the I/O.
4
Instruction Representation
Within the computer, each instruction is
represented by a sequence of bits.
The instruction is divided into fields,
corresponding to the constituent elements
of the instruction. A simple example of an
instruction format is shown in the figure.
A simple instruction format
5
Instruction Representation
In machine code each instruction has a
unique bit pattern.
For human consumption (programmers) a
symbolic representation of machine
instructions is used.
6
Instruction Representation
Opcodes are represented by abbreviations,
called mnemonics, that indicate the
operation.
Common examples include:
◦ ADD - Add
◦ SUB - Subtract
◦ MUL - Multiply
◦ DIV - Divide
◦ LOAD - Load data from memory
◦ STOR - Store data to memory
7
Instruction Representation
Operands are also represented symbolically.
For example, the instruction ADD R,Y
(R R + Y ) may mean add the value
contained in data location Y to the contents
of register R.
In this example, Y refers to the address of a
location in memory, and R refers to a
particular register.
Note that the operation is performed on the
contents of a location, not on its address.
8
Instruction Types
Consider a high-level language instruction that could
be expressed X=X+Y.
This statement instructs the computer to add the value
stored in Y to the value stored in X and put the result in
X.
Let us assume that the variables X and Y correspond to
memory locations 513 and 514. This operation could
be accomplished with three instructions:
1. Load a register with the contents of memory location 513
2. Add the contents of memory location 514 to the register
3. Store the contents of the register in memory location 513
9
List and briefly discuss the four
categories of instruction types
Thus, the set of machine instructions must be
sufficient to express any of the instructions
from a high-level language.
With this in mind we can categorize
instruction types as follows:
◦ Data processing: arithmetic and logic instructions
◦ Data storage (main memory): data in/out to/from
registers, memory locations
◦ Data movement (I/O): I/O instructions
◦ Program flow control: test and branch instructions
10
Instruction Types
Arithmetic instructions provide computational capabilities
for processing numeric data.
Logic (Boolean) instructions operate on the bits of a word.
These operations are performed primarily on data in
processor registers.
I/O instructions are needed to transfer programs and data
into memory and the results of computations back out to
the user.
Test instructions are used to test the value of a data word
or the status of a computation.
Branch instructions are then used to branch to a
different set of instructions depending on the decision
made.
11
Number of Addresses
One of the traditional ways of describing
processor architecture is in terms of the
number of addresses contained in each
instruction.
What is the maximum number of
addresses one might need in an
instruction?
All arithmetic and logic operations are
either unary (one source operand) or
binary (two source operands).
12
Number of Addresses
3 addresses format
◦ Operand 1, Operand 2, Result
◦ a = b + c;
With three-address instrcutions, each
instruction specifies two source operand
locations and a destination operand
location.
This format is not common because it
requires a relatively long instruction format to
hold the three address references.
13
Write a program to compute by using three
address instructions.
14
Number of Addresses
2 addresses format
One address doubles as operand and result
◦a = a + b
The two-address format reduces the space
requirement and the length of instruction
but requires some extra work.To avoid altering the
value of an operand, a MOVE instruction is used
to move one of the values to a result or temporary
location before performing the operation.
15
Write a program to compute by using two
address instructions.
16
Number of Addresses
Simpler yet is the one-address instruction. For
this to work, a second address must be
implicit.
This was common in earlier machines, with
the implied address being a processor
register known as the accumulator (AC).
The accumulator contains one of the
operands and is used to store the result.
17
Write a program to compute by using one
address instructions.
18
Number of Addresses
In fact, it is possible to do with zero
addresses for some instructions.
Zero-address instructions are applicable to a
special memory organization, called a stack.
A stack is a last-in-first-out set of locations.
The stack is in a known location and, often, at
least the top two elements are in
processor registers.
19
Number of Addresses
Thus, zero-address instructions would
reference the top two stack elements.
For example, c = a + b can be implemented
by zero addresses as:
◦ push a
◦ push b
◦ add
◦ pop c
20
Write a program to compute by using zero
address instructions.
PUSH A
PUSH B
SUB
PUSH C
PUSH D
PUSH E
MUL
ADD
DIV
POP Y
21
Table below summarizes the interpretations to be placed on
instructions with zero, one, two, or three addresses. In each case in
the table, it is assumed that the address of the next instruction is
implicit, and that one operation with two source operands and one
result operand is to be performed
22
Instruction Set Design
The design of an instruction set is very
complex because it affects so many aspects
of the computer system.
The instruction set defines many of the
functions performed by the processor and
thus has a significant effect on the
implementation of the processor.
The instruction set is the programmer’s
means of controlling the processor.
23
List and briefly explain five important
instruction set design issues
Instruction set design issues includes:
◦ Operation repertoire
How many operations ? What can they do?
How complex are they?
◦ Data types
◦ Instruction formats
Length of op code field
Number of addresses
◦ Registers
Number of CPU registers available and which operations
can be performed on which registers?
◦ Addressing modes by which the address of an operand is
specified.
24
2. Types of Operands
What types of operands are typical in machine instruction sets?
The most important general categories of
data are:
◦ Addresses (In many cases, some calculation must
be performed on the operand reference in an
instruction to determine the main or virtual
memory address.)
◦ Numbers (Integer/floating point)
◦ Characters (ASCII etc.)
◦ Logical Data (Bits or flags)
25
Types of Operations
The number of different opcodes varies widely from
machine to machine. However, the same general
types of operations are found on all machines.
A useful and typical categorization is the following:
◦ Data Transfer
◦ Arithmetic
◦ Logical
◦ Conversion
◦ I/O
◦ System Control
◦ Transfer of Control
26
Data Transfer
Transfer data from one location to another.
The data transfer instruction must specify the
location of the source and destination
operands.
Each location could be memory, a register, or
the top of the stack.
If memory is involved:
◦ Determine memory address
◦ Perform virtual-to-actual-memory address
transformation
◦ Check cache
◦ Initiate memory read/write
27
28
29
30
Data Transfer
The data transfer instruction must specify several things
• The location of the source and destination operands must be
specified. Each location could be memory, a register, or the top
of the stack.
• The length of data to be transferred must be indicated.
• All instructions with operands, the mode of addressing for
each operand must be specified.
31
In terms of processor action, data transfer operations are
perhaps the simplest type.
• If both source and destination are registers, then the
processor simply causes data to be transferred from one
register to another; this is an operation internal to the
processor.
• If one or both operands are in memory, then the processor
must perform some or all of the following actions:
1. Calculate the memory address, based on the address
mode.
2. If the address refers to virtual memory, translate from
virtual to real memory address.
3. Determine whether the addressed item is in cache.
4. If not, issue a command to the memory module.
32
Arithmetic
May involve data transfer, before and/or
after.
Perform function in ALU
◦ Add, Subtract, Multiply, Divide
◦ May include
Increment (a++)
Decrement (a--)
Absolute (|a|)
Negate (-a)
Set flags
33
Logical
Bitwise operations
◦ AND, OR, NOT, XOR, EQUALS
34
Logical and Arithmetical
Operations
Most machines provide a variety of shift
and rotate functions
35
With a logical shift, the bits of a word are shifted left or right. On
one end, the bit shifted out is lost. On the other end, a 0 is shifted in.
A right arithmetic shift corresponds to a division by 2, with truncation
for odd numbers. An arithmetic left shift corresponds to a
multiplication by 2
Rotate, or cyclic shift, operations preserve all of the bits being
operated on. One use of a rotate is to bring each bit
successively into the leftmost bit, where it can be identified by
testing the sign of the data
36
Conversion
Conversion instructions are those that
change the format or operate on the format
of data.
An example is converting from decimal to
binary.
May involve special logic to perform
conversion.
37
Input/Output
Issue command to I/O module
There are a variety of approaches including
programmed I/O, interrupt driven I/O, DMA,
and the use of an I/O processor.
38
System Control
System control instructions are those that
can be executed only while the processor is
in a certain privileged state or is executing
a program in a special privileged area of
memory.
Typically, these instructions are reserved for
the use of the operating system.
◦ For example,.
1. NOP (No operation) 2. HLT (Halt) 3. DI (Disable
interrupts) 4. EI (Enable interrupts)
39
Transfer of Control
A significant fraction of the instructions in any
program have as their function changing the
sequence of instruction execution.
For these instructions, the operation performed by
the processor is to update the program counter
to contain the address of some instruction in
memory.
The most common transfer of control operations
found in instruction sets are branch, skip and
procedure call.
40
Branch instructions :
A branch instruction, also called a jump instruction, has as one of its
operands the address of the next instruction to be executed.
Most often, the instruction is a conditional branch instruction.
The branch is made (update program counter to equal address
specified in operand) only if a certain condition is met.
Otherwise, the next instruction in sequence is executed
A branch instruction in which the branch is always taken is an
unconditional branch.
41
On such a machine, there could be four different conditional
branch instructions:
BRP X : Branch to location X if result is positive.
BRN X : Branch to location X if result is negative.
BRZ X : Branch to location X if result is zero.
BRO X : Branch to location X if overflow occurs
Another approach that can be used with a three-address
instruction format is to perform a comparison and specify a
branch in the same instruction.
For example,
BRE R1, R2, X
(Branch to X if contents of R1 = contents of R2)
42
Branch Instruction
43
Skip Instruction
Another form of transfer- of- control instruction is the
skip
instruction. The skip instruction includes an implied
address.
A typical example is the increment-and-skip-if-zero (ISZ)
instruction.
44
Procedure Calls
A procedure is a self contained computer program that is
incorporated into a larger program.
At any point in the program the procedure may be invoked, or
called
Two principal reasons for the use of procedures are economy
and modularity.
The procedure mechanism involves two basic instructions: a call
instruction that branches from the present location to the
procedure, and a return instruction that returns from the
procedure to the place from which it was called. Both of these are
forms of branching instructions.
45
46
Use of stack
When the processor executes a call, it places the return
address on the stack. When it executes the return, it uses
the address on the stack.
47