Professional Documents
Culture Documents
Mahnoor Tariq CA ASSIGNMENT
Mahnoor Tariq CA ASSIGNMENT
On the other hand, we might choose to have only a small set of simple instructions. This
may force the programmer to have to combine 2 or more instructions to get something
done, but the advantage is fewer bits required to encode an operation, and a simpler CPU
design.
How Many Operands?
With the number of instructions decided, how many operands will each operation
require?
If the CPU gets most of its data from registers, then we probably want to have some 3-
operand instructions like
ADD R1, R2, R3, i.e. Add R2 and R3, and save the result into R1
so the instruction format needs to have bits set aside to identify each of the three registers.
Other 3-operand instructions include instructions which compare and then divert the PC
to a new instruction, e.g.
BGT R1, R2, 100, i.e. if R1 > R2, branch the CPU to instruction at current PC + 100
Not all instructions have 3 operations. Examples of 2-operand instructions include:
LOAD R3, 4000, i.e. get the value from memory location 4000 and load it into register R3.
SAVE R4, 5000, i.e. write R4's value out to memory location 5000.
SET R6, 23, i.e. set R6 to the literal value 23 (not the value at location 23).
And, of course, a CPU designer may think of 1-operand instructions, such as:
INCR R4, i.e. increment the value in R4. The Java equivalent is R4++.
Later on, we will talk about the various addressing modes which a CPU designer might
wish to use.
Literal Values
Many instructions require literal values.
e.g. in Java when we write for (i=0; i<100; i++), there are two literal values: 0 and 100.
Are we going to be able to find space in each instruction to put in literal values?
If so, that will be great, but it will be wasted space if programs don't have many literal
values.
If we can't find space, then each time there is a literal, it is going to have to be stored in a
register, or we are going to have to go out to memory to fetch the literal value.
Instruction Format
Each different instruction has to be encoded differently: operation, operands, literal
values, where to place the result, size of the data being operated on etc.
What is the instruction format going to look like?
Can we make each instruction the same size, or are some instructions going to be
different sizes?
Is there going to be a single format, which makes the decoding in silicon easy, or are we
going to have several different instruction types, each with a different format?
A Hypothetical Example
Before we go any further, let's make the above concrete by doing some ISA design.
Let's design an ISA with 8 registers, a 16-bit data bus and word size, and a 24-bit address
bus: quite suitable for an embedded CPU, e.g. in a microwave or engine control system.
Let's also have 3-operand instructions where the operands are all registers:
Rdest=Rsrc1 OP Rsrc2
We need 3 bits to encode each register's number, so that's 9 bits out of 16 used up,
leaving 7 bits.
Let's use 1 bit to encode the size of the data being manipulated: 8-bit byte or 16-bit int.
That leaves 6 bits.
We could go for a single instruction format:
but now there's no way to copy values between the registers and the main memory, nor is
there any way to put literal values into an instruction.
Let's add a second instruction format, still 16-bits long, but which contains a literal value:
This allows us to do things like SET R0, 26, where the 26 is stored in the 8-bit literal
section.
But we still don't have a way to access locations in memory, so there is one final
instruction format:
This instruction format is 2 words long, and the memory address is specified as a 24-bit
value. This allows us to do things like LOAD R0, 4000 and SAVE R4, 5000.
We now have:
o a 1-word instruction format with 25=32 possible operations on 3 register operands,
o a 1-word instruction format with 22=4 possible operations on one register and an
8-bit literal value, and
o a 2-word instruction format with 22=4 possible operations on one register and a
24-bit memory location
Decision Making
Up to now, we haven't considered how we are going to modify the value in the PC, so
that it can deviate from the normal "next instruction" flow of execution.
We need a way to skip over instructions, based on a decision, so as to implement IF
statements.
We also need to branch backward, based on a decision, so as to implement loop
constructs.
We need to jump to the start of a function, and also know how to return back to where we
left.
All of the above change the default PC behaviour: move to the address of the next
instruction.
What decisions are we going to provide? ==, !=, <, <=, >, >= ?
Some of these we could leave out, e.g. if (R3 < R4) is the same as if (R4 > R3).
There are several choices here. We could design an instruction format which encodes:
o two operands to be compared,
o what type of comparison to make,
For example:
An alternative is to have a status register with bit flags that are set on/off based on the
result of the last instruction, and other instructions which branch depending on what bits
in the status register are set.
For example, let's have a last result was zero (Z) flag, and a last result was negative (N)
flag. Other common flags hold the ALU's Carry and Overflow output.
We can now have several branching instructions:
o branch to instruction X if Z==1, i.e. the last instruction's result was 0.
o branch to instruction X if Z==0, i.e. the last instruction's result was not 0.
o branch to instruction X if N==1, i.e. the last instruction's result was negative.
o branch to instruction X if N==0, i.e. the last instruction's result was positive.
o branch to instruction X if N==0 and Z==0, i.e. the last instruction's result was
positive and not zero, i.e. >0.
Given the above, we can now make these comparisons just with a subtraction operation:
Let's now turn our attention to two of the most popular ISA design philosophies: RISC
and CISC.
They are nearly diametrically opposed in term of how to achieve their goals, but the goal
is the same: to design an ISA which which has enough functionality so that programmers
can write programs for it.