Skip to content

Instantly share code, notes, and snippets.

@Joc193
Last active April 11, 2025 06:52
Show Gist options
  • Save Joc193/0f0ca565e7013940e5843ee6632b801d to your computer and use it in GitHub Desktop.
Save Joc193/0f0ca565e7013940e5843ee6632b801d to your computer and use it in GitHub Desktop.
Programa ARM64 - Calcular la media de los números: 2, 4, 6, 8, 10.
/*
* ---------------------------------------------------------------------------------
* Lenguajes de Interfaz en TECNM Campus ITT
* Autor: Jocelyn Alvarez Paniagua
* Fecha: 2025-04-10
* Descripción: Calcular la media de los números: 2, 4, 6, 8, 10.
* Demostración: [ https://asciinema.org/a/bArAYLZyMJvcD1ZqM6a4Uk9YB ]
* ---------------------------------------------------------------------------------
*/
.global _start
.section .data
mensaje: .asciz "Media = "
newline: .asciz "\n"
nums: .quad 2, 4, 6, 8, 10
buffer: .skip 32
.section .text
_start:
ldr x10, =nums // dirección base del arreglo
mov x11, #5 // cantidad de elementos
mov x12, #0 // índice
mov x13, #0 // acumulador
sum_loop:
cmp x12, x11
beq calcular_media
ldr x0, [x10, x12, LSL #3] // cargar número (64 bits por elemento)
add x13, x13, x0
add x12, x12, #1
b sum_loop
calcular_media:
mov x0, x13
mov x1, x11
udiv x0, x0, x1 // x0 = suma / total
// Imprimir mensaje "Media = "
mov x2, #8
mov x0, #1
ldr x1, =mensaje
mov x8, #64
svc #0
// Convertir resultado en x0 a ASCII
bl int_to_ascii
mov x0, #1
ldr x1, =buffer
mov x2, x23
mov x8, #64
svc #0
// Salto de línea
mov x0, #1
ldr x1, =newline
mov x2, #1
mov x8, #64
svc #0
mov x0, #0
mov x8, #93
svc #0
// --------------------------------------------------------
// Función: int_to_ascii
// Convierte número en x0 a ASCII en 'buffer' y devuelve longitud en x23
// --------------------------------------------------------
int_to_ascii:
mov x1, x0
ldr x2, =buffer
add x2, x2, #31
mov x23, #0
itoa_loop:
mov x3, #10
udiv x4, x1, x3
msub x5, x4, x3, x1
add x5, x5, #'0'
strb w5, [x2]
sub x2, x2, #1
add x23, x23, #1
mov x1, x4
cmp x1, #0
bne itoa_loop
add x2, x2, #1
mov x3, #0
ldr x5, =buffer
copy_ascii:
ldrb w6, [x2, x3]
strb w6, [x5, x3]
add x3, x3, #1
cmp x3, x23
blt copy_ascii
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment