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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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