Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save FranciscoJCE/ef5c612d6d906cacd51f4c45578a3347 to your computer and use it in GitHub Desktop.
Codigo Assembly ARM64 para RaspbianOS
/*
* Programa: Cálculo de la media de números
* Autor: Francisco Javier Crisostomo Enciso
* Descripción: Calcula la media de los valores 2, 4, 6, 8, 10
* Resultado esperado: 6
* ASCIINEMA:https://asciinema.org/a/Z0NsWYYYbz155Q55aEi4euLca
*/
.section .data
valores: .word 2, 4, 6, 8, 10
nvalores: .word 5
msg_result: .asciz "Media = "
endl: .asciz "\n"
.section .bss
.lcomm num_buffer, 12
.section .text
.global _start
_start:
// Inicialización
ldr x19, =valores // Dirección del array
ldr w20, nvalores // Número de elementos
mov x21, #0 // Índice
mov x22, #0 // Acumulador de suma
sum_loop:
cmp x21, x20
b.ge calc_avg
// Cargar valores[i]
ldr w23, [x19, x21, LSL #2]
add x22, x22, x23 // Sumar al acumulador
add x21, x21, #1 // Incrementar índice
b sum_loop
calc_avg:
// Calcular media: suma / cantidad
udiv w24, w22, w20 // w24 = media
// Mostrar "Media = "
mov x0, #1
ldr x1, =msg_result
mov x2, #8
mov x8, #64
svc #0
// Mostrar valor numérico
mov w0, w24
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 // Usar registro w3 en lugar de x3
mov w4, w0 // Copia del número
// Manejar caso especial 0
cbz w4, handle_zero
convert_loop:
udiv w5, w4, w3 // Corregido: usar w3 en lugar de x3
msub w6, w5, w3, w4 // Corregido: usar w3 en lugar de x3
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