Skip to main content
Engineering LibreTexts

3.1: Overview of the Machine Code Instruction Format

  • Page ID
    27252
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    All machine code instructions for our computer will consist of two 4-bit segments, and one 8-bit segment, as shown below.

    Figure 3-1: 16-bit machine instruction format

    Screen Shot 2020-07-02 at 8.15.59 PM.png

    The first 4-bit segment will represent the type of operation. The possible types of operations are the following:

    • 1 –This opcode represents an immediate operation which uses the ALU to produce a result. This instruction consists of the 4-bit opcode, a 4-bit ALU option (ALUopt) to tell the ALU what operation to execute, and an 8 bit data immediate value for an operand. As implemented the ALU only executes 2 operations, 0x0 is add and 0x1 is subtract, though the exercises at the end of the text add more operations. A maximum of 16 operations can be implemented in the CPU.

      Examples of translating these assembly instructions into machine code follow.

      The instruction:

      addi 2
      

      translates into the following machine code:

      0x1002
      

      The instruction:

      subi 15
      

      translates to the following machine code

      0x110f
      
    • 2 – This opcode represents a memory address operation which uses the ALU to produce a result. This instruction consists of the 4-bit opcode, a 4-bit ALUopt to tell the ALU what operation to execute, and an 8 bit data memory address for an operand. As implemented the ALU only executes 2 operations, 0x0 is add and 0x1 is subtract, though the exercises at the end of the text add more operations. A maximum of 16 operations can be implemented in the CPU.

      Examples of translating these assembly instructions into machine code follow.

      The instruction:

      add 2

      translates into the following machine code:

      0x2002
      

      The instruction

      sub 15
      

      translates into the following machine code

      0x210f
      

      Note that during assembly process labels in assembly code are translated into addresses, so labels will never appear in machine code.

    • 3 – This opcode executes the clac operation (e.g. it sets the $ac to 0). In this instruction all of the subsequent bits after the 0x3 are ignored, so they can contain any value. By convention, the extra bits should always be set to 0.

      For example, the following assembly instruction

      clac
      

      translates to

      0x3000
      
    • 4 – This opcode executes the stor operation. In this instruction the 4 bit ALU opt is not used, and should be set to 0. The address value is the address at which to store the value in the $ac. For example, the following instruction
      stor 15
      

      translates to:

      0x400f
      
    • 0x5 – The opcode executes the beqz operation. In this instruction the 4 bit ALU operation is not used, and should be set to blank. The result of this operation is the $pc is set to the address value if the $ac is zero. Setting the value for the $pc causes the program to branch to that address.

      For example, the following instruction

      beqz 40
      

      translates to:

      0x5028
      

    This page titled 3.1: Overview of the Machine Code Instruction Format is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?