// ****************************************************************************
// * Lenguajes de Interfaz en TECNM Campus ITT
// * Nombre del archivo: practica.s
// * Descripción: Programa que genera numeros aleatorios entre 0 y 100
// * Autor: [America Fernanda Nevarez de la Cruz]
// * Fecha: [2025-04-08]
// * Plataforma: Raspberry Pi OS (64 bits)
// * Demostración: [https://asciinema.org/a/l5R7zxTu4hv4UDyV5KeXJ9SQg]
// ****************************************************************************
// ****************************************************************************
// * Codigo equivalente en javascript
// *// Función para generar un número aleatorio entre 0 y 100
// *function generarNumeroAleatorio() {
// * // * Math.random() genera un número decimal entre 0 (inclusive) y 1 (exclusive)
// * // * Multiplicamos por 101 para incluir el 100 y usamos Math.floor para redondear hacia abajo
// * return Math.floor(Math.random() * 101);
// *}
// *
// *// Función principal
// *function main() {
// * // * Generar 10 números aleatorios como ejemplo
// * for (let i = 0; i < 10; i++) {
// * // * Llamar a la función generadora
// * const numero = generarNumeroAleatorio();
// * // * Imprimir el número generado
// * console.log(`Número aleatorio ${i + 1}: ${numero}`);
// * }
// *}
// *
// *// * Llamar a la función principal
// *main();
// ****************************************************************************
.section .data
msg: .ascii "Numero aleatorio (0-100): "
len_msg = . - msg
nl: .ascii "\n"
len_nl = . - nl
.section .bss
timespec: .skip 16 // struct timespec (tv_sec + tv_nsec)
.section .text
.global _start
_start:
// clock_gettime(CLOCK_REALTIME, ×pec)
mov x0, #0 // CLOCK_REALTIME
ldr x1, =timespec
mov x8, #113 // syscall: clock_gettime
svc #0
// Cargar tv_nsec de timespec (offset 8)
ldr x1, [x1, #8] // x1 = nanosegundos
// Calcular aleatorio entre 0-100: x1 % 101
mov x2, #101
udiv x3, x1, x2 // x3 = x1 / 101
msub x4, x3, x2, x1 // x4 = x1 - (x3 * x2) = x1 % 101
// Mostrar mensaje
ldr x0, =msg
mov x1, #len_msg
bl print_msg
// Mostrar número en x4
mov x0, x4
bl print_int
// Mostrar salto de línea
ldr x0, =nl
mov x1, #len_nl
bl print_msg
// Salir del programa
mov x8, #93
mov x0, #0
svc #0
// --------------------------------------------------
print_msg: // x0 = ptr, x1 = len
mov x2, x1
mov x1, x0
mov x0, #1 // stdout
mov x8, #64 // syscall write
svc #0
ret
// --------------------------------------------------
print_int: // x0 = número a imprimir
sub sp, sp, #16 // reservar espacio
mov x1, x0 // x1 = número
mov x2, sp // buffer ptr
cmp x1, #0
b.ne int_convert
mov w3, #'0'
strb w3, [x2]
mov x2, #1
b int_print
int_convert:
mov x3, #10
mov x4, x2 // guardar ptr final
int_loop:
udiv x5, x1, x3
msub x6, x5, x3, x1
add x6, x6, #'0'
sub x4, x4, #1
strb w6, [x4]
mov x1, x5
cmp x1, #0
b.ne int_loop
sub x2, x2, x4 // longitud
mov x1, x4 // puntero inicio
int_print:
mov x0, #1
mov x8, #64
svc #0
add sp, sp, #16
ret