Skip to main content
Engineering LibreTexts

2.6: Assembler Programs

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

    The following assembly programs illustrate how the assembly language defined in this chapter can be used to implement some simple programs.

    2.6.1 Loading a value into the AC

    This first program loads an immediate value of 5 into the $ac register. After running the program, the value in the $ac will be 5.

    Program 2-1: Loading a value into the $ac from an immediate value
    
    .text
    clac
    addi 5
    

    This second program loads the value from the memory address corresponding to the label var1 into the $ac. Since the value at the address of var1 is 5, the program loads the value 5 into the $ac.

    Program 2-2: Loading a value into the $ac memory using a label
    
    .text
    clac
    add var1 
    .data 
    .label var1
    .number 5
    

    This third program adds the value at address 0 in the data segment to the $ac. Since the value 5 has been loaded as the first value in the .data segment, the value 5 is loaded to the $ac.

    Program 2-3: Loading a value into the $ac from memory using a reference
    
    .text
    clac
    add 0
    .data
    .number 5
    

    2.6.2 Adding Two Immediate values

    This program illustrates adding 2 immediate values together in the $ac. The $ac is initialize to 0, and then the first value is loaded in the $ac from the immediate value of in the instruction. The immediate value in the second instruction is then added to the $ac, and the $ac contains the final result.

    Program 2-4: Adding two immediate values
    
    .text
    clac
    addi 5
    addi 2
    # Answer in the $ac is 7
    

    2.6.3 Adding two values from memory, and storing the results

    This program adds two values from memory at labels var1 and var2, adds them, and stores the result back to the value at label ans. This program also introduces a new construct, which we will call halt. The halt is a set of instructions that creates an infinite loop at the end of the program so that the program does not simply continue to execute noop instructions until it runs out of memory. This constructs sets the $ac to 0, and then branches to the same statement over and over again. The program is running, but it is not progressing the $pc or changing the state of the computer.

    Program 2-5: Adding two memory values and storing back to memory
    
    .text
    clac
    add var1
    add var2
    stor ans
    .label halt
        clac
        beqz halt
    # Answer is in data memory at address 0.
    .data
    .label ans
        .number 0
    .label var1
        .number 5    
    .label var2
        .number 2    
    

    2.6.4 Multiplication by iterative addition

    This program multiples two numbers by iteration. This means than n*m is calculated by adding n to itself m times, e.g. n + n + n... etc.

    Program 2-6: Multiplication using iterative addition
    
    .text
    # Begin the multiplication loop.
    .label startLoop
    
    # When the program is assembled the multiplier and multiplicand are
    # initialized # in memory. The counter is initialized to 0
    # and incremented by 1 each time through the loop. When the
    # counter == multiplier, # the value of multiplier-counter = 0,
    # and the beqz instruction branches to the end of the loop.
    clac
    add multiplier
    sub counter
    beqz endLoop  # Go to end when done
    
    # calculate product. The product is initially zero, but each 
    # time through the loop the product
    # is augmented by the value of the multiplicand. The product is 
    # then stored back to memory for use on the next pass.
    clac
    add product
    add multiplicand
    stor product
    
    # The counter is incremented and the program branches 
    # back to the beginning of the loop for the next pass.
    clac
    add counter
    addi 1
    stor counter
    clac
    beqz startLoop
    
    # When counter == multiplier, the program has completed.
    # The answer is at the memory location of the label product. 
    .label endLoop
    clac
        beqz endLoop
    # result is in ans (data address 0)
    
    .data
    .label multiplicand
        .number 5
    .label multiplier
        .number 4
    .label counter
        .number 0
    .label product
        .number 0            
    

    This page titled 2.6: Assembler Programs 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?