GNU/Linux xterm-256color bash 136 views

// ****************************************************************************
// * Lenguajes de Interfaz en TECNM Campus ITT
// * Nombre del archivo: practica.s
// * Descripción: Genera la tabla de multiplicar que el usuario ingrese.
// * Autor: [America Fernanda Nevarez de la Cruz]
// * Fecha: [2025-04-08]
// * Plataforma: Raspberry Pi OS (64 bits)
// * Demostración:  [https://asciinema.org/a/Xyn0qt7kpz6mPXvcp8dVdTJkZ]
// ****************************************************************************


// ****************************************************************************
// * Codigo equivalente en Python
// *n = int(input("Ingrese un numero: "))
// *for i in range(1, 11):
// *    print(f"{n} x {i} = {n*i}")
// ****************************************************************************

.section .bss
buffer: .skip 64

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

msg_x: .ascii " x "
msg_igual: .ascii " = "
nl: .ascii "\n"

.section .text
.global _start

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

    // Leer número
    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
parse_loop:
    ldrb w3, [x1], #1
    cmp w3, #'0'
    blt fin_parse
    cmp w3, #'9'
    bgt fin_parse
    sub w3, w3, #'0'
    mov x4, #10
    mul x2, x2, x4
    add x2, x2, x3
    b parse_loop
fin_parse:
    mov x20, x2      // Guardar el número ingresado

    // Inicializar contador de multiplicación: i = 1
    mov x21, #1

tabla_loop:
    cmp x21, #11
    b.eq end_programa

    // Imprimir: numero
    mov x0, x20
    bl print_number

    // Imprimir " x "
    ldr x0, =msg_x
    mov x1, #3
    bl print_msg

    // Imprimir: i
    mov x0, x21
    bl print_number

    // Imprimir " = "
    ldr x0, =msg_igual
    mov x1, #3
    bl print_msg

    // Calcular resultado: numero * i
    mul x0, x20, x21
    bl print_number

    // Imprimir salto de línea
    ldr x0, =nl
    mov x1, #1
    bl print_msg

    // i++
    add x21, x21, #1
    b tabla_loop

end_programa:
    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