2.6: Programas de ensamblador
- Page ID
- 80637
\( \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}\)Los siguientes programas de montaje ilustran cómo se puede utilizar el lenguaje ensamblador definido en este capítulo para implementar algunos programas simples.
2.6.1 Cargar un valor en la CA
Este primer programa carga un valor inmediato de 5
en el registro de $ac
. Después de ejecutar el programa, el valor en los $ac
será 5.
Program 2-1: Loading a value into the $ac from an immediate value .text clac addi 5
Este segundo programa carga el valor de la dirección de memoria correspondiente a la etiqueta var1
en el $ac
. Dado que el valor en la dirección de var1
es 5
, el programa carga el valor 5
en el $ac
.
Program 2-2: Loading a value into the $ac memory using a label .text clac add var1 .data .label var1 .number 5
Este tercer programa agrega el valor en la dirección 0
en el segmento de datos al $ac
. Dado que el valor 5
se ha cargado como el primer valor en el segmento .data
, el valor 5
se carga en el $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 Sumando dos valores inmediatos
Este programa ilustra sumar 2 valores inmediatos juntos en el $ac
. El $ac
se inicializa a 0
, y luego se carga el primer valor en el $ac
desde el valor inmediato de en la instrucción. El valor inmediato en la segunda instrucción se agrega luego al $ac
, y el $ac
contiene el resultado final.
Program 2-4: Adding two immediate values .text clac addi 5 addi 2 # Answer in the $ac is 7
2.6.3 Agregar dos valores de la memoria y almacenar los resultados
Este programa agrega dos valores de memoria en las etiquetas var1
y var2
, los agrega y almacena el resultado de nuevo al valor en label ans
. Este programa también introduce un nuevo constructo, que llamaremos alto. El alto es un conjunto de instrucciones que crea un bucle infinito al final del programa para que el programa no continúe simplemente ejecutando instrucciones noop
hasta que se quede sin memoria. Esto construye establece el $ac
en 0
, y luego se ramifica a la misma declaración una y otra vez. El programa se está ejecutando, pero no está progresando el $pc
ni cambiando el estado de la computadora.
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 Multiplicación por adición iterativa
Este programa múltiplos dos números por iteración. Esto significa que n*m se calcula sumando n a sí mismo m veces, por ejemplo 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