12.1 Machine Instruction Characteristics

COS2621 - Computer Organisation · 12. Instruction Sets: Characteristics and Functions

12. Instruction Sets: Characteristics and Functions

12.1 Machine Instruction Characteristics

A machine instruction is a binary-coded operation that a computer's CPU (Central Processing Unit) can execute directly. These instructions are fundamental for a computer's operation, enabling it to perform tasks such as arithmetic operations, data movement, and control flow.

Characteristics of Machine Instructions

Machine instructions have several key characteristics:

  • Operation Code (Opcode): This specifies the operation to be performed, such as addition or subtraction.
  • Operands: These are the data items the instruction operates on, which can be registers, memory addresses, or immediate values.
  • Instruction Length: This refers to the number of bits used to encode the instruction. Different architectures may have fixed or variable instruction lengths.
  • Addressing Mode: This specifies how the operands are accessed. Common modes include immediate, direct, indirect, and indexed addressing.

Example of a Machine Instruction

Consider the instruction: SUB R3, R1. In this instruction:

  • Opcode: SUB indicates that the operation is subtraction.
  • Operands: R3 and R1 are the registers involved in the operation.

This instruction subtracts the contents of register R1 from register R3 and stores the result in R3.

Instruction Formats

Machine instructions can be represented in various formats, depending on the architecture. A common format includes:

  • Opcode Field: Specifies the operation.
  • Addressing Mode Field: Indicates the addressing mode used.
  • Operand Fields: Contain the operands.

For example, a hypothetical instruction represented as 0001 0010 0000 0001 could be interpreted as follows:

  • Opcode: 0001 (ADD)
  • Operand 1: 0010 (register R2)
  • Operand 2: 0001 (register R1)

Watch out: Ensure you understand the specific instruction set architecture (ISA) of the machine you are working with, as the opcode and operand representation can vary significantly.

Instruction Execution

When executing an instruction, the CPU fetches it from memory, decodes it to determine the operation and operands, then executes the operation. For instance, executing MUL R2 will multiply the contents of the accumulator by the value in register R2, storing the result back in the accumulator.

Flags and Their Effects

Flags are special bits in a CPU's status register that indicate the outcome of operations. After executing an instruction like ROL R1, 1, the most significant bit (MSB) is moved into the carry flag (CF). This is crucial for subsequent operations, as it can affect the execution flow.

Branching Instructions

Branching instructions alter the flow of execution based on conditions. For example, to branch to a label TEST if the sign flag is set, you would use JS TEST. If you subtract R1 from R2 and want to branch if the result is negative, you would use JN TEST.

Example Problem

Suppose a machine has twelve (12) general-purpose 32-bit registers. How many bits must be reserved in the machine code instruction to address any one of these registers? The formula to calculate the number of bits required is:

n = log2(N), where N is the number of registers.

In this case, N = 12, so:

n = log2(12) ≈ 3.58

Since we cannot have a fraction of a bit, we round up to the nearest whole number, which is 4 bits.

Common Instructions and Their Effects

  • INC: Increments the operand by 1. This instruction does not affect the carry flag; it remains unaffected.
  • NEG: Negates the operand (two's complement).
  • LEA: Loads the effective address of a memory position into a register.
  • LOOP: Decrements the CX register and continues looping if CX is not zero.

Machine instructions consist of opcodes and operands, with characteristics including instruction length and addressing modes. Understanding these concepts is crucial for low-level programming and computer architecture.

Check your understanding

  • What is the purpose of the opcode in a machine instruction?
  • Explain the difference between immediate and direct addressing modes.
  • How many bits are needed to address twelve registers?
  • Which instruction would you use to branch to a label if the sign flag is set?

Test yourself

5 quick questions on this lesson. Pick an answer to see if you're right.

  1. 1. Which of the following is an example of an immediate addressing mode?

  2. 2. A segment may be located in the memory provided it starts on a paragraph boundary.

  3. 3. What is the effect of the INC instruction on the carry flag?

  4. 4. Suppose a machine has fifteen (15) general-purpose 32-bit registers. How many bits must be reserved in the machine code instruction in order to address any one of these fifteen registers?

  5. 5. What is the default operand1 for the IMUL instruction when using a memory operand?