Saltar al contenido principal
LibreTexts Español

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}}\)

    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            
    

    This page titled 2.6: Programas de ensamblador 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.