GNU/Linux xterm-256color bash 185 views

// ****************************************************************************
// * Nombre del archivo: practica.s
// * Descripción: Numeros aleatorios entre 0 y 10
// * Autor: Roldan Castro Luis Alberto
// * Fecha: 10-04-2025
// * Plataforma: Raspberry Pi OS (64 bits)
// * Asciinema: https://asciinema.org/a/65lMb8CAeeNyDqcivUzTk7H1d
// ****************************************************************************

// ****************************************************************************
// Version en JAVA
//
//import java.util.Random; // Importamos la clase Random
//
//public class RandomNumber { // Definimos la clase principal
//    public static void main(String[] args) { // Método principal
//        Random random = new Random(); // Creamos un objeto Random
//        int numeroAleatorio = random.nextInt(11); // Genera un número entre 0 y 10 (inclusive)
//        
//        // Imprimimos el número generado
//        System.out.println("Número aleatorio entre 0 y 10: " + numeroAleatorio);
//    }
//}
// ****************************************************************************

.section .data
msg: .ascii "Numero aleatorio (0-10): "
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, &timespec)
    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-10: x1 % 11
    mov x2, #11
    udiv x3, x1, x2         // x3 = x1 / 11
    msub x4, x3, x2, x1     // x4 = x1 - (x3 * x2) = x1 % 11

    // 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