GNU/Linux xterm-256color bash 141 views

// ****************************************************************************
// * Lenguajes de Interfaz en TECNM Campus ITT
// * Nombre del archivo: practica.s
// * Descripción: Sumatoria de los numeros anteriores a N
// * Autor: [America Fernanda Nevarez de la Cruz]
// * Fecha: [2025-04-08]
// * Plataforma: Raspberry Pi OS (64 bits)
// * Demostración:  [https://asciinema.org/a/XUHpQoVHgZ5HN15ilrIat5jTc]
// ****************************************************************************

// ****************************************************************************
// * Codigo equivalente en C
// *import java.util.Scanner;
// *
// *public class Sumatoria {
// *    public static void main(String[] args) {
// *        Scanner scanner = new Scanner(System.in);
// *        int suma = 0;
// *
// *        // * Mostrar mensaje de ingreso
// *        System.out.print("Ingrese un numero: ");
// *
// *        // * Leer número del usuario
// *        int N = scanner.nextInt();
// *
// *        // * Calcular sumatoria del 1 al N
// *        for(int i = 1; i <= N; i++) {
// *            suma += i;
// *        }
// *
// *        // * Mostrar resultado
// *        System.out.println("Resultado: " + suma);
// *
// *        scanner.close();
// *    }
// *}
// ****************************************************************************

.section .bss
buffer: .skip 64

.section .data
msg_ingrese: .ascii "Ingrese un numero: "
len_ingrese = . - msg_ingrese

msg_resultado: .ascii "Resultado: "
len_resultado = . - msg_resultado

newline: .ascii "\n"
len_nl = . - newline

.section .text
.global _start

_start:
    // Mostrar mensaje
    ldr x0, =msg_ingrese
    mov x1, #len_ingrese
    bl print_msg

    // Leer número del usuario
    mov x0, #0
    ldr x1, =buffer
    mov x2, #64
    mov x8, #63
    svc #0

    // Convertir texto a número
    ldr x1, =buffer
    mov x2, #0         // acumulador
parse_loop:
    ldrb w3, [x1], #1
    cmp w3, #'0'
    blt fin_parse
    cmp w3, #'9'
    bgt fin_parse
    sub w3, w3, #'0'
    mov w4, #10
    mul w2, w2, w4
    add w2, w2, w3
    b parse_loop
fin_parse:
    mov x10, x2        // x10 = N (valor ingresado)

    // Sumatoria del 1 al N
    mov x3, #1         // i
    mov x4, #0         // suma = 0

sum_loop:
    cmp x3, x10
    bgt mostrar
    add x4, x4, x3
    add x3, x3, #1
    b sum_loop

mostrar:
    ldr x0, =msg_resultado
    mov x1, #len_resultado
    bl print_msg

    mov x0, x4         // x0 = suma
    bl print_number

    ldr x0, =newline
    mov x1, #len_nl
    bl print_msg

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

// --- Función: imprimir mensaje ---
print_msg:
    mov x2, x1
    mov x1, x0
    mov x0, #1
    mov x8, #64
    svc #0
    ret

// --- Función: imprimir número ---
print_number:
    mov x1, x0
    ldr x2, =buffer
    add x2, x2, #63
    mov w3, #0

convert_loop:
    mov x4, #10
    udiv x5, x1, x4
    msub x6, x5, x4, x1
    add x6, x6, #'0'
    strb w6, [x2], #-1
    mov x1, x5
    add w3, w3, #1
    cbnz x1, convert_loop

    add x2, x2, #1
    mov x1, x2
    mov w2, w3
    mov x0, #1
    mov x8, #64
    svc #0
    ret