Skip to content

Instantly share code, notes, and snippets.

@Alex-JAML
Created April 11, 2025 06:39
Show Gist options
  • Select an option

  • Save Alex-JAML/3ae864738b9c5b284f06712cb6ee2e5f to your computer and use it in GitHub Desktop.

Select an option

Save Alex-JAML/3ae864738b9c5b284f06712cb6ee2e5f to your computer and use it in GitHub Desktop.
Codigo Assembly ARM64 para RaspbianOS
/*
* ---------------------------------------------------------------------------------
* Lenguajes de Interfaz en TECNM Campus ITT
* Autor: Jorge Alejandro Martinez Lopez
* Fecha: 2025-04-09
* Descripción: Cálculo de probabilidad de obtener al menos 2 impares en 4 lanzamientos
* de un dado estándar. Implementado en Assembly ARMv8.
* Matemáticas: P = [C(4,2)*(3/6)^2*(3/6)^2 + C(4,3)*(3/6)^3*(3/6)^1 + C(4,4)*(3/6)^4]
* = 15/16 = 93.75%
* ---------------------------------------------------------------------------------
*/
/*
* ----------------------------------------------
* C# Referencia (Operaciones equivalentes):
* ----------------------------------------------
* using System;
*
* class Program {
* static double Combinatoria(int n, int k) {
* if (k == 0 || k == n) return 1;
* return Combinatoria(n-1, k-1) + Combinatoria(n-1, k);
* }
*
* static void Main() {
* int lanzamientos = 4;
* double prob = (Combinatoria(4,2) + Combinatoria(4,3) + Combinatoria(4,4)) / Math.Pow(2,4);
* Console.WriteLine($"Probabilidad: {prob:P2}");
* }
* }
*/
.global _start
.section .data
combinaciones: .word 11 // C(4,2)+C(4,3)+C(4,4) = 6+4+1 = 11
espacio: .word 16 // 2^4 = 16
factor: .word 100 // Para porcentaje
msg: .ascii "Probabilidad: "
resultado: .ascii "93.75%\n"
msglen = . - msg
.section .text
_start:
// 1. Cargar valores
ldr x0, =combinaciones
ldr w0, [x0] // w0 = 11
ldr x1, =espacio
ldr w1, [x1] // w1 = 16
ldr x2, =factor
ldr w2, [x2] // w2 = 100
// 2. Calcular (11/16)*100
mul w3, w0, w2 // 11*100 = 1100
udiv w4, w3, w1 // 1100/16 = 68 (parte entera)
mul w5, w4, w1 // 68*16 = 1088
sub w6, w3, w5 // 1100-1088 = 12 (resto)
// 3. Calcular decimales (12*100)/16 = 75
mul w6, w6, w2 // 12*100 = 1200
udiv w7, w6, w1 // 1200/16 = 75
// 4. Convertir a ASCII
// Parte entera (93)
mov w8, #10
udiv w9, w7, w8 // 75/10 = 7
msub w10, w9, w8, w7 // 75-70 = 5
add w9, w9, #48 // '7'
add w10, w10, #48 // '5'
// 5. Construir resultado
ldr x11, =resultado
mov w12, #57 // '9'
strb w12, [x11] // Posición 0
mov w12, #51 // '3'
strb w12, [x11, #1] // Posición 1
mov w12, #46 // '.'
strb w12, [x11, #2] // Posición 2
strb w9, [x11, #3] // '7' (posición 3)
strb w10, [x11, #4] // '5' (posición 4)
mov w12, #37 // '%'
strb w12, [x11, #5] // Posición 5
// 6. Mostrar resultado
mov x0, #1
ldr x1, =msg
mov x2, msglen
mov x8, #64
svc #0
// 7. Terminar
mov x0, #0
mov x8, #93
svc #0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment