Skip to content

Instantly share code, notes, and snippets.

@FranciscoJCE
Last active April 10, 2025 04:27
Show Gist options
  • Select an option

  • Save FranciscoJCE/008488af3d803959f78fe041d37051d9 to your computer and use it in GitHub Desktop.

Select an option

Save FranciscoJCE/008488af3d803959f78fe041d37051d9 to your computer and use it in GitHub Desktop.
Codigo Assembly ARM64 para RaspbianOS
/*
* Programa: Cálculo de la mediana
* Autor: Francisco Javier Crisostomo Enciso
* Descripción: Calcula la mediana de un arreglo ordenado [3,7,8,12,14]
* Resultado esperado: 8
* ASCIINEMA:https://asciinema.org/a/3FF5tewxOSTA0RwUgAozaiWMz
*/
.section .data
arreglo: .word 3, 7, 8, 12, 14 // Arreglo ordenado
tam: .word 5 // Tamaño del arreglo
msg_result: .asciz "Mediana = "
endl: .asciz "\n"
.section .bss
.lcomm num_buffer, 12
.section .text
.global _start
_start:
// Cargar dirección base del arreglo
ldr x0, =arreglo // x0 = dirección base
// Calcular índice medio (tamaño/2 para arreglos impares)
ldr w1, tam // w1 = tamaño del arreglo
lsr w2, w1, #1 // w2 = tamaño / 2 (índice medio)
// Calcular dirección del elemento medio
lsl x2, x2, #2 // Multiplicar índice por 4 (tamaño word)
add x3, x0, x2 // x3 = dirección del elemento medio
// Cargar la mediana
ldr w4, [x3] // w4 = valor de la mediana
// Mostrar "Mediana = "
mov x0, #1
ldr x1, =msg_result
mov x2, #10
mov x8, #64
svc #0
// Mostrar valor numérico (usando w4 directamente)
mov w0, w4
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