// ****************************************************************************
// * Lenguajes de Interfaz en TECNM Campus ITT
// * Nombre del archivo: practica.s
// * Descripción: Suma los digitos que integran una cadena de numeros
// * Autor: [America Fernanda Nevarez de la Cruz]
// * Fecha: [2025-08-2025]
// * Plataforma: Raspberry Pi OS (64 bits)
// * Demostración: [https://asciinema.org/a/fxCm7VUpT5DFMdSIqsNMKPAen]
// ****************************************************************************
// ****************************************************************************
// Codigo en JAVA
// *public class SumaDigitos {
// * public static void main(String[] args) {
// * Scanner scanner = new Scanner(System.in);
// * int suma = 0;
// *
// * // * Mostrar mensaje
// * System.out.print("Ingrese un numero: ");
// *
// * // * Leer entrada del usuario
// * String input = scanner.nextLine();
// *
// * // * Sumar cada dígito
// * for (int i = 0; i < input.length(); i++) {
// * char c = input.charAt(i);
// *
// * // * Verificar si es dígito
// * if (Character.isDigit(c)) {
// * // * Convertir carácter a valor numérico y sumar
// * suma += Character.getNumericValue(c);
// * }
// * }
// *
// * // * Mostrar resultado
// * System.out.println("Suma de los digitos: " + suma);
// *
// * scanner.close();
// * }
// *}
// ****************************************************************************
.section .bss
buffer: .skip 64
.section .data
msg_ingrese: .ascii "Ingrese un numero: "
len_ingrese = . - msg_ingrese
msg_resultado: .ascii "Suma de los digitos: "
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 entrada del usuario
mov x0, #0
ldr x1, =buffer
mov x2, #64
mov x8, #63
svc #0
mov x9, x0 // x9 = longitud ingresada
ldr x1, =buffer // x1 apunta al buffer
// Inicializar suma
mov x3, #0 // x3 = suma
sumar_loop:
cbz x9, mostrar
ldrb w2, [x1], #1 // leer siguiente carácter
sub x9, x9, #1
cmp w2, #'0'
blt sumar_loop // ignorar si no es dígito
cmp w2, #'9'
bgt sumar_loop
sub w2, w2, #'0' // convertir ASCII a entero
add w3, w3, w2 // sumar a la suma
b sumar_loop
mostrar:
ldr x0, =msg_resultado
mov x1, #len_resultado
bl print_msg
mov x0, x3 // 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 para imprimir números ---
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