INSTRUCTION FORMATS

 

We know that machine instruction has an opcode and zero or more operands.  Encoding an instruction set can be done in a variety of ways.

 

Architectures are differentiated from one another by the number of bits allowed per instruction (16, 32 and 64 are the most common), by the number of operands allowed per instruction, and by the types of instructions and data each can process.  More specifically, instruction sets are differentiated by the following features:

 

  • Operand storage in the CPU
  • Number of explicit operands per instruction (0, 1, 2 and 3 being the most common)
  • Operand location (instructions can be classified as register-to-register, register-to-memory, which simply refer to the combinations of operands allowed per instruction.
  • Operations (including not only types of operations but also which instruction can access memory and which cannot)
  • Type and size of operands (operands can be addresses, numbers, or even characters)

 

DESIGN DECISIONS FOR INSTRUCTION SETS

 

When computer architecture is in the design phase, the instruction set format must be determined before many other decisions can be made.  Selecting this format is often quite difficult because the instruction set must match the architecture, and the architecture, if well designed, could last for decades.  Decisions made during the design phase have long-lasting ramifications.

 

Instruction set architectures are measured by several different factors, including:

  1. The amount of space a program requires
  2. the complexity of the instruction set, in terms of the amount of decoding necessary to execute an instruction, and the complexity of the tasks performed by the instructions
  3. The length of the instructions and
  4. The total number of instructions.

 

Things to consider when designing an instruction set:

 

  • Short instructions are typically better because they take up less space in memory and can be fetched quickly.  However, this limits the number of instruction, because there must be enough bits in the instruction to specify the number of instruction we need.  Shorter instructions also have tighter limits on the size and number of operands.

 

  • Instructions of a fixed length are easier to decode but waste space.

 

  • Memory organization affects instruction format.  If memory has, for example,  16- or 32-bit words and is not byte addressable, it is difficult to access a single character. For this reason, even machines that have 16-, 32-, or 64-bit words are often byte addressable, meaning every byte has a unique address even though words are longer than a byte.

 

  • A fixed length instruction does not necessarily imply a fixed number of operands. 

 

  • How many registers should the architecture contain and how should these registers be organised?  How should operands be stored in the CPU?

 

NUMBER OF OPERANDS AND INSTRUCTION LENGTH

 

The traditional method for describing computer architecture is to specify the maximum number of operands, or addresses, contained in each instruction.  This has a direct impact on the length of the instruction.

 

Instructions on current architectures can be formatted in two ways:

 

Fixed length – Wastes space but is fast and results in better performance when instruction-level pipelining is used.

 

Variable length – More complex to decode but saves on storage space.

 

Typically, the real-life compromise involves using two or three instruction lengths, which provides bit patterns that are easily distinguishable and simple to decode.  The instruction length must also be compared to the word length on the machine.  If the instruction length is exactly equal to the word length, the instructions align perfectly when stored in main memory.  Instructions always need to be word aligned for addressing reasons.  Therefore, instructions that are half, quarter, double, or triple the actual word size can waste space.

 

Variable length instructions are clearly not the same size and need to be aligned, resulting in loss of space as well.

 

The most common instruction formats include zero, one, two, or three operands.  Arithmetic and logic operations typically have two operands, but can be executed with one operand, if the accumulator is understood.  We can extend this idea to three operands if we consider the final destination as an operand.

 

Some common instruction formats are as follows:

 

  • OPCODE only (zero addresses)
  • OPCODE + 1 Address (usually a memory address)
  • OPCODE + 2 Addresses (usually registers, or one register and one memory address)
  • OPCODE + 3 Addresses (usually registers, or combinations of registers and memory)

 

 

 

All architectures have a limit on the maximum number of operands allowed per instruction.  One-, two- or even three-operand instructions are reasonably easy to understand; an entire ISA (Instruction Set Architecture) built on zero operand instructions can, at first, be somewhat confusing.

 

Machine instructions that have no operands must use a stack (last-in-first-out data structure, where all insertions and deletions are made from the top) to perform those operations that logically require one or two operands (such as ADD).  Instead of using general purpose registers, a stack-based architecture stores the operands on the top of the stack, making the top element accessible to the CPU.  (Note that one of the most important data structures in machine architectures is the stack. Not only does this structure provide an efficient means of storing intermediate data values during complex calculations, but it also provides an efficient method for passing parameters during procedure calls as well as a means to save local block structure and define the scope of variables and subroutines).

 

In architectures based on stacks, most instructions consist of opcodes only; however, there are special instructions (those that add elements to and remove elements from the stack) that have just one operand.  Stack architectures need a push instruction and a pop instruction, each of which is allowed one operand.  Push X places the data value found in memory location X onto the stack; Pop X removes the top element on the stack and stores it at a location X.  Only certain instructions are allowed to access memory; all others must use the stack for any operands required during execution.

 

For operations requiring two operands, the top two elements of the stack are used.  For example, if we execute an Add instruction, the CPU adds the top two elements of the stack, popping them both and then pushing the sum onto the top of the stack.  For non-commutative operations such as subtraction, the top stack element is subtracted from the next-to-the-top element, both are popped, and the result is pushed on top of the stack.

 

Example:

 

Suppose we wish to evaluate the following expression:

Z = (X * Y) + (W * U)

 

Typically, when three operands are allowed, at least one operand must be in a register, and the first operand is normally the destination.  Using three-address instructions, the code to evaluate the expression for Z is written as follows:

 

MULT             R1, X, Y

MULT             R2, W, U

ADD               Z, R2, R1

 

When using two-address instructions, normally one address specifies a register (two-address instructions seldom allow for both operands to be memory addresses).  The other operand could either be a register or a memory address.  Using two-address instructions, our code becomes:

 

            LOAD             R1, X

            MULT             R1, Y

            LOAD             R2, W

            MULT             R2, U

ADD               R1, R2

STORE           Z, R1

 

 

Note that it is important to know whether the first operand is the source or the destination.  In the above instructions, we assume it is the destination. 

 

Using one-address instructions, we must assume a register (normally an accumulator) is implied as the destination for the result of the instruction.  To evaluate Z, our code now becomes:

 

            LOAD             X

            MULT             Y

            STORE           temp

            LOAD             W

            MULT             U

ADD               temp

STORE           Z

 

Note that we reduce the number of operands allowed per instruction, the number of instructions required to execute the desired code increases.  This is an example of a typical space/time trade off in architecture design – shorter instructions but longer programs. 

 

INSTRUCTION TYPES 

Most computer instructions operate on data; however, there are some that do not.  Computer manufacturers regularly group instructions into the following categories: 

  • Data movement
  • Arithmetic
  • Boolean
  • Bit Manipulation (shift and rotate)
  • Input/Output I/O 

We will definitely need to know bit/data manipulation control and input/output, although the others will be explained.

 

·         Data Movement

 

Data movement instructions are the most frequently used instructions.  Data is moved from memory into registers, from registers to registers, and from registers to memory, and many machines provide instructions depending on the source and destination.  For example, there may be a mover instruction that always requires two register operands, whereas a move instruction that allows one register and one memory operand.

 

·         Arithmetic Operations

Arithmetic operations include those instructions that use integers and floating point numbers.  Many instruction sets provide different arithmetic instructions for various data sizes.  As with the data movement instructions, there are sometimes different instructions for providing various combinations of register and memory accesses in different addressing modes.  Instructions may exist for arithmetic operations on both signed and unsigned numbers, as well as for operands in different bases.  Many times, operands are implied in arithmetic instructions.  For example, a multiply instruction may assume the multiplicand is resident in a specific register so it need not be explicitly given in the instruction.  Arithmetic instructions include ADD, SUBTRACT, MULTIPLY, DIVIDE, INCREMENT, DECREMENT and NEGATE (to change the sign).

 

·         Boolean Logic Instructions

Boolean logic instructions perform Boolean operations, much in the same way that arithmetic operations work.  These instructions allow bits to be set, cleared, and complemented.  Logic operations are commonly used t control I/O devices.  There are typical instructions for performing AND, NOT, OR, XOR, TEST, and COMPARE.

 

·         Bit/Data manipulation Instructions

Bit Manipulation instructions are used for setting and resetting individual bits (or sometimes groups of bits) within a given word.  These include arithmetic and logical SHIFT instructions and ROTATE instructions, each to the left or the right. 

 

Shift Instruction

 

Logical Shift instructions simply shift bits to either the left or the right by a specified number of bits, shifting in zeros at the opposite end.  For example, if we have an 8-bit register containing the value 11110000, and we perform a logical shift left by one bit, the result is 11100000.  If our register contains 11110000 and we perform a logical shift right by one bit, the result is 01111000.

 

Arithmetic shift instructions, commonly used to multiply or divide by 2, treat data as signed two’s complement numbers, and do not shift the leftmost bit, since this represents the sight of the number.  On a right arithmetic shift, the sign bit is replicated into the bit position(s) to its right:  if the number is positive, the left-most bits are filled by zeros; if the number is negative, the leftmost bits are filled by ones. 

 

A right arithmetic shift is equivalent to division by 2.  For example, if our value is 00001110 (+14) and we perform an arithmetic shift right by one bit, the result is 00000111 (+7).  If the value is negative, such as 11111110 (-2), the result is 11111111 (-1).  On a left arithmetic shift, bits are shifted left, zeros are shifted in, but the sign bit does not participate in the shifting.  An arithmetic shift left is equivalent to multiplication by 2.  For example, if our register contains 00000011 (+3), and we perform an arithmetic shift left, one bit, the result is 00000110 (+6).  If the register contains a negative number such as 11111111 (-1), performing an arithmetic shift left by one bit yields 11111110 (-2). 

 

If the last bit shifted out the (excluding he sign bit) does not match the sign, overflow and underflow occurs. For example, if the number is 10111111 (-65) and we do an arithmetic shift left by one bit, the result is 11111110 (-2), but the bit that was “shifted out” is a zero and does not match the sign; hence w have a overflow.

 

Rotate Instruction

 

Rotate instructions are simply shift instructions that shift in the bits that are shifted out – a circular shift basically.  For example, on a rotate left one bit, the leftmost bit is shifted out and rotated around to become the rightmost bit.  If the value 00001111 is rotated left by one bit, we have 00011110.  If 00001111 is rotated right by one bit, we have 10000111.  When we rotating we do not worry about the sign bit.

 

·         Input/Output Instructions

 

I/O instructions vary greatly from architecture to architecture.  The input (or read) instruction transfers data from a device or port to either memory or a specific register.  The output (or write) instruction transfers data from a register or memory to a specific port or device.  There may be separate I/O instructions for numeric data and character data.  Generally, character and string data use some type of block I/O instruction, automating the input of a string. 

 

 

 

 

Make a Free Website with Yola.