GNU/Linux xterm-256color bash 191 views

// ****************************************************************************
// * Nombre del archivo: practica.s
// * Descripción: Enciende y apaga un led
// * Autor: Roldan Castro Luis Alberto
// * Fecha: 10-04-2025
// * Plataforma: Raspberry Pi OS (64 bits)
// * Asciinema: https://asciinema.org/a/8xp81VOiHQuK4AHpBmwqZLTkA
// ****************************************************************************

// ****************************************************************************
//  Version en Objetive C
//
// #import <AVFoundation/AVFoundation.h>
//
//@interface LEDController : NSObject
//+ (void)encenderLED;
//+ (void)apagarLED;
//@end
//
//@implementation LEDController
//+ (void)encenderLED {
//    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//    if ([device hasTorch] && [device isTorchModeSupported:AVCaptureTorchModeOn]) {
//        [device lockForConfiguration:nil];
//        [device setTorchMode:AVCaptureTorchModeOn];
//       [device unlockForConfiguration];
//    }
//}
//
//+ (void)apagarLED {
//    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//    if ([device hasTorch] && [device isTorchModeSupported:AVCaptureTorchModeOff]) {
//        [device lockForConfiguration:nil];
//        [device setTorchMode:AVCaptureTorchModeOff];
//        [device unlockForConfiguration];
//    }
//}
//@end
//
// Ejemplo de uso:
//int main() {
//    NSLog(@"Encendiendo LED...");
//    [LEDController encenderLED];
//    sleep(2); // Mantiene el LED encendido por 2 segundos
//    NSLog(@"Apagando LED...");
//    [LEDController apagarLED];
//    return 0;
//}
// ****************************************************************************

.section .data
msg_on:     .ascii "led encendido\n"
len_on = . - msg_on

msg_off:    .ascii "led apagado\n"
len_off = . - msg_off

.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-1: x1 % 2
    mov x2, #2
    udiv x3, x1, x2         // x3 = x1 / 2
    msub x4, x3, x2, x1     // x4 = x1 - (x3 * x2) = x1 % 2

    // Mostrar mensaje según valor
    cmp x4, #0
    beq led_off

led_on:
    ldr x0, =msg_on
    mov x1, #len_on
    bl print_msg
    b fin

led_off:
    ldr x0, =msg_off
    mov x1, #len_off
    bl print_msg

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