Information that must be found in a computer instruction
 

Question: What information does a computer instruction have to contain ?

From the last webpage, an instruction must provide the following information:

  1. What operation the computer must perform

    Examples:

      • move
      • add, subtract, multiple, etc
      • branch always, branch if less than, branch if equal, etc

  2. Which (source) register(s) to use in the operation

  3. Which (destination) register to use to store the result of the operation

How to encode computer instructions
 

The required information needed by a computer instruction are stored (= represented) together in a sequence of bytes

Example:

 Format of one (= 1) computer instruction:

                        4 bytes (= 32 bits)
   <------------------------------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   <-----------> <-------------> <---------------> <-------------->
     opcode           dest            source1               source2
   

Encoding the operation
 

To encode (=represent) the operation, each computer operation is represented by a unqiue operation code (it's called opcode for short)

Example:

    Operation        Opcode
   -------------    --------
       Move          0000000
       Add           0000001
       Subtract      0000010
       Multiply      0000011
       Divide        0000100
        ...            ...     (and so on)     
   

Encoding the operation - Example
 

For example: the binary code 0000001 represents an add operation:

                        4 bytes (= 32 bits)
   <------------------------------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  |0|0|0|0|0|0|1| | | | | | | | | | | | | | | | | | | | | | | | | |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   <-----------> <-------------> <---------------> <-------------->
     opcode           dest            source1               source2
   
   

 

Note: the source1, source2 and dest fields will encode the source and destination register operands

Note: each computer manufacturer has its own way to encode instructions !!!

Encoding the operation - Example
 

This represents an add operation that uses the value in the register #1 as the first source operand:

                        4 bytes (= 32 bits)
   <------------------------------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  |0|0|0|0|0|0|1| | | | | | | | |0|0|0|0|0|0|0|0|1| | | | | | | | |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   <-----------> <-------------> <---------------> <-------------->
     opcode           dest            source1               source2
   
   

 

Note: the source1, source2 and dest fields will encode the source and destination register operands

Note: each computer manufacturer has its own way to encode instructions !!!

Encoding the operation - Example
 

This represents an add operation that uses the values in register #1 and #3 as the source operands:

                        4 bytes (= 32 bits)
   <------------------------------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  |0|0|0|0|0|0|1| | | | | | | | |0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|1|1|
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   <-----------> <-------------> <---------------> <-------------->
     opcode           dest            source1               source2
   
   

 

Note: the source1, source2 and dest fields will encode the source and destination register operands

Note: each computer manufacturer has its own way to encode instructions !!!

Encoding the operation - Example
 

This represents the instruction add the values in regs #1 and #3 and store result in reg #4:

                        4 bytes (= 32 bits)
   <------------------------------------------------------------->
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  |0|0|0|0|0|0|1|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0|1|0|0|0|0|0|0|1|1|
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   <-----------> <-------------> <---------------> <-------------->
     opcode           dest            source1               source2
   
   

 

In assembler programming,, this instruction may be represented by the nmemonic:   add R4, R1, R3

Note: each computer manufacturer has its own way to define assembler nmemonics too !!!