GNU/Linux xterm-256color bash 177 views

// ****************************************************************************
// * Lenguajes de Interfaz en TECNM Campus ITT
// * Nombre del archivo: practica.s
// * Descripción: Programa que realiza 10 iteraciones.
// * Autor: [America Fernanda Nevarez de la Cruz]
// * Fecha: [2025-04-08]
// * Plataforma: Raspberry Pi OS (64 bits)
// * Demostración:  [https://asciinema.org/a/opdU6fX0KeOyVSVuD7h78Iybg]
// ****************************************************************************

// ****************************************************************************
// * Codigo equivalente en C#
// *using System;
// *
// *class CicloIteraciones
// *{
// *    static void Main()
// *    {
// *        Console.WriteLine("Ciclo de 10 iteraciones:");
// *        
// *        for(int i = 1; i <= 10; i++)
// *        {
// *            Console.WriteLine(i);
// *        }
// *        
// *        Console.WriteLine();  // Salto de línea final
// *    }
// *}
// ****************************************************************************

.section .data
msg:        .ascii "Ciclo de 10 iteraciones:\n"
len_msg = . - msg

newline:    .ascii "\n"

.section .bss
num_buffer: .skip 16

.section .text
.global _start

_start:
    // Imprimir mensaje
    ldr x0, =msg
    mov x1, #len_msg
    bl print_msg

    mov x20, #1        // Contador i = 1

loop:
    cmp x20, #11       // ¿i > 10?
    bge end_program

    mov x0, x20        // Copiar i en x0 para imprimir
    bl print_int

    add x20, x20, #1   // i++
    b loop

end_program:
    // Salto de línea final
    ldr x1, =newline
    mov x2, #1
    mov x0, #1
    mov x8, #64
    svc #0

    // Salir
    mov x8, #93
    mov x0, #0
    svc #0

// ---------------- Funciones ----------------

print_msg:
    mov x2, x1      // longitud
    mov x1, x0      // puntero a string
    mov x0, #1      // STDOUT
    mov x8, #64     // syscall write
    svc #0
    ret

print_int:
    // Convierte el entero x0 a string y lo imprime
    ldr x1, =num_buffer
    add x1, x1, #15
    mov x2, #10
    mov x3, x0
    mov x4, x1

convert:
    udiv x5, x3, x2
    msub x6, x5, x2, x3
    add x6, x6, #'0'
    sub x1, x1, #1
    strb w6, [x1]
    mov x3, x5
    cbz x3, print_it
    b convert

print_it:
    sub x2, x4, x1
    mov x0, #1
    mov x8, #64
    mov x1, x1
    svc #0

    // Imprimir salto de línea
    ldr x1, =newline
    mov x2, #1
    mov x0, #1
    mov x8, #64
    svc #0
    ret