Skip to main content
Engineering LibreTexts

2.5: Designing an Assembly Language

  • Page ID
    27242
  • \( \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}}\)

    When designing an assembly language, a language to manipulate a CPU, there are 3 major concerns:

    1. Transferring data from main memory to memory internal to the CPU (registers or operand stack).
    2. The set of operations that can be performed by the ALU on the data, for example add, subtract, and, shift, etcetera.
    3. A way to provide program control, for example to implement branch (if) and looping (for or while) type structures in a program. Normally control structure will be provided by a branch operation.

    These three major concerns, and how they are addressed in the assembly language, will be discussed in the next sections. The last section of this chapter will give some programs that will illustrate how a program will be written in this assembly language.

    2.5.1 Transferring data from main memory to internal CPU memory

    The amount of memory directly accessible to a programmer on the CPU (e.g. registers) is very limited. In the case of the one-address architecture, only one memory slot, the $ac, is directly useable by a programmer. Therefore programs need to rely on main memory to store program instructions and data.

    To transfer items from data memory to the $ac the instructions add, sub, and stor are used.

    For the add and sub instructions, the second operand of the instruction is the label or memory address of the value to retrieved from memory and sent to the ALU. So for example, to load a value into the $ac from a memory location labelled A the following code would be used.

    clac
    add A
    

    Note that the $ac should always be set to 0 (using the clac) before loading a value into the $ac, or the value stored in the $ac will be the result of adding the value at memory location A with the current value in the $ac.

    For the stor instruction, the second operand is the label or memory address at which to stor the value from the $ac. For example, to store the value in the $ac to memory at the address in label B, the following code would be used.

    stor B
    

    2.5.2 Set of valid ALU operations

    The next consideration is the set of operations which the ALU can perform on the input data. This list depends on the complexity of the ALU. The ALU in this computer is very simple, and so will only support the operations add and sub.

    2.5.3 Program Control (Branching)

    To do any useful program, if and loop constructs must be supported. In the implemented one- address CPU this is accomplished by the Branch-if-equal-zero (beqz) operation. For this operation, if the $ac is 0, the program will branch to the text memory address that is contained in the branch statement. This address can be either a label representing the address, or the numeric value of the branch address. So in the following instruction, the program will branch to the address of label EndLoop if the value in the $ac is 0.

    beqz EndLoop
    

    An unconditional branch statement is often used, but can be simulated by first setting the $ac to 0 before the branch statement. The following instruction implements an unconditional branch.

     clac
     beqz StartLoop
    

    2.5.4 Assembler Instructions

    Based on the criteria of the preceding section, A minimum set of assembler instructions is defined to create useful programs. These instructions are sufficient to create useful programs, and several examples will be shown at the end of this chapter.

    • Either a label or the actual address of the value can be used in this instruction. Thus if the label A refers to the dm address of 5, the following two instructions are the same.
      add A 
      add 5
      
    • addi immediate - Add the immediate value in this instruction to the $ac. The immediate value is an 8-bit integer value between -128...127
      $ac <- $ac + immediate
      

      An example of an addi instruction which adds 15 to the value in the $ac follows.

      addi 15
      
    • beqz [label/address] – The beqz instruction changes the value in the Program Counter ($pc) to the text memory address in the instruction if the value in the $ac is 0. In this CPU, the $pc always specifies the next instruction to execute, so this has the effect of changing the next instruction to execute to the address in the instruction. This is called a program branch, or simply branch.
      $pc <- address IF $ac is 0
      

      An example of the beqz instruction that branches to address 16 if the $ac is 0 follows:

      beqz 16
      
    • clac – The clac instruction sets the $ac to 0. This could be done with a set of stor and sub operations, so instruction is mostly for convenience.
      $ac <- 0
      
    • sub [label/address] - Subtract a value from data memory to the current $ac.
      $ac <- $ac - dm[address]
      

      Either a label or the actual address of the value can be used in this instruction. Thus if the label A refers to the dm address of 5, the following two instructions are the same.

      sub A
      sub 5
      
    • subi immediate - Sub the immediate value in this instruction to the $ac. The immediate value is an 8-bit integer value between -128...128.
      $ac <- $ac - immediate
      

      An example of an subi instruction which adds 15 to the value in the $ac follows.

      subi 15
      
    • stor [label/address] – Store the current value in the $ac to data memory.
      dm[address] <- $ac
      

      Either a label or the actual address of the value can be used in this instruction. Thus if the label A refers to the dm address of 5, the following two instructions are the same.

      stor A
      stor 5
      
    • noop – this statement does nothing. Executing this statement does not change the value of any memory or registers (except the $pc) in the system. It is included so that text memory can be set to 0 and executed without changing the internal state of the computer.

    This page titled 2.5: Designing an Assembly Language 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?