Last active
April 10, 2025 04:25
-
-
Save FranciscoJCE/3eb7263ad70e106e53e61b258f379341 to your computer and use it in GitHub Desktop.
Codigo Assembly ARM64 para RaspbianOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Programa: Cálculo de varianza | |
| * Autor: Francisco Javier Crisostomo Enciso | |
| * Descripción: Calcula la varianza de [5,10,15,20,25] con media 15 | |
| * Resultado esperado: 50 | |
| * ASCIINEMA: https://asciinema.org/a/Y8FMDyHKAwX7Pszf5KoBVMcPT | |
| */ | |
| .section .data | |
| arreglo: .word 5, 10, 15, 20, 25 | |
| tam: .word 5 | |
| media: .word 15 | |
| msg_result: .asciz "Varianza = " | |
| endl: .asciz "\n" | |
| .section .bss | |
| .lcomm num_buffer, 12 | |
| .section .text | |
| .global _start | |
| _start: | |
| // Inicialización | |
| ldr x0, =arreglo // x0 = dirección del arreglo | |
| ldr w1, tam // w1 = tamaño (n) | |
| ldr w2, media // w2 = media | |
| mov w3, #0 // w3 = suma acumulada (diff²) | |
| mov w4, #0 // índice i | |
| bucle: | |
| cmp w4, w1 // while i < 5 | |
| b.ge calcular_varianza | |
| // Cargar elemento arreglo[i] | |
| ldr w7, [x0, w4, SXTW #2] // w7 = arreglo[i] | |
| // Calcular diferencia = arreglo[i] - media | |
| sub w8, w7, w2 // w8 = diff | |
| // Calcular cuadrado: diff * diff | |
| mul w9, w8, w8 // w9 = diff² | |
| // Acumular en suma total | |
| add w3, w3, w9 // suma += diff² | |
| // i++ | |
| add w4, w4, #1 | |
| b bucle | |
| calcular_varianza: | |
| // varianza = suma / n | |
| udiv w10, w3, w1 // w10 = varianza | |
| // Mostrar "Varianza = " | |
| mov x0, #1 | |
| ldr x1, =msg_result | |
| mov x2, #11 | |
| mov x8, #64 | |
| svc #0 | |
| // Mostrar valor numérico | |
| mov w0, w10 | |
| bl imprimir_numero | |
| // Mostrar nueva línea | |
| mov x0, #1 | |
| ldr x1, =endl | |
| mov x2, #1 | |
| mov x8, #64 | |
| svc #0 | |
| // Salir | |
| mov x8, #93 | |
| mov x0, #0 | |
| svc #0 | |
| // ------------------------------------------------------------ | |
| // Función: imprimir_numero | |
| // Imprime el número en w0 | |
| // ------------------------------------------------------------ | |
| imprimir_numero: | |
| ldr x1, =num_buffer | |
| mov x2, #0 | |
| mov w3, #10 // Divisor | |
| mov w4, w0 // Copia del número | |
| // Manejar caso especial 0 | |
| cbz w4, handle_zero | |
| convert_loop: | |
| udiv w5, w4, w3 // w5 = número / 10 | |
| msub w6, w5, w3, w4 // w6 = número % 10 | |
| add w6, w6, #'0' // Convertir a ASCII | |
| strb w6, [x1, x2] | |
| add x2, x2, #1 // Incrementar contador | |
| mov w4, w5 // Actualizar número | |
| cbnz w4, convert_loop // Continuar si no es cero | |
| // Invertir dígitos | |
| mov x5, #0 // Índice inicial | |
| sub x6, x2, #1 // Índice final | |
| reverse_loop: | |
| cmp x5, x6 | |
| b.ge print_num | |
| ldrb w7, [x1, x5] | |
| ldrb w8, [x1, x6] | |
| strb w8, [x1, x5] | |
| strb w7, [x1, x6] | |
| add x5, x5, #1 | |
| sub x6, x6, #1 | |
| b reverse_loop | |
| handle_zero: | |
| mov w5, #'0' | |
| strb w5, [x1] | |
| mov x2, #1 | |
| print_num: | |
| mov x0, #1 | |
| ldr x1, =num_buffer | |
| mov x8, #64 | |
| svc #0 | |
| ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment